Advertisements

Posts Tagged 'XQuery'

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>

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

On restart of Eclipse IDE and OSB server, we should be able to see the custom XPath function that we just defined.

           Xpath1

           Xpath4

           Xpath2

            Xpath3
Use the custom XPath function and run the proxy service to see the expected results as shown below.

            Xpath5

Advertisements

Using XQuery expression to get the Proxy Service Name

In the last post, we have seen that calling proxies send the proxy name as a payload to CommonPS as shown below.

<Payload>
    <ProxyName>PS_A</ProxyName>
    <AuditPayload>
        <Date>2011-05-23</Date>
        <Description>Audit Data</Description>
    </AuditPayload>
</Payload>

This approach has a problem that we have to manually change the ProxyName element in the payload every time we rename the proxy service. To overcome this problem we can use the XQuery expression $inbound/@name to get the proxy name. This would give the proxy name in the following format.

ProxyService$<fully qualified path of project separated by $>

So in PS_A, modify the assign activity that is present in the request pipeline as shown below.

               image

Using the expression $inbound/@name in the above payload will be converted into the following once it reaches the CommonPS.

<Payload>
    <ProxyName name="ProxyService$TestConfigProject$PS_A"/>   

<AuditPayload>
        <Date>2011-05-23</Date>
        <Description>Audit Data</Description>
    </AuditPayload>
</Payload>

This triggers a change in the message flow of CommonPS as well. So modify the assign activity which is shown below with a new expression to get the proxy name.

                 image

Modify the expression in this assign activity to the following.

                 fn:tokenize($body/Payload/ProxyName/@name, ‘\$’)[last()]

Now deploy both of the OSB projects(TestConfigProject and Project_A) and run PS_A and PS_B once by setting the Flag value as ‘Y’ and once with value ‘N’. Observe that the auditing is happening only when the Flag value is set to ‘Y’ as observed in earlier scenario.

Using XQuery Resource in OSB for Configurable Properties

We often see the following scenarios in any typical software application development or deployment and OSB is also not an exception from this perspective.

– There will be few things or parameters that have to be modified depending on the environment that we are deploying to.

– Provision for switching on and off of the some functionality based on the requirement. For example, a service that logs some information into database should be configured to switch on and off when required.

In these scenarios, We can leverage the XQuery resources in OSB to put all of our configurable properties. This post explains the following things to achieve this functionality:

         – Creating the XQuery Resource with configurable properties.

         – Using the XQuery Resource in Proxy Service Message Flow

Creating the XQuery Resource

Create any OSB project (TestConfigProject), right click on the TestConfigProject and select New –> XQuery Transformation as shown below.

                 image

Give the filename as ‘ConfigXML’ and click on Next.

                  image

Select anytype, add it and click on Next.

                  image

Again select anytype, add it and click on Finish.

                  image

Following screen will appear after clicking on the ‘Finish’ button.

                  image

Go to ‘Source’ tab on clicking and delete all the contents.  Assume that we want to specify the configurable property for each and every proxy service present in our OSB configuration project. So we could paste the following contents in our XQuery resource. This XML structure allows us to add as many properties we want for each proxy service.

<ProxyServiceList>
    <ProxyService name="PS_A">
        <Flag>Y</Flag>
    </ProxyService>
    <ProxyService name="PS_B">
        <Flag>N</Flag>
    </ProxyService>
</ProxyServiceList>

Using the XQuery Resource in OSB Proxy Service

Consider a scenario where the proxies PS_A and PS_B have to call some other proxy CommonFuncPS which does the actual activity of inserting some auditing information in database. And this functionality should be configurable so that we can enable/disable the auditing whenever required.

We can use the above XQuery resource for configuring this functionality. The flag value ’Ý’ indicates that the auditing should happen and the value ‘N’ indicates that auditing should not happen.

To achieve this, let’s create another proxy service CommonPS that checks for this configuration property value and calls CommonFuncPS based on the value given in the XQuery resource. The following screenshot shows all the proxy services that are involved in this exercise and let’s create all proxy services as ‘Any XML Service’ for simplicity.

                     image

CommonPS Message Flow

Since this proxy has to determine the calling proxy, all other proxy services has to send its name along with the payload that has to be inserted into database. So assume that following is the structure of the request message that we agreed on

<Payload>
    <ProxyName>PS_A</ProxyName>
    <AuditPayload>
        <Date>2011-05-23</Date>
        <Description>Audit Data</Description>
    </AuditPayload>
</Payload>

Go to the message flow of the proxy service and insert a pipelinepair Node and Stage and insert an assign activity. Click on the ’Éxpression’ in the properties and select XQuery resource as shown below.

                        image

Select the XQuery resource by clicking on Browse button as shown below.

                         image

Click on OK button after selecting ConfigXML.xq resource in the popup window and give the assign variable as varConfigPayload.

                       image

Create another assign activity to get the proxy name that is been passed in the input and use the expression as shown below and assign this value to a variable varProxyName.

                        image

We have a variable where the configuration information is stored for all the proxies and also we have a variable which gives the information about calling proxy. Now we need to check whether audit flag is enabled or not before calling the CommonFuncPS. So insert If-Then activity from Stage Actions –> Flow Control as shown below.

                          image

Enter the expression for if condition as shown below.

                         image

Now insert a service callout to call the CommonFuncPS and set the variables as shown below. Since this article is about the configurable properties we will not look at the actual message flow of CommonFuncPS.

                         image

Insert another assign activity in the request pipeline of the service callout and populate the variable varRequestPayload by extracting the auditing payload from the body variable as shown below:

                        image

We have done with the message flow of CommonPS and we will see the message flow of the calling proxies.

Calling Proxies (PS_A or PS_B) Message Flow

Calling proxies message flow is very simple which will have just service callout to CommonPS proxy service as shown below.

                      image

Insert an assign activity in the request pipeline to populate the request variable of above service callout and pass the actual proxy name for ProxyName element as shown below.

                    image

 

Now deploy both of these OSB projects and run PS_A and PS_B once by setting the Flag value as ‘Y’ and once with value ‘N’. Observe that the auditing is happening only when the Flag value is set to ‘Y’.

This way an XQuery resource can be used to define all the configurable properties for individual proxies existing in your OSB configuration project. The XML used in this demonstration is also extensible and number of properties can be added as and when required.


Advertisements

Pages

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 374 other followers

Enter your email address to follow this blog and receive notifications of new posts by email.

Advertisements