Saturday, November 22, 2008

Custom XSL functions @ESB & BPEL

In one of my projects there was a requirement of assigning a unique Id from a database to keep track of the request in both ESB and BPEL. So the options we had in mind were either to use an XPath function or to use a XSL function. Compared to XPath function XSL looked an easier option. Today I will brief you on how to define and use a custom XSLT function in ESB & BPEL.

As usual with BPEL and ESB it has a java part and an xml(configuration) part.


I will start with java part


Create a class for ex: ‘com.geo.IdGenerator’. Write a static method say ‘getGlobalMessageId()’ which will return you the unique Id. For making the samle simple, I will use a static integer which is initialized to 0 and keeps incrementing by 1.


package com.geo;

public class IdGenerator{

static int id=0;

public static String getGlobalMessageId () {
return “”+id++;
}

}

 
Your java class is ready. Compile(using javac) and create a jar using jar utilty 
-> jar cf XSLService.jar com/geo/IdGenerator.class
 
Copy the jar to $ORACLE_HOME/j2ee/$OC4J_INSTANCE/applib 

Coming to the configuration part, first question that comes to your mind is how XSL file will access the class file?


You need to add the namespace

http://www.oracle.com/XSL/Transform/java/$package.className

in your XSLT where $package.className will be replaced by com.geo.IdGenerator in our example.


Next question is how the method name will be recognized?

In the xsl file use the method with namespace-prefix

<xsl:value-of select=" service: $method_name" />

where $method_name will be replaced by getGlobalMessageId () in our example


So finally your xsl function snippet will look like


<xsl ……………….

xmlns:service=”http://www.oracle.com/XSL/Transform/java/com.geo.IdGenerator

…./>

< ………………………

<messageId>

<xsl:value-of select=" service: getGlobalMessageId ()" />

<messageId>

………..

/>


Isnt it simple. :)

No comments: