sábado, 20 de junio de 2009

java2script SWT portlets

author: Sebastián Gurin
Añadir vídeo
In this document I will try to explain how to make a simple jsp portlet with a java2script swt application as its contents. We will use liferay portal for testing it. You can download the portlet WAR file and project sources from java2script group files . the file is called MyJ2sPortlet.war

This is not a tutorial neither of swt gui applications, portlets, or client-server application. Here I will only show what is the best way, IMHO, of showing a swt shell as a portlet..

If you want to test this portlet you only need to download liferay portal. If you want to import this as an eclipse project and make your own swt portlet you will have to be familiarized with portlet development and java2script.

In the first place I developed a simple swt application using my favorite visual editor in eclipse. In gnome it looks like the following:

We want to render it as a portlet. In this solution, the resulting portlet (with red halo) will be:

In the following picture we use liferay freeform layout. Observe that the shell will expand accordingly to the portlet size:

deploying the portlet to liferay for testing

The .war file is a web application portlet archive ready for deploy in a portal application. For testing the .war portlet in liferay you must:

  1. download liferay-bundled-with-tomcat, for example liferay 4.4.1 bundled with tomcat 5.5 http://sourceforge.net/project/downloading.php?group_id=49260&filename=liferay-portal-tomcat-5.5-4.4.1.zip&a=34438561

  2. run bin/startup,

  3. login as test@liferay.com password “test”.

  4. copy myJ2sPortlet.war to /home/yourusername/liferay/deploy (or in the case of windows something like c:\Documents and settings\Users\yourusername\liferay\deploy).

  5. Wait until liferay console prints “22:43:54,308 INFO [PortletHotDeployListener:425] Portlets for myJ2sPortlet registered successfully”

  6. Click on “welcome test!” → add application → java2script → myj2sportlet and the portlet should be added to the page

In the 4 step is where we actually make the portlet hot deploy in liferay. Alternatively, you can install de portlet from inside the portal. Just open Admin->admin liferay portlet, click "plugins" tab, click "install more plugins", click "upload file" tab, and choose MyJ2sPortlet.war. Once this step is finnished, liferay console should print somethings like the following telling us the portlet was successfully deployed:

13:06:38,595 INFO [BaseDeployer:493] Deploying myJ2sPortlet.war
Expanding: /home/sebastian/beeblos4/deploy/myJ2sPortlet.war into /tmp/20090621130638598
Copying 1 file to /tmp/20090621130638598/WEB-INF
13:06:39,230 INFO [BaseDeployer:1132] Modifying Servlet 2.5 /tmp/20090621130638598/WEB-INF/web.xml
Copying 735 files to /home/sebastian/desarrollo/portal4/tomcat/webapps/myJ2sPortlet
Copied 76 empty directories to 3 empty directories under /home/sebastian/desarrollo/portal4/tomcat/webapps/myJ2sPortlet
Copying 1 file to /home/sebastian/desarrollo/portal4/tomcat/webapps/myJ2sPortlet
Deleting directory /tmp/20090621130638598
13:06:40,151 INFO [PortletAutoDeployListener:87] Portlets for /home/sebastian/beeblos4/deploy/myJ2sPortlet.war copied successfully
13:06:43,934 INFO [PluginPackageHotDeployListener:74] Reading plugin package for myJ2sPortlet
13:06:43,935 WARN [PluginPackageHotDeployListener:123] Plugin package on context myJ2sPortlet cannot be tracked because this WAR does not contain a liferay-plugin-package.xml file
13:06:44,506 INFO [PluginPackageHotDeployListener:187] Plugin package myJ2sPortlet/myJ2sPortlet/unknown/war registered successfully
13:06:44,511 INFO [PortletHotDeployListener:133] Registering portlets for myJ2sPortlet
13:06:44,600 INFO [PortletHotDeployListener:425] Portlets for myJ2sPortlet registered successfully

Note that the portlet is not deployed until liferay prints " Portlets for myJ2sPortlet registered successfully"

Steps I followed for creating the project

The .war file is a zip and contains both binary (.class and .js) and source code (java). in src/ directory you can find portlet and servlet code and in /html/myJ2s_j2sproject/src-j2s it is the swt shell java sources.

I recommend you to decompress it and import the project to eclipse. This is a “portlet project” and you can easily build, package and deploy on liferay via a simple ant script (configure build.properties according to your system).

In summary this web application serve the java2script content statically and once deployed it still can be accessed from outside the portal application , for example from http://localhost:8080/myJ2sPortlet/html/myJ2s_j2sproject/org.sgx.portlet.myJ2sPortlet.gui.Dialog1.html

For one side, we have an html and javascript files generated by java2script that will render a maximized swt shell:

dialogShell = new Shell(parent, SWT.NONE);

//.... initialize shell content......





Display display = dialogShell.getDisplay();

while (!dialogShell.isDisposed()) {

if (!display.readAndDispatch())



(see Dialog1.java)

in the other side, out portlet contents will be a maximized iframe which points to the static content. Portlet's View.jsp:

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<iframe height="100%" width="100%" src="/myJ2sPortlet/html/myJ2s_j2sproject/org.sgx.portlet.myJ2sPortlet.gui.Dialog1.html"/>

Having java2script application in a separate iframe (and thus in a separate dom) will prevent to have nasty javascript libraries incompatibilities. Also we took advantage of swt shell maximization for the portlet content size adjustment.

Hope this can be useful for people who wish to experiment with java2script applications inside portals.

No hay comentarios: