Tuesday, June 16, 2009

JSF 2.0 AJAX overview

Many AJAX JSF frameworks are on the market - but the moment you start combining them you may get random results due to incompatibilities. JSF 2.0 will help with this! Specifying the AJAX API will help assemble the different approaches under one roof. The spec (JSR-314) defines an f:ajax tag as well as a javscript API together with an AJAX enhanced JSF lifecycle.

I helped implement one of the numerous AJAX JSF approaches for years (the fry:ajax tag from J4Fry) and during the last months this approach was integrated into Apache MyFaces 2.0 while adapting it's interfaces to the new spec. The spec covers a lot of the basic features in a generic and flexible way. Btw.: MyFaces 2.0 is on its way, we're currently gluing everything together and I'm hoping for an alpha by end of august.

The most basic part of an AJAX implementation is replacing parts of the document. These are the features the JSF 2.0 API provides:
  • execute - The components that will be processed on the server in a blank separated list. Only the components named within this parameter get their decode, validate and updateModel methods called during phase 2-4. There is an @all keyword you can use to process the entire component tree.
  • render - The components that will be rendered and replaced within the HTML page in a blank separated list.
  • onevent - A JS callback that is triggered with three different events:
    • begin - occurs immediately before the request is sent
    • complete - occurs after the AJAX request has completed but before DOM manipulation has taken place
    • success - occurs after DOM manipulation has taken place (only for successfull request, else see onerror)
  • onerror - A JS callback that is triggered when the server signalizes that an error has occured.
  • params - An object that may include additional parameters to include in the request.
There have been discussions on both the MyFaces and Mojarra mailing lists on the nature of the ids in the execute and render parameters. Are they HTML ids or component ids? Here's the truth: The f:ajax tag can take either component ids that resolve relative to the nearest naming container or HTML ids. There is an algorithm that tries both. The procedural interface jsf.ajax.request(source, event, {execute: ..., render: ...}) requires HTML ids. The f:ajax tag will resolve the component and generate a behaviour that is in fact a call to jsf.ajax.request.

f:ajax provides three more attributes that offer JSF specific enhancements:
  • disabled - Indicates whether the AJAX behavior script should not be rendered.
  • listener - A method binding to execute when the AJAX request is processed on the server.
  • immediate - same as the attribute you know from the standard JSF action sources.
For Tomahawk 2.0 we are preparing a t:ajax tag that will include a bunch of extra options. Implementation will happen in J4Fry JSF 2.0 with a fry:ajax tag (targeted for end of juli) which will be repackaged for Tomahawk 2.0 when it is ready. This is the list of extra options we want to support:
  • timout - How long to wait for the HTTP response before aborting the request.
  • delay - How long to wait before issuing a request (helpfull with onkeyup or mouse move events to avoid tons of requests).
  • queuesize - Number of requests to queue (discard the oldest when the queue is full and a new one arrives).
  • partial submit - The current spec requires submission of the entire form though only the elements named in the execute parameter are being processed. Setting partial submit (or pps) to true could reduce the submit volume to the actually processed values.
  • disable - HTML id's of the components to disable while the request is running (can be implemented via onevent).
  • loadingbar - HTML id of an img tag to make visible while the request is running (can also be implemented via onevent).
  • errorhandling for errors that occur within the javascript - can be done with the onerror parameter, but the spec doesn't mention this use.
Just in case you've made it up to here (which would only happen if you are really deep into JSF and AJAX technology) you will be keen to know whether JSF 2.0 AJAX will do an auto eval on incoming scripts. If you rerender a portion of your markup that contains a script then auto eval will execute the newly arrived scripts. The spec doesn't mention this issue, Mojarra 2.0 (in it's current beta state) doesn't do auto eval but MyFaces 2.0 will.

Thursday, June 4, 2009

HowTo integrate Apache Axis and WebBeans

IMO WebBeans becomes a pretty cool dependency injection framework for Java. It works in a type-safe way (greetz to Spring ;-)) by using annotations. It provides Interceptors, Decorators and Event-Processing as well.
On the other hand Axis is a powerful framework to implement WebServices.

If you want to use the WebBeans benefits in your Axis Services you have to integrate both technologies. Axis provides two simple ways to do this:
  1. with a custom MessageReceiver
  2. with a ServiceObjectSupplier
