Sunday 9 February 2014

Custome XQuery in OSB.

Custom XQuery Functions in OSB 11g
-----------------------------------
Oracle Service  Bus 11g version, offers an extensible framework for writing your own
XQuery functions and allows to use them in the OSB Message Flows

You can also specify the scope of these custom XQuery functions and where these
functions should be available in OSB - for example in Pipeline and Split-Join or only
under Pipeline

Steps for implementing the custom functions

1. Create an XML file
2. Create a properties file(optional). Keys wrapped in % symbols in the XML file gets
    the values from this properties file
3. Create your custom Java class and package it as a jar file

I have implemented the XQuery custom functions and you can find the following
resource files

1. osbCustomFunctions.xml
2. osbCustomFunctions.properties
3. customXPathFunc.jar

These three resources should be placed under "%OSB_HOME%\config\xpath-functions"
for the OSB to recognize the XQuery functions.

For example, in Windows OSB installation,
files are copied to "C:\beaplatforms\11g\wls1033\osb11g\config\xpath-functions"

Remember that for a Clustered Installation spanning multiple hosts, you should copy
these files to the location on each host machine

1. osbCustomFunctions.xml
--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
 <xpf:category id="%OSB_CUSTOM_XQUERY_FUNCTIONS%">
  <xpf:function>
   <xpf:name>customConcat</xpf:name>
   <xpf:comment>%FUNCTION_CUSTOM_CONCAT_COMMENT%</xpf:comment>
   <xpf:namespaceURI>http://test.org/osb/custom/functions/CustomXpathFunc
   </xpf:namespaceURI>
   <xpf:className>org.test.custom.CustomXpathFunc</xpf:className>
   <xpf:method>java.lang.String customConcat(java.lang.String, java.lang.String)
   </xpf:method>
   <xpf:isDeterministic>false</xpf:isDeterministic>
   <xpf:scope>Pipeline</xpf:scope>
   <xpf:scope>SplitJoin</xpf:scope>
  </xpf:function>
 </xpf:category>
</xpf:xpathFunctions>

2. osbCustomFunctions.properties
--------------------------------
%OSB_CUSTOM_XQUERY_FUNCTIONS%=Test OSB XQuery Custom Functions
%FUNCTION_CUSTOM_CONCAT_COMMENT%=Returns the concatenated string of the two input
string arguments

3. customXPathFunc.jar
-----------------------
Here is a simple Java Class with one method.
Compile the Java class, and create JAR file (customXPathFunc.jar)
The method returns a concatenated string of the two input strings and
appends "from Amsterdam"

package org.test.custom;
public class CustomXpathFunc {
 public CustomXpathFunc() {
 // TODO Auto-generated constructor stub
 }
 public static java.lang.String customConcat(String inputOne, String inputTwo){
 return "You are " + inputOne+ " "+inputTwo+ "from Amsterdam";
 }
}

Testing in Oracle Service Bus- SBConsole
---------------------------------------
Copy all the above files to the location "%OSB_HOME%\config\xpath-functions" and
restart your OSB Server.

Log onto the sbconsole: http://localhost:7001/sbconsole
Create a Proxy Service Message flow, and choose to edit an XQuery Expression

In the XQuery expression editor, you should now see the Custom XQuery function
under the XQuery functions in the Proxy Service Message Flow - XQuery expression editor

OSB XQuery Expression Editor is shown here

sbconsole-xquery-customXpath

You can also test the XQuery function

sbconsole-testpage

Reference:
http://download.oracle.com/docs/cd/E17904_01/doc.1111/e15866/custom_xpath.htm