Using custom XPath functions

In this recipe, we will show how to implement custom XPath functions, which extends the collection of XPath functions available with the OSB platform. We will use the same functionality that we used in the Using the Java Callout action to invoke Java code recipe, but now make the calculate checksum functionality available as an XPath function.

Getting ready

You can import the OSB project containing the base setup for this recipe into Eclipse OEPE from chapter-9getting-readyusing-custom-xpath-function.

How to do it...

First we have to create the Java functionality we like to expose as a custom XPath function. We will reuse the same Java class we used in the Using the Java Callout action to invoke Java code recipe which is shown here:

package osbcookbook.util.checksum;

import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class ChecksumUtil {
  public static long calculateChecksum(String data) {
    Checksum checksum = new CRC32();
    checksum.update(data.getBytes(), 0, data.getBytes().length);
    return checksum.getValue();
  }
}

A JAR with this class is available in chapter-9getting-readymiscosb-checksum-util.jar.

Next, we need to configure the XPath function on the OSB server and map it to the calculateChecksum method of the ChecksumUtil class. The configuration for the XPath functions is held in the [WL_HOME]/Oracle_OSB1/config/xpath-functions. Perform the following steps in an Explorer window.

  1. Navigate to the [WL_HOME]/Oracle_OSB1/config/xpath-functions folder.
  2. Copy the osb-built-in.xml file and paste it as a new file called osb-cookbook.xml which will hold the definition of the custom XPath function.
  3. Open the osb-cookbook.xml file in an Editor.
  4. Remove all the existing <xpf:function> elements and add our function as shown in the following screenshots:
    How to do it...
  5. Save the file.

    Now we have to make the JAR available to the OSB Server:

  6. Copy osb-checksum-util.jar from the chapter-9getting-readymisc to the [WL_HOME]/Oracle_OSB1/config/xpath-functions folder.
  7. Restart both Eclipse OEPE and the OSB server to reload the configuration.

    Now let's use our new XPath function. In Eclipse OEPE, perform the following steps:

  8. Open the Checksum proxy service and navigate to the Message Flow tab.
  9. Insert a Replace action into CalculateChecksumStage.
  10. On the Properties tab of the Replace action, enter body into the In Variable field.
  11. Click <Expression> and select the XQuery Functions tab.
    How to do it...
  12. Replace the $arg-string in the parameter list by $body/text() and click OK.
    How to do it...
  13. Select the Replace node contents option.
  14. Deploy the project to the OSB server.

    Now let's test the proxy service by performing the following steps in the Service Bus console:

  15. In the Project Explorer navigate to proxy folder inside the using-custom-xpath-function project.
  16. Click on the Launch Test Console icon of the Checksum proxy service.
  17. Enter This is some text on which the Checksum will be calculated! into the Payload field and click Execute.
  18. The output in the Response Document section should show the same checksum value 4265366956 as in the recipe with the Java Callout action.

How it works...

We have seen how we can extend the set of XPath functions available on the OSB platform by custom XPath function. A custom XPath function is implemented by some Java code. In order to map the Java functionality to the custom XPath function, the Java code needs to be exposed as a public static method in a public Java class.

The Java method should not produce side-effects; for example, it should not update any databases or participate in a global transaction. Such code should be invoked by a Java Callout action, as shown in the Using the Java Callout action to invoke Java code recipe.

A custom function will only appear in Eclipse OEPE if the OSB can find the Java class. Such a custom XPath function can be used both in inline XQuery expression, as shown with the Replace, and in XQuery resources, just as any other functions provided by the Oracle Service Bus.

The isDeterministic property in the configuration specifies whether the function is deterministic or non-deterministic. Deterministic functions always provide the same results whereas non-deterministic functions return the unique results. The XQuery standard recommends that functions should be deterministic, so that the XQuery engine is able to perform optimizations.

There's more...

A custom XPath function can also be invoked in an XQuery resource, as shown in the following screenshot:

There's more...

Just drag the XPath function from the Expression Functions tree into the Expression window, as with any other built-in XPath function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset