Dear Coders,

This web page is dedicated to the code lines compiled by the contributors to solve various challenges. You can find valuable examples and snippets within our posts and leave your comments.
M.C.

Java Hash to Hex String (for DB Storage)

1
String dbStoreableHash = new java.math.BigInteger(1, MessageDigest.getInstance("SHA").digest(thePassword.getBytes())).toString(16);
VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)

Binding the JNDI Initial Context from Code

You can use the following code in your main function to create a datasource with the JNDI name which is usually used by the application server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
            InitialContext ic = null;
            OracleConnectionPoolDataSource ds = null;
            PropertyBase pmap = PropertyBase.getInstance(); 
            try {
                  System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
                  System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
                  ic = new InitialContext();
 
                  ds = new OracleConnectionPoolDataSource();
                  ds.setURL("jdbc:oracle:thin:@my_server_name:1521:SRV_NAME");
                  ds.setUser("username");
                  ds.setPassword("password");
 
                  ic.createSubcontext("jdbc");
                  ic.bind("jdbc/MY_DS_NAME", ds);
            } catch (Exception e) {
                  logger.error("", e);
            }
VN:F [1.9.8_1114]
Rating: 10.0/10 (1 vote cast)

Posting UTF-8 Data

Extending the PostMethod class to support UTF-8 data posting.

1
2
3
4
5
6
7
8
9
10
11
12
13
      public static class UTF8PostMethod extends PostMethod {
            public UTF8PostMethod(String url) {
                  super(url);
            }
 
            @Override
            public String getRequestCharSet() {
                  return "UTF-8";
            }
      }
 
      PostMethod method = new UTF8PostMethod("http://www.google.com");
      method.addParameter(new NameValuePair("q", "something with special chars"));
VN:F [1.9.8_1114]
Rating: 10.0/10 (1 vote cast)

Tooltip for GWT table

If you want to add tooltip for the pure GWT table (com.google.gwt.user.cellview.client.CellTable) we can suggest the following lines;

1
2
3
4
5
6
7
8
9
	Column<String[], SafeHtml> objectNameColumn = new Column<String[], SafeHtml>(new SafeHtmlCell()) {
		@Override
		public SafeHtml getValue(String[] object) {
			String val = (object[index]==null?"":object[index]); 
			return new SafeHtmlBuilder().appendHtmlConstant("<span title='" + 
				new SafeHtmlBuilder().appendEscaped(val).toSafeHtml().asString() + "'>" + val + "</span>").toSafeHtml();
		}
	}; 
	cellTable.addColumn(objectNameColumn, colLabel);
VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)

A quick reminder!

If you use Java String’s replaceAll method and not using rgexp groups, do not forget to use Matcher.quoteReplacement method.
Otherwise, you will get “illegal group reference” or “”String index out of range” error when the $ sign occurs in the replacement value (2nd parameter).

Example:

1
2
3
4
5
6
7
8
9
10
	System.out.println("replace the dollar sign".replaceAll("dollar sign", "$"); 
	//Error: "String index out of range: 1"
	System.out.println("replace the dollar sign".replaceAll("dollar sign", "$ sign"));
	//Error: "Illegal group reference"
	System.out.println("replace the dollar sign".replaceAll("dollar sign", Matcher.quoteReplacement("$")));
	//"replace the $"
 
	// or you always have the comfortable StringUtils.replace option
	System.out.println(StringUtils.replace("replace the dollar sign", "dollar sign", "$")); 
	//"replace the $"
VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)

Separating log files in Jboss 5

You have more than one war application in your JBoss 5.1.0 GA server and some of your applications have to use same libraries.

Eventually, all logs go into the same server.log file and you cannot find out where the lines are coming from because of the shared libs.

There are many suggestions and information about seperating log files on web. Some of them targeting JBoss 4 and other a few solutions that I’ve seen are not easy to maintain later.

So, here is the simplest solution I’ve found for myself.

1. Each application initializes it’s own log4j as described here.

2. Then, log4j.xml should be the one which described in the bottom of this page.

You do not have to change any JBoss configuration in this approach.

VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)

Injecting JAX-WS UsernameToken for Apache CXF

To inject Username Token from API for a JAX-WS service client you can use;

1
2
3
	Map<String, Object> reqctx = ((BindingProvider)p).getRequestContext();
	reqctx.put("ws-security.username", "my_username");
	reqctx.put("ws-security.password", "my_password");

where p is the port object.

But keep in mind that this is specific to Apache CXF. If you are using WSIT you should use BindingProvider.USERNAME_PROPERTY and BindingProvider.PASSWORD_PROPERTY property names.

VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)

Lambda Convert

If you want to modify the contents of your array in c# you can use ConvertAll and lambda expressions.
For example; to get rid of the extensions of the file names within a directory, you can simply use the following code line.

1
            String[] files = Array.ConvertAll<string, string>(Directory.GetFiles("MyFolder"), element => element.Split('.')[0]);
VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)

Delimited string to table

If you need to split string easily in plsql, this is the select statement. :)

1
2
3
	SELECT regexp_substr (str, '[^,]+', 1, ROWNUM) split
	FROM (SELECT 'ABC,DEF,GHI,JKL,MNO' str FROM dual)
	CONNECT BY LEVEL &lt;= LENGTH (regexp_replace (str, '[^,]+'))  + 1 ;
VN:F [1.9.8_1114]
Rating: 10.0/10 (3 votes cast)

Unreal Detour

The following c++ code is a detour call for UNREAL tournament engine, which is used for providing frames to an image server.


Continue reading…

VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)