Custom XPath Functions in OSB:
Wikipedia says “Extensibility is a system design principle where the implementation takes into consideration future growth.Extensions can be through the addition of new functionality or through modification of existing functionality”.
The extensibility is possible even in case of OSB and custom XPath functions is one of the ways to extend the out-of-the-box functionality provided with OSB. In this post, we talk about the procedure to come up with the custom XPath functions. Though the example taken in this post (division of numbers)does not represent the real-world case but sufficient enough to stress the point.
During the IDE and server start up, OSB checks for custom functions in the path $OSB_HOME/config/xpath-functions.
Creating the custom XPath functions involves coming up with an xml file and the java code that does the required task. The xml file is going to be similar to the file osb-built-in.xml provided in above path that contains OSB functions. So let us create a xml file custom-func-demo.xml with the below contents and place it the folder mentioned above.
<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
<xpf:category id="Custom Functions">
<xpf:function>
<xpf:name>DivideNumbers</xpf:name>
<xpf:comment>Function used for division of numbers</xpf:comment>
<xpf:namespaceURI>http://www.oracle.com/custom/custom-functions</xpf:namespaceURI>
<xpf:className>demo.DivideNumbers</xpf:className>
<xpf:method>java.lang.Double divide(java.lang.Double,java.lang.Double)</xpf:method>
<xpf:isDeterministic>true</xpf:isDeterministic>
<xpf:scope>Pipeline</xpf:scope>
<xpf:scope>SplitJoin</xpf:scope>
</xpf:function>
</xpf:category>
</xpf:xpathFunctions>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
<xpf:category id="Custom Functions">
<xpf:function>
<xpf:name>DivideNumbers</xpf:name>
<xpf:comment>Function used for division of numbers</xpf:comment>
<xpf:namespaceURI>http://www.oracle.com/custom/custom-functions</xpf:namespaceURI>
<xpf:className>demo.DivideNumbers</xpf:className>
<xpf:method>java.lang.Double divide(java.lang.Double,java.lang.Double)</xpf:method>
<xpf:isDeterministic>true</xpf:isDeterministic>
<xpf:scope>Pipeline</xpf:scope>
<xpf:scope>SplitJoin</xpf:scope>
</xpf:function>
</xpf:category>
</xpf:xpathFunctions>
The above XML fragment shows the custom XPath function name (Divide Numbers), Class Name, Java Method and the namespace that should be used to access the function in the message flow. ‘isDeterministic’ specifies whether the function is deterministic or non-deterministic. Deterministic functions always provide the same results where as Non-Deterministic functions return the unique results.
Create a simple java class with the following code. Make sure that class name (including the package) and method signature matches with the above xml contents. Create a jar of this and place it in the above mentioned location so that IDE and server can detect the custom function.
package demo;
import java.lang.Double;
public class DivideNumbers {
public static Double divide(Double a, Double b) {
return a/b;
}
}
public static Double divide(Double a, Double b) {
return a/b;
}
}
On restart of Eclipse IDE and OSB server, we should be able to see the custom XPath function that we just defined.
Use the custom XPath function and run the proxy service to see the expected results as shown below.
--------------------------------------------------------------------------------------------------------------------------------