Monday, December 6, 2010

Writing a sample EJB Client Program using EJB 3.0

This post describes of creating a sample EJB Stateless bean and testing it in the JEE Server and it is based on EJB 3.0. Hope it will be useful..


DepartmentPersonIntf.java

package samplestateless;
public interface DepartmentPersonIntf{ //public void addNewDepartments(String name,String location); //public void addPersonInDept(String name); //public List getPersonInDept(String name);
public String getMessage(String name);
}


SampleStatelessbean.java

package samplestateless;
import javax.ejb.Stateless;import javax.ejb.Remote;import samplestateless.DepartmentPersonIntf;
@Stateless(name="SampleStatelessbean")@Remote(samplestateless.DepartmentPersonIntf.class)
public class SampleStatelessbean implements samplestateless.DepartmentPersonIntf{ public String getMessage(String name){ return "Message from Server :" +name; } public static void main(String[] args) { System.out.println("Hello World!"); }}

in META-INF create an xml sun-ejb-jar.xml with the below text.

<span><sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>SampleStatelessbean</ejb-name>
<jndi-name>SampleStatelessbean</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar></span>


Use the below command for creating a jar.
jar -cvf ejbtest.jar samplestateless META-INF

SampleEJBTest.java

package samplestateless;
import javax.naming.Context;import javax.naming.InitialContext;import samplestateless.DepartmentPersonIntf;import java.util.Properties;
public class SampleEJBTest{ public static void main(String[] args) throws Exception { DepartmentPersonIntf departmentPersonIntf; Properties prop = new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory"); prop.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming"); prop.put(Context.PROVIDER_URL, "localhost:4848"); InitialContext initialContext = new javax.naming.InitialContext(prop); departmentPersonIntf = (DepartmentPersonIntf)initialContext.lookup("SampleStatelessbean"); String message = departmentPersonIntf.getMessage("Unit Testing"); System.out.println("Hello World!"+message); }}

Create an EJBTest.jar file and deploy it in the server to test the above program.

Sunday, November 21, 2010

String.replaceAll() vs String replaceAll code

String manipulation is one of the most commonly used aspect of programming. We do a lot of string manipulation like replacing a particular word in a string, stripping the string of unwanted words/tags, concatenating a string etc., .

It is very easy to introduce inefficiencies in the code while doing string manipulation. Programming languages provides a lot of built in API’s to manipuate a string. There is a very little chance for the built in API(s) to have inefficient code. Mostly it is the incorrect usage of API’s by us which brings in the inefficiency.

Let us take a case to illustrate this point,

Do a HTML escape for the given html document. The code should convert all the <, > , & , ‘ , ” in the string to < , > , & , ' , " respectively.

Source: Codeexperiments

Version 1:

This can be easily accomplished by using the java api String.replaceAll(String regex, String replacement). The following code does the html escaping.

String str = “

Hello World
”;
str = str.replaceAll(“<”,”<”); str = str.replaceAll(“>”,”>”);
str = str.replaceAll(“\”",”"”);
str = str.replaceAll(“\’”,”'”);
str = str.replaceAll(“\’”,”&”);

As you can see the code is very simple and clean. Does the job of HTML escaping the given string. But is it efficient?

Version 2:

We shall try to do HTML escaping without using the String.replaceAll() method. The following code analyzes each character in the given string and replaces it with the appropriate escaped value.

StringBuilder strBuilder = new StringBuilder();
int size = strIn.length();
for(int i = 0 ; i < charval =" strIn.charAt(i);">’:
{
strBuilder.append(“>”);
break;
}
case ‘\”:
{
strBuilder.append(“'”);
break;
}
case ‘\”‘:
{
strBuilder.append(“"”);
break;
}
case ‘&’:
{
strBuilder.append(“&”);
break;
}
default:
{
strBuilder.append(charVal);
}
}
}

String escapedString = strBuilder.toString();

Results:

Each version of the code was run 1000 times in a loop and here are the results

Version 1: Time taken for String.replaceAll(str,str) ::250 ms
Version 2: Time taken for custom replace all code ::15 ms

On running both the version of the code, it is found the version 2 which did not use the String.replaceAll() was on an average 10 to 15x faster than the version 1 of the code which used String.replaceAll().