Wednesday, September 17, 2008

implementing custom XPATH BPEL function

In this blog I will explain the steps you need to follow to create a simple HelloWorld example of XPATH BPEL function.

As you know the implementation is having 2 parts -- java part and xml part. Lets start with the java part.

Create a java class which implements com.oracle.bpel.xml.xpath.IXPathFunction Interface.You can find this class and related xpath functions in orabpel.jar

In this program I have used org.w3c.dom.Element, Node, Text from the xmlparserv2.jar.These are basically used to parse the elements being passed in as arguments to the function. This utility method is taken from Reynolds blog on XPATH functions.


Java Code

package com.geo.extension;

import com.oracle.bpel.xml.xpath.IXPathContext;

import com.oracle.bpel.xml.xpath.XPathFunctionException;

import com.oracle.bpel.xml.xpath.IXPathFunction ;

import java.util.List;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.Text;

public class HelloExt implements IXPathFunction {

public HelloExt() {

}

public Object call(IXPathContext ixPathContext, List list) throws XPathFunctionException{

if (list.size() != 2) {

throw new XPathFunctionException("function needs 2 arguments");

}

// Get value of input parameters

Object p1 = list.get(0);

Object p2 = list.get(1);

String sp1 = getParamValue(p1);

String sp2 = getParamValue(p2);

return "hello world to you "+sp1+" Ur age is "+sp2;

}

private String getParamValue(Object param) {

String value;

if (param instanceof Element) {

Node n = (Node)param;

value = n.getFirstChild().getNodeValue();

} else if (param instanceof Text) {

Node n = (Node)param;

value = n.getNodeValue();

} else {

value = String.valueOf(param);

}

return value;

}

}

You need to have the 2 jars - orabpel.jar & xmlparserv2.jar in your classpath for compiling this java class.

>javac -classpath lib\orabpel.jar;lib\xmlparserv2.jar -d classes com\geo\extension\HelloExt.java


Here lib points to the location of these jars. –d classes points to the output folder where I will get my class file.Copy the content of the classes folder which will have the package folders as well as the class file and place it in BPEL classpath. Its easy to put into default classpath of BPEL ->$ ORACLE_HOME/bpel/system/classes. By this you have taken care of the java part. Now comes the xml part of plugging in the functionality.

Open xpath-functions.xml

Add this xml snippet

<function id="getHelloMessage" arity="2">

<classname>com.geo.extension.HelloExt</classname>

<comment>

<![CDATA[The signature of this function is <i>hw:getHelloMessage(name, age)</i>.

Returns: hello world message]]>

</comment>

<property id="namespace-uri">

<value&gt;http://helloworld/xpath</value>

<comment>Namespace URI for this function</comment>

</property>

<property id="namespace-prefix">

<value>hw</value>

<comment>Namespace prefix for this function</comment>

</property>

</function>

In my BPEL process I used a assign operation to invoke the function

<assign name="Assign_HW">

<copy><from expression="hw:getHelloMessage(bpws:getVariableData('inputVariable','payload','/client:XpathBpelProcessRequest/client:name'),bpws:getVariableData('inputVariable','payload','/client:XpathBpelProcessRequest/client:age'))"/>

<to variable="outputVariable" part="payload"

query="/client:XpathBpelProcessResponse/client:result"/>

</copy>

</assign>

When you compile the bpel process you will get a compilation error because the namespace-prefix and namespace-uri are not declared in the bpel process. Add the namespace- prefix and namespace-uri from your new function defined in xpath-functions.xml to the process tag of .bpel file

<process name="XpathBpel" targetNamespace="http://xmlns.oracle.com/XpathBpel"

……….

…….

xmlns:hw="http://helloworld/xpath"

>

Compile and deploy your bpel process. The output will be the return string of the call method defined in the function.

2 comments:

Anonymous said...

If it is more descriptive, it will be better.

Unknown said...

In this blog I hav explained all the steps for a working sample.. If u r looking for anything specific you can list it here ..will get bak to u at the earliest