Sunday, 25 September 2011

Creating and Using Custom XPath Functions

While designing BPEL Processes we run into situations where the Standard Xpath functions available to the BPEL Process just won’t come handy. The typical example of this is to insert a record into Data base for a Custom Function and this article describes creating, configuring, installing and using your own custom Xpath Function.
Custom Xpath Functions are pretty much more or less like a java code. Custom Functions can be resembled to a library of methods that you create and make available during process runtime to BPEL
Objectives
- Implement a custom function in java.
- Add the custom function java code to the server and make it available to BPEL.
- Configure Xpath-functions.xml.
- Implement custom Xpath function in BPEL Designer.
- Restart Oracle Application Server.
Let us go through step by step and achieve implementing custom Xpath function in BPEL
Implement a custom function in java
Write the java code logic as per the requirement, sample java code is here

/**
* Copyright 2008, My Company Limited All Rights Reserved.
*
* FILE : customXpathFuncImpl.java
* Description : Class used in Creating Core Logic for Custom XPath Function used in BPEL
* company : My Company Limited
* Author : anvv sharma
* Date : 04-Dec-2008
**/

package com.myCompany.eai.util;

// import packages

public class customXpathFuncImpl {

/**
* @param param1
* @param Param2
* @return String
*/

public static synchronized String customXpathFunction(String param1, String Param2) {

// Core logic to implement Custom Xpath
// Scenario to Insert a Record in Data base

try {
// establish a DB Connection
}

catch (Exception e) {

} finally {
}

return customXpathVariable;
}

}

Now create another which can called as Extension Function (creating this is optional, if you wish to merge this code in the above java file, but highly recommended)

/**
* Copyright 2008, My Company Limited All Rights Reserved.
*
* FILE : OrderCacheExtFunction.java
* Description : Class used in Creating Custom XPath Function used in BPEL
* company : My Company Limited
* Author : anvv sharma
* Date : 04-Dec-2008
**/

package com.myCompany.eai.util;

// import packages

public class CustomXpathTutorialExtFunc {
public CustomXpathTutorialExtFunc() {
}

public static class customXpathFunction {
public customXpathFunction() {
}

/**
* @param iXPathContext
* @param list
* @return
* @throws XPathFunctionException
* @throws NamingException
* @throws SQLException
*/
public synchronized Object call(IXPathContext iXPathContext, List list) throws XPathFunctionException,
NamingException, SQLException {
try {
if (list.size() != 2)
throw new XPathFunctionException("Invalid Number of Arguments required 2 arguments");

Object p1 = list.get(0); //param1
Object p2 = list.get(1); //param2

String sp1 = getParamValue(p1); //param1
String sp2 = getParamValue(p2); //param2

return customXpathFuncImpl.customXpathFunction(sp1, sp2);

} catch (XPathFunctionException e) {
throw new XPathFunctionException("Failed to initialise DatatypeFactory", e);
}
}

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;
}
}
}

The purpose of creating above java file will be discussed in step 3 & 4
Add the custom function java code to the server and make it available to BPEL
Copy the compiled class files to the server at the specified location <BPEL_HOME>/bpel/system/classes, make sure a folder by name ‘com’ already exists.
Configure Xpath-functions.xml

- Edit and append xpath-functions.xml file from the following location /bpel/system/config
- Take a backup of this file before modifying.
- Make sure you append the following lines in xpath-functions.xml file between
Edit and append xpath-functions.xml file from the following location <BPEL_HOME>/bpel/system/config
Take a backup of this file before modifying.
Make sure you append the following lines in xpath-functions.xml file between
<bpel-xpath-functions></bpel-xpath-functions>

<function id="customXpathFunction" arity="2">  <classname>com.myCompany.eai.util.CustomXpathTutorialExtFunc$customXpathFunction</classname>
  <comment>
    <![CDATA[The signature of this function is <i>util:customXpathFunction(String param1, String param2)</i>.
The arguments to the function:
<ol type="2">
<li>param1 - Description (Required)</li>
<li>param2 - Description (Required)</li>
</ol>
Returns: an XML strValue formatted string]]>
  </comment>
  <property id="namespace-uri">
    <value>http://www.oracle.com/XSL/Transform/java/com.myCompany.eai.util.customXpathFuncImpl</value>
    <comment>Namespace URI for this function</comment>
  </property>
  <property id="namespace-prefix">
    <value>util</value>
    <comment>Namespace prefix for this function</comment>
  </property>
</function>

Do not disturb the existing xpath function already defined.
If something goes wrong in this file, the server will not startup properly.

Now we discussion about the additional java file we created ‘Extension Function’.
Suppose we have an scenario were we need to add another function, [say customXpathFunctionAddln, which updates a record in DB] apart from the existing function [customXpathFunction, inserts a record in DB]
We need to create another java file by name customXpathFunctionAddln with a new namespace in xpath-functions.xml with an additional entry for customXpathFunctionAddln similar to customXpathFunction entry with a new namespace, say ‘util1’
Implement custom Xpath function in BPEL Designer.
Create a BPEL Process and implement the Custom Xpath Function in Transformation File.

<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper null?>
<stylesheet version="1.0"
            xmlns:util="http://www.oracle.com/XSL/Transform/java/com.myCompany.eai.util.customXpathFuncImpl">
  <template match="/">
    <orderRequest>
      <orderLineItem>
        <CRMOLIId>
          <value-of select='util:customXpathFunction(/xsdLocal:param1, xsdLocal:param2)'/>
        </CRMOLIId>
      </orderLineItem>
    </orderRequest>
  </template>
</stylesheet>

Restart Oracle Application Server
Restart the Oracle Server and related proc ’s using opmnctl command line option
/opmn/bin > ./opmnctl status
> ./opmnctl stopall
> ./opmnctl startall

1 comment:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Oracle 11g Fusion Java Programming, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Oracle 11g Fusion Java Programming. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    MaxMunus
    E-mail: nitesh@maxmunus.com
    Skype id: nitesh_maxmunus
    Ph:(+91) 8553912023
    http://www.maxmunus.com/



    ReplyDelete