Thursday, December 13, 2007

Widget Server Spring Framework Integration

As Java lightweight framework, Spring is widely used in Java world. it is because Spring is simple, light, and easy to integrate with other frameworks. Widget Server Framework, as one of Xml Gui Implementation with Java, gives us a great solution, that is when we make an application, we can use it as a standalone, Thin Client ,and web based app without changing the code.

The main idea when we want to integrate this two frameworks (using Widget Server at Presentation Layer and Spring at Bussines Layer) is we have to make Widget Server call Spring Application Context/Bean Factory(i.e file applicationContext.xml) when Widget Server Servlet is initialized by Servlet Container.

First thing we should do is creating Widget Server mandatory directory in spring project directory

for example at Spring "DistroSystem" Project i have war/ and src/ directory

DistroSystem
|—-src
|
|—war–
|
|–WEB-INF
|
|—lib
|
|—classes

Widget Server Mandatory directory is

|
|–*.xml
|—start
| |
| |–src
|
|–WEB-INF
|
|—lib
|
|—classes

Next, move Widget Server's “start” directory to Spring's “war” directory, After that move all file (not directory just file) under to “war” directory. Move all Widget Server library under “lib” to Spring's “lib”. For Widget Server "src" directory, move all its content to spring's “src” directory.

After that directory structure must like this:

DistroSystem
|
|—-src
| |
| |–Wiser Java Source
| |–Old Bussines and/
| or persistence Java source
|—war–
|
|–start
|
|–WEB-INF
|
|—lib
| |–Spring And Wiser library
| dependency
|
|—classes

Step 2 is modifying piccolo.jar, theWiser library dependency. Extract piccolo.jar . Remove all piccolo's xml parsers package(folder javax). and compress piccolo again. So in this new piccolo.jar only exist "com" and "META-INF" folder. After this you can write your web.xml.

Step 3 is developing your presentation layer and Listener code. In order to make Listener code get bean from Spring, We must make parent listener for all listeners which we want it to call Spring bean code. Here is my code
package Listeners;

import de.ug2t.unifiedGui.service.UnBasicServlet;
import de.ug2t.kernel.KeRegisteredObject;
import javax.servlet.*;
import javax.servlet.http.*;

import org.springframework.web.context.*;
import org.springframework.web.context.support.*;

public abstract class BasicListener{
private WebApplicationContext wac ;

public BasicListener()
{
ServletConfig sg=
(ServletConfig)KeRegisteredObject.pcmf_getGlobalObjByName
(UnBasicServlet.SERVLET_CONFIG);
ServletContext sc =sg.getServletContext();
wac = WebApplicationContextUtils.getWebApplicationContext(sc);
}

public WebApplicationContext getWebApplicationContext()
{
return wac;
}
}

This is one of my Widget Server listener code which use Spring's Bean

package Listeners;

import de.ug2t.kernel.*;
import de.ug2t.unifiedGui.*;
import de.ug2t.unifiedGui.interfaces.*;
import de.ug2t.unifiedGui.loader.*;

import java.util.*;
import DistroSystem.Stock.DAO.*;
import DistroSystem.Stock.Logic.*;
import DistroSystem.*;

import org.springframework.web.context.*;

public class BarangGetAll_li extends BasicListener implements IUnGuiEventListener
{

private BukuManager buku_man;

public void pcmf_execListener(UnComponent xParam) throws Exception
{
WebApplicationContext wac = getWebApplicationContext();
buku_man=(BukuManager)wac.getBean("bukuManager");
// get all relevant widgets from the registry
IUnTable l_table = (IUnTable) KeRegisteredObject.pcmf_getObjByName("atable");
UnTableLoader l_load = (UnTableLoader) KeRegisteredObject.pcmf_getObjByName("tableLoader");

List list = buku_man.getAll();

// insert row
((BukuTableModel) l_load.pcmf_getModel()).setBuku(list);
l_table.pcmf_repaint();

return;
}
}

When we want to get beans's Spring we just must extend BasicListener class. To get a bean first call super method getWebApplicationContext(), this method will return "WebApplicationContext" object. With this object you can get all beans from Spring's Application Context

No comments: