Sunday 12 June 2011

Adding Custom Xpath function in BPEL

1. Introduction :
Below steps will provide the way to add custom xpath function in your BPEL Code. In the below example we will create a custom xpath function which will take a string as an input from the BPEL code and will return “string + ‘is good’” as an output.

2. Creating the java application

Create a java application which will implement IXPATHFunction class and perform the required string operation. Below is the source code :

package com.bpelsoa.xpath;

import com.collaxa.cube.xml.dom.DOMUtil;
import com.oracle.bpel.xml.xpath.IXPathContext;
import com.oracle.bpel.xml.xpath.IXPathFunction;
import com.oracle.bpel.xml.xpath.XPathFunctionException;
import java.util.List;
import org.w3c.dom.Node;

public class ApplicationCustomXpathPOC implements IXPathFunction {

    private static final int NB_ARGS = 1;
    private static final int INDEX_ARG = 0;


/* below function called by the BPEL engine. */
    public Object call(IXPathContext context,
                       List args) throws XPathFunctionException {
        if (args.size() == NB_ARGS) {
            Object o = args.get(INDEX_ARG);
            String str = getValue(o);
            return concatIsGood(str);
            //return "test";
        }
        throw new XPathFunctionException("This function requires one argument.");

    }

    private String getValue(Object o) throws XPathFunctionException {
        if (o instanceof String) {
            return ((String)o);
        } else if (o instanceof Node) {
            return DOMUtil.getTextValue((Node)o);
        } else {
            throw new XPathFunctionException("Unknown argument type.");
        }
    }

    private String concatIsGood(String str) {
       /* for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;*/
         //throw new javax.xml.xpath.XPathFunctionException("This function requires one argument.");
        
         String s= str + " is good";
         return s;
    }
}

3. Create the jar
Compile and create the jar from the java application following below steps…

i. Right click on the java project and click new…



ii. Go to deployment profiles and click on jar files…


iii. Give a proper deployment profile name (in the same name the jar will be created)



iv. Click ok on the next window…


iv.    The .deploy file will be created under the resource directory of the project…




iv.    Now right click on .deploy file and click deploy to jar file to create the jar.


Changes in xpath-function.xml
We need to add the new custom xpath function in the xpath-funtion.xml file which is under /aiaapp/oracle/product/10.1.3.1/OracleAS_1/bpel/system/config directory.

Below entry is made for the custom xpath function :

<function id="concatIsGood">
  <classname>com.bpelsoa.xpath.ApplicationCustomXpathPOC</classname>
  <comment><![CDATA[This implements a small tool which concats is good with any string given as an argument. The signature of this function is bpelsoa:concatIsGood(String).]]></comment>
  <property id="namespace-uri">
    <value>http://bpelsoa.blogspot.com</value>
    <comment>Namespace URI for this function</comment>
  </property>
  <property id="namespace-prefix">
    <value>custXpath</value>
    <comment>Namespace prefix for this function</comment>
  </property>
</function>

Put the jar in Server
Put the CustomXpathPOC.jar in server’s jar location (/aiaapp/oracle/product/10.1.3.1/OracleAS_1/bpel/libcox/) and make the corresponding entry in server.xml which is under /aiaapp/oracle/product/10.1.3.1/OracleAS_1/j2ee/oc4j_soa/config/. After this a restart the server.

Using your new custom xpath function in a BPEL process

A custom xpath function can be used as any xpath functions. It has to be in an expression field from an assign activity (copy, append, insert after/before, etc.).

Below is the code snippet…

<assign name="xpathCall">
      <copy>
        <from variable="inputVariable" part="payload"
              query="/client:POCForCustomXpathProcessRequest/client:input"/>
        <to variable="Str"/>
      </copy>
      <copy>
        <from expression="custXpath:concatIsGood(bpws:getVariableData('Str'))"/>
        <to variable="outputVariable" part="payload"
            query="/client:POCForCustomXpathProcessResponse/client:result"/>
      </copy>
    </assign>
 


No comments:

Post a Comment