Thursday, March 04, 2010

Understanding XPATH in SOA Suite

we often use the XPath variables predefined in the BPEL server.But we never tried to find out how does it work and where it is stored.

Just log in to you BPELAdmin console.

http://localhost:port/BPELAdmin

and switch to XPath Library tab you will find all the XPATH with their properties.

Now lets suppose we want to add our own custom XPATH how we will do it.


We will start from the begining to check how to design your own custom path and use it in your BPEL process.

Create a new application in jdeveloper.Select the application and create a new BPEL process project .Lets make this BPEL project a asynchronous BPEL process project and provide it some logical name.



Now just select the Project you have created.
Right click on it and say new a new window will come up there in you have to select java class as below.



Now provide some name to the java class and let other settings be the default settings



Now in the code section provide the following code.Any java code for XPATH has to implement the IXPathFunction interface.

It is an Interface for implementing custom xpath function in SOA components.

Implementations of Function are functions which are used to evaluate a function-call within an XPath expression.

Any java code implementing this method must be calling this function

call(IXPathContext context, java.util.List args)

Here are the details of this function

call

java.lang.Object call(IXPathContext context,
java.util.List args)
throws XPathFunctionException

Parameters:
context - The context at the point in the expression when the function is called.
args - List of arguments provided during the call of the function.
Returns:The result of the XPath function.
Throws:XPathFunctionException



I am not very gud with java but you can just get an idea by luking in to this program that we have implemented the function and we are getting some input from console and we are appending it with "Arpit Rahi " as an author.


package BusinessProcess;

import java.util.List;

import org.w3c.dom.Node;


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

public class Author implements IXPathFunction
{

public Object call(IXPathContext context,
List args) throws XPathFunctionException
{
if (args.size() == 1)
{
if (args.get(0) instanceof String)
{
return getAuthor((String)args.get(0));
}
else if (args.get(0) instanceof Node)
{
Node n = (Node) args.get(0);
return getAuthor(n.getNodeValue());
}
}
throw new XPathFunctionException( "reverseString() requires one string arugment." );
}

public String getAuthor(String str)
{
StringBuffer sb = new StringBuffer(str);
return (sb.toString()+ " " +"Arpit Rahi");
}

}


So once the code is written you will find some issues with the code saying

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

are not found.This is because we have not added the jar files required for these packages.Just select the project and right click on it and select project properties




A new window will come up select libraries in the left panel.





Now click on add libray and select three jar files from there

ADF Device Repository
JSP runtime and
BPM workflow





so after adding all these jars to your project it should show the list of all the jars like this.



Once you will complete this you will find all the errors are gone so you are now done with your java code.

now just compile your java project.

Now we will make an entry to the Xpath-fucntions.xml

Go to the following location SOA_HOME\bpel\system\config and open xpath-functions.xml in wordpad,there you need to add the following entry.

<function id="getAuthor">
<classname>BusinessProcess.Author</classname>
<comment>
<![CDATA[The character sequence contained in the specified string is replaced by the string and Author.. The signature of this function is <i>demo:getAuthor(string)</i>.]]>
</comment>
<property id="namespace-uri">
<value>http://arpit/rahi/DemoXpathFunction</value>
<comment>Namespace URI for this function</comment>
</property>
<property id="namespace-prefix">
<value>demo</value>
<comment>Namespace prefix for this function</comment>
</property>
</function>


Now here we have a lot of things to undestand.First of all



here we define the method name that we have created in our java code.

BusinessProcess.Author

The class name should be given as package name.name of java programme.

http://arpit/rahi/DemoXpathFunction

The namespace is nothing but a identifier to let the Xml knows it belongs to this group.



demo

the prefix that you want to use in your BPEL code for calling this function.

as we have in general bpws:getVarialbe same way we will be calling our custom defined XPATH as demo:getAuthor.

We will check this while designing the assign activity in BPEL process.

Make all these changes ,Now there is one more important thing.

as you can see we have provided the class name as BusinessProcess.Author

This mean we should be having the Author.class file within

SOA_HOME\bpel\system\classes\BusinessProcess directory.

Make a folder called BusinessProcess and paste the Author.class file in there.Restart the server and if you will now log in to bpel admin console and go to xpath library you will find a entry for the Author.

Now we will just check how to use this custom xpath in our bpel source.

Drag and drop a assign activity in your BPEL process created earlier between the receiveInput and callbackClient.

Double click the assign acitivity and create a copy operation.

Choose expression and provide

demo:getAuthor(String(bpws:getVariableData('inputVariable','payload','/client:DemoXPathProcessRequest/client:input')))




in the from copy operation to the output variable in the to operation.



Save you project and deploy it.Now invoke the process and provide some input

you will find the output appended with the author name.

You might found issue like

XPath expression failed to execute.
Error while processing xpath expression, the expression is "demo:getAuthor(bpws:getVariableData('inputVariable','payload','/client:XpathDemoProcessRequest/client:input'))", the reason is FOTY0001: type error.
Please verify the xpath query.

Because the input is not converted to string.

or you might get some error like

"demo:getAuthor(bpws:getVariableData('inputVariable','payload','/client:XpathDemoProcessRequest/client:input'))", the reason is Extension function namespace
should start with 'http://www.oracle.com/XSL/Transform/java/'..

which means that you have not extended the IXpathfunction interface.

So just follow each and every steps as mentioned in this document and let me know if some issues occur.

No comments: