Thursday, January 28, 2010

Injecting request and response objects of GWT client in Spring.

Hi,


In my last post I had explained how I integrated Spring with GWT by using the external library. Now I had another issue, in which I wanted the HttpSession object in the service class. I had to retrieve some variables from the session object. Since I wasn't extending my service layer class from RemoteServiceServlet I couldn't use the methods to get access to the request and response objects and hence I couldn't get the session object. I discovered that I there were some helper classes in the same external library which could be used and injected using Spring AOP. There is a RequestInjection class which was used  by us as an interceptor to the request.
It was mapped in the spring-servlet file in the following manner.


< bean id="requestSetter" class="org.gwtwidgets.server.spring.RequestInjection" >
        < property name="requestSetterName" value="setRequest"/ >
        < property name="responseSetterName" value="setResponse" / >
    < / bean >
< bean id="testServiceProxy"
          class="org.springframework.aop.framework.ProxyFactoryBean" >
          < property name="target" ref="testService" / >
          < property name="autodetectInterfaces" value="true" / >
          < property name="interceptorNames" >
              < list >
                  < value >requestSetter< / value >
              < / list >
          < / property >
    < / bean >

Wednesday, January 27, 2010

Using Spring with GWT (Google Web Toolkit)

Hi All,
I have been working on the Google web toolkit (GWT) technology version 1.6 lately. We had a task at hand to integrate server side spring technology and use GWT at the client side. GWT is an AJAX based framework developed by google and all the server side calls are processed through AJAX. The catch here is since GWT dispatches only RPC calls to the server. To integrate spring framework, an external library gwt-sl-0.1.5b.jar  was used in which we have a support package for spring integration. This discovery helped us to solve most of out problems. A GWTHandler class was used to handle the RPC requests from the GWT client.
This class is mapped in the spring-servlet file on the server side as follows:


     < bean class="org.gwtwidgets.server.spring.GWTHandler" >
          < property name="mappings" >
              < map >
                      < entry key="/module/myTestModule/test.rpc" 

                                   value-ref="test" / >
              < / map >
          < / property >
       < / bean > 


Prior to using this method, the service classes on the server side used to extend from the RemoteServiceServlet class. But using this mapping I could avoid extending from any classes and hence keeping the class free to extend from any other application specific classes.

Monday, October 12, 2009

How to write to a .properties file dynamically from java code?

We all must have used the properties file for using our desktop\web application in different locales. But once in one of the projects I encountered a situation where we had to write the configuration settings to a properties file which were a one time activity from the end user's side.

We followed the following approach to write to a .properties file from java code

File propFile = new File(System.getProperty("user.dir")+"src\\uts\\config\\envConfig.properties");
Properties prop = new Properties();

try
{
   FileInputStream fis = new FileInputStream(propFile);
   prop.load(fis);
   prop.setProperty("env_"+environmentName, environmentName);
   prop.setProperty("uid_"+userId, userId);
   prop.setProperty("pass_"+password, password);
   prop.store(new FileOutputStream(propFile), null);
}
catch(Exception e)
{
  e.printStackTrace();
}

Tuesday, September 29, 2009

Serialization

Serialization is a technique to write object's state to a data file. In order to be able to write the object to a file the class whose instance (object) we need to write has to implement the marker interface Serializable. This interface does not contain any methods.There are two methods that we use to serialize and deserialize an object, they are writeObject(Object) , readObject(). The writeObject method is in the ObjectOutputStream class while the readObject() method is in the ObjectInputStream class. All the instance variables of the object are saved and retrieved successfully as they were saved provided no instance variable is marked with the keyword transient. This keyword if used specifies that the state of that instance variable is not to be saved. 

Following code demonstrates how to do serialization

Monday, September 28, 2009

What can be done with enums?

This post shows the various things that enums can do. Enum data type was introduced since Java 5 and is a very powerful type. It is a special type of class which contains constant objects, instance variables,constructors,overloaded constructors, methods and overloaded methods as well. Enums can be declared outside or inside the class.Another important feature of enums is semicolon is optional.


Sunday, September 20, 2009

Calling new on an object

When can we call a new on an object? We normally use a new keyword in java to create a new object as follows:
Integer i = new Integer();
But can we use the new keyword on an object itself as follows:
TestClass.MyClass c = new TestClass().new MyClass();

Wednesday, September 16, 2009

Generics with non - generics

When we use generics we use the syntax as List< Integer > newList= new ArrayList< Integer >(), Can there be a way by which I can put a string into it. How can I cheat....????
Of course there is a way. This is by mixing generic code with non-generic code.
This is how it can be done.
public class TestGeneric{
   public static void main(String args[])
  {
       List newList = new ArrayList();
       newList.add(7); // autoboxing feature
       newList.add(new Integer(10)); // normal adding objects
       insert(newList);
  }
  public void insert(List nonGenericList)
 {
     nonGenericList.add("42"); // valid compiles fine and surprisingly runs fine too.....check the reason y below
 }
}