So lets integrate WebBeans into Axis.
I'm running on Tomcat 6.0.18 with Apache Axis2 1.4.1 and WebBeans 1.0.0. Preview1.

Create a WebBeans integration class

package org.j4fry.webbeans;
import javax.inject.manager.Manager;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.jboss.webbeans.CurrentManager;

public abstract class WebBeansIntegration {
public static Object createInstance(AxisService axisService) throws AxisFault {
Parameter parameterServiceClass = axisService.getParameter(Constants.SERVICE_CLASS);
Class serviceClass = null;
try {
serviceClass = Class.forName(parameterServiceClass.getValue().toString());
} catch (ClassNotFoundException e) {
throw new AxisFault("There's not valid serviceClass specified", "serviceClass");
}
Manager manager = CurrentManager.rootManager();
return manager.getInstanceByType(serviceClass);
}
}

Possibility 1: Adjust Axis to use this integration class via a custom Message Receiver

package org.j4fry.webbeans;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;

public class CustomMessageReceiver extends org.apache.axis2.rpc.receivers.RPCMessageReceiver {
@Override
protected Object makeNewServiceObject(MessageContext msgCtx) throws AxisFault {
return WebBeansIntegration.createInstance(msgCtx.getAxisService());
}
}

Possibility 2: Adjust Axis to use this integration class via ServiceObjectSupplier

package org.j4fry.webbeans;
import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;

public class WebBeansObjectSupplier implements ServiceObjectSupplier {
public Object getServiceObject(AxisService axisService) throws AxisFault {
return WebBeansIntegration.createInstance(axisService);
}
}

Tomcat Configuration - web.xml
Tomcat's web.xml have to reference the WebBeans Manager (of course you have to include the AxisServlet and the mapping information as well)

<resource-env-ref>
<description>WebBeans Manager</description>
<resource-env-ref-name>app/Manager</resource-env-ref-name>
<resource-env-ref-type>javax.inject.manager.Manager</resource-env-ref-type>
</resource-env-ref>

Axis Service Configuration - services.xml
The services.xml from your Axis-Service archive (.aar file)
If you choose possibility 1 you have to include your custom Message Receiver with the following tag inside the service-element:

<messageReceivers>
<messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="org.j4fry.webbeans.CustomMessageReceiver"/>
</messageReceivers>

If you choose possiblity 2 you have to add the "ServiceObjectSupplier" parameter to your service element like this

<parameter name="ServiceObjectSupplier">org.j4fry.webbeans.WebBeansObjectSupplier</parameter>

Use WebBeans Annotation inside your Service class
If you've finished the previous steps you can use the WebBeans Annotations to inject some resources in your service class.

Resources
WebBeans RI 1.0.0 Preview1 (thanks to JBoss): http://downloads.sourceforge.net/jboss/webbeans-1.0.0.PREVIEW1.zip
Apache Axis2 1.4.1: http://ws.apache.org/axis2/

Wednesday, May 20, 2009

Use Subversive and Putty together on Windows

