Tag Archives: wtp

GWT with Cypal Studio

GWT 1.5 (official version is 1.5.2) is now finally released and not longer a release candidate. GWT is very nice as AJAX toolkit for a java programmer. You code your application in pure Java and all the JavaScript stuff is done behind the scenes. Important is to understand the build process. The Java Source Code is translated by the GWT Compiler to JavaScript Code. This GWT Compiler understands Java 1.5 with limited API. Eclipse has it’s own built-in Java compiler corresponding to the compiler level of the Java Project. Usage of unsupported Java functions will only be shown by the GWT compiler. Eclipse can only limit the access by setting access rules on the used JRE. In the project preferences under Java Build Path -> Libraries is the JRE. Under this Tree element is the access rules item. In the edit dialog can e.g. the usage limited to java.lang.* by setting “java/lang/*” as accessible. All other classes will not be available and cause compile errors in eclipse.

GWT ships with command line tools to create a new eclipse project including batch files to run the GWT compiler and launch files to start the hosted mode of GWT. Hosted mode starts a embedded tomcat and special GWT browser. The refresh button of this browser recompile the Java Code to JavaScript. Changes in the Java Code will directly shown in the browser after refresh. No restart is required.

GWT has two main concepts for a GWT application. The module defines the main class for a application and the AsyncCallbacks the service layer for accessing server side logic. The module name must be defined for the applicationCreator gwt command line tool as a java package name like com.mypackage.myapp.Main. Inside the source folder of the created eclipse project is the xml file with the gwt project definition and a Java class extending EntryPoint. The EntryPoint onModuleLoad method creates the UI of our GWT module. In the GWT docs online you will find a nice gallery with available ui elements called widgets. Remember that all code inside this method will be translated into JavaScript code.

Accessing business logic requires to define a interface extending RemoteService and a @RemoteServiceRelativePath annotation containing the name of the Service. This service must be implemented by class extending RemoteServiceServlet and implementing your interface. Additionally is a interface needed called <yourInterfaceName>Asyn, which contains the same signatures with a added AsyncCallback callback parameter e.g.

 
void sayHello(String text, AsyncCallback<String> callback);
final MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class); service.sayHello(textbox.getText(), new AsyncCallback<String>() { public void onFailure(Throwable caught) { Window.alert(caught.getMessage()); } public void onSuccess(String result) { Window.alert(result); } }); } });

So many steps has to be made by hand to code a GWT app in this way.

Cypal Studio is a open source eclipse plugin to extend web tools platform of eclipse with GWT. Just extract the install zip file to the plugins subfolder of eclipse 3.3. With Eclipse 3.4 (aka Ganymede) you have to use the dropins/<pluginname>/plugins folder. Cypal integrates as a facet for dynamic web projects. Best environment is a JRe 1.5, GWT 1.5 and a Tomcat 5.5 with this JRE as run time. Call the New Project wizard of Eclipse and choose Dynamic web project. Choose the Tomcat 5.5 as target run time, set web modules version to 2.4 and choose “Cypal Studio for GWT” as configuration. Change either the workspace or project JRE to 1.5. If not done before you must now define the GWT home folder. Give here the path to the extracted GWT 1.5.2 archive. Select under File->New->Other..->Cypal Studio->GWT Module. Give the Module a Java Package name and and a name. The EntryPoint class, the html and the gwt xml file will be created. Under Run->Run configurations you will find “GWT hosted mode application”. Click on the new button, select the Project and click on the run button. The application will be compiled by GWT and the internal browser of GWT started with your Module. Remote Services can be created with File->New->Other..->Cypal Studio->GWT Remote Service. Enter here a name and a uri. Advised is to use the same value for Name and service URI e.g. MyService. This creates a public interface, a async interface, a implementation and change your gwt xml file to include this service as a servlet. Remember to use gwt serializable objects as parameters and return value for your service methods.

apache cxf 2.1 with spring 2, maven 2 and eclipse wtp

Apache CXF 2.1 is released. I know XFire since a long time as web service provider. CXF is a merger of XFire with celtix, which provided an enterprise service bus. The main reason for choosing XFire rather then Axis was the WS-I BP compatibility. It’s a better arguing with a commercial eai provider when your webservice is at least WS-I BP 1.0 conform. CXF is JAX-WS 2.1 compatible. You can enable Pojo services with standard annotations as web services. To try that new release out i created with eclipse web tools platform a new dynamic web project with tomcat 6 as web container. First step is to add the libs needed for CXF. Normally i would download by myself the libs and copy it in to Web-inf/lib folder. Disk space is not a problem but you can’t share such projects by mail because of the size needed for the libs. So i use maven 2 to define the needed libs. The pom.xml define the project and its dependencies. Place it in the root of the project:



	4.0.0
	de.schaeftlein.dev
	cxf-sample
	war
	
	
	0.0.1-SNAPSHOT
	
	
	
		
			org.apache.cxf
			cxf-rt-core
			2.1
		
		
			org.apache.cxf
			cxf-rt-frontend-simple
			2.1
		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			2.1
		
		
			org.apache.cxf
			cxf-rt-databinding-aegis
			2.1
		
		
			org.apache.cxf
			cxf-rt-transports-local
			2.1
		
		
			org.apache.cxf
			cxf-rt-transports-http
			2.1
		
		
			org.apache.cxf
			cxf-rt-transports-http-jetty
			2.1
		
		
			org.apache.cxf
			cxf-rt-transports-jms
			2.1
		
		
			org.apache.cxf
			cxf-rt-management
			2.1
		
		
			org.apache.cxf
			cxf-common-utilities
			2.1
		
		
			org.mortbay.jetty
			jetty
			6.1.6
		
		
			junit
			junit
			3.8.2
			test
		
		
			org.springframework
			spring
			2.0.8
		
	
	
		
			
				maven-compiler-plugin
				
					1.6
					1.6
				
			
		
		war:install
	


So the libs are defined but nothing happened in eclipse. I use the M2 Eclipse plugin. Now i enable maven with right click on the project and choose from M2 the “Enable dependency management” entry. The plugin now download the libs into the local repository for all projects. In the eclipse build path you will find now a new library called “maven dependencies”. Eclipse WTP does not deploy out of the box this library. Inside the project configuration you will find a section called “j2ee module dependencies”. Just click the check box beside the “maven dependencies” library. Now all dependent libs are deployed as well. As controller act the CXFServlet and for spring need a listener to be defined both in the web.xml:



  cxf-sample
	
		contextConfigLocation
		WEB-INF/beans.xml
	

	
		
			org.springframework.web.context.ContextLoaderListener
		
	

	
		CXFServlet
		
			org.apache.cxf.transport.servlet.CXFServlet
		
		1
	

	
		CXFServlet
		/*
	

I use spring as main configuration tool. CXF depends on spring 2.0.8. The beans.xml imports the default cxf settings and defines the HelloWorld service:

The web service has a interface:

package demo.spring;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);
}

and a implementation:

package demo.spring;

import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        return "Hello " + text;
    }
}

If not set up a local server during creation of the project you need to go to the servers tab in eclipse. Define a new server with server type “Tomcat v6.0 Server”. If the adapter does not appear click on the link to “download additional server adapters”. Under “installed runtime” should be a tomcat 6 server. If not you have to add a new runtime. For tomcat 6 i recommend at least java 1.5 as jre. In the last step you can directly add the created project for cxf. Spring and CXF use commons logging for logging. So i provide in the classpath a simple log4j.xml to output everything interesting to std out:








	
		
			
		
	
	
		
	
	
		
		
	

Setup and implementation are now done. So i started the server inside eclipse. The console should contains lines from spring configuring cxf. To see a list of deployed web services just open a browser to http://localhost:8080/cxf-sample/services, where cxf-sample is the name of your eclipse project. You should see a list of links to the generated wsdl’s of the defined services. Copy the url to the HelloWorldImplPort in the clipboard. For testing i use the free tool soapui which will be started and installed by webstart. Choose under file the point “new wsdl project”. Give it a name and paste the url from the wsdl. Leave the tack for creating sample requests and choose ok. In the tree you see the project, the service, the operations and a request for each operation. Double Click on the only request for our service. Replace the ? with a value of your own inside the arg0 tag and hit the green run button:


   
   
      
         
         
         John Doe
      
   

You should see a response like that on right side of the request in a new window:


   
      
         Hello John Doe