1. Download Eclipse Ganymede
From http://www.eclipse.org/
2. Download Putty
From http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
3. Extract Putty
Extract the putty.zip under C:\Apps\putty\ for instance.
4. Install Subversive (Subversion Plugin) and SVN Connectors
Open Eclipse and select "Help"->"Software Updates..."
Click on "Available Software" Tab
You need these two update sites:
Subversive Plugin: http://download.eclipse.org/technology/subversive/0.7/update-site/
SVN Connectors: http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/
So add these two sites by clicking on "Add Site..." Button (two times)
Afterwards you see in your "Available Software" Panel the new two sites.
Expand the "Subversive Site" and select the following plugins:
Subversive SVN Team Provider Plugin
Now expand the "Subversive SVN Connectors Site" and select the following plugins:
Subversive SVN Connectors
After these steps click the "Install..." Button
If the installation is finished you have to restart Eclipse.
5. Configure PLINK
Add a new environment variable
Name: SVN_SSH
Value: C:\\Apps\\putty\\plink.exe -ssh -2 -A -l mySvnUser
You have to adjust the svn user.
Now start the PAGEANT and select your Private Key
6. Configure SVN Connectors
In Eclipse go to "Window"->"Preferences"
Select the "Team"->"SVN"
Select the "SVN Connector" Tab
Set the Option "SVN Connector" to "Native JavaHL (svn:1.6.0 (r36650) jni:0.9.0)"
Click the "Apply" Button
7. Add a new Repository Location
Go to "Window"->"Open Perspective"->"Other..." and select "SVN Repository Exploring"
Switch to the SVN Repository Perspective and click the Button "New Repository Location"
Now enter your SVN Repository (for example: svn+ssh://example.org/var/svn/myRepo)
Click the "Finish" Button.

There's one harm: Everytime when you use the Subversive Plugin via PLINK a DOS Prompt is opened by Windows :-(

Sunday, March 15, 2009

Fuzzy String Match in PostgreSQL with Debian

If you want to use FuzzyStringMatching in PostgreSQL you have to do the following steps.
Prerequisites: Debian 4.0 Etch (or higher), installed PostgreSQL 8.3, existing database in postgres

1. Install the postgresql-contrib package via apt

apt-get install postgresql-contrib

2. Switch to your postgres user

su - postgres

3. Open the psql interactive terminal with the following options database, user, password and the installation file for the fuzzy string functions

psql -d myDatabase -U myUser --password myPassword -h localhost -f /usr/share/postgresql/8.3/contrib/fuzzystrmatch.sql

Afterwards you can use the soundex, levenshtein, metaphone and double metaphone functions in your sql statements.
Offical documentation about the fuzzy string match functions: http://www.postgresql.org/docs/8.3/static/fuzzystrmatch.html

Tuesday, February 24, 2009

Comparing RIA Frameworks

If you are interested in RIA Frameworks you can have a look at that comparison http://sonymathew.blogspot.com/2009/02/comparing-ria-frameworks.html (Thanks to . ADF Oracle Faces RC you can find here, thanks to Juan Camilo Ruiz.
Unfortunately JavaFX as well as Microsoft Silverlight is not mentioned in that comparison. That's why you can find the "missing" JavaFX column here:


Platform/Technology: JavaFX/Java
User Experience: Modest Rich if you're familar with Java. Is exactly like a Desktop App. No Page refreshes, all data-access & rendering in the background. Includes also data-bindings from variables to GUI-widgets. JavaFX wraps some Swing components to use them in a JavaFX GUI. JavaFX 1.1 API
Browser Support: Support all browser. You can deploy JavaFX applications via an Java Applet, Java-WebStart (JNLP - Java Network Launching Protocol) or stand-alone applications on the desktop.
Throu Java Web Start a one-click installation is possible. Awesome feature: You drag an applet from the browser to your desktop.
UI Code: JavaFX Scripting and Java. Via JavaFX you can describe your GUI in a declarative way. JavaFX allows to bind variables to GUI-widgets (data-bindings) and provide a possibility to add triggers to variables.
You can develop the business logic in Java. There's a plugin for different Adobe Tools (e.g. Photoshop) for designing your GUI.
Access Remote Services & Data: You can invoke remote Java-Services via RMI.
Code Complexity Management: JavaFX files (JavaFX Script Programming Language), Java files, Organize into Class/Object hierarchies, Packages. It's statically typed and a compiled language.
Tool Support / Eclipse Integration: There's a JavaFX Plugin for Netbeans. The plugin in provides Code-Completion, Live-Preview of the GUI and so on. Download page
Refactoring & Code-Completion Support: All Java Refactoring & Code Completion with NebBeans.
JEE Integration: You can invoke EJBs like in any other stand-alone Java-Application.
Migration: You can use existing Backend-Services. Of course you can use existing any Java-Code inside a JavaFX-Application
Performance: Partial downloading of the JRE is one of the performances pros. So you can use JavaFX-Applications on clients which haven't got a full installed version of the JRE. Modest download-time to browser because of modularized JRE (Entire JRE: 14,5MB but you need in most cases only the Typical Applet version 4,6MB)
Static-Content (Externally Managed): You can style your widgehts with CSS
Requirements: You need JRE 1.6 Update 10
Search Engine Optimization: Not compatible.

Currently J4Fry is working on some Dojo-Tags for Facelets for RIA feelings in JSF applications.
The declarative Tag should ease the use of Dojo in JSF Applications.
There'll be news about J4Fry-DojoFaces-Project when there's a beta version for testing.