Defining and importing the user-defined functions

In this recipe, we will illustrate how to define the custom functions in Java and how to import them into JDeveloper for the XSLT mapper.

How to do it…

The following text will cover the steps needed to define the custom functions and import them into JDeveloper:

  1. We start by opening an empty Java project in JDeveloper.
  2. In the Java project, we create the Java class (ValueWithUnit.java) that is used as a placeholder for the function logic. What we do is concatenate the value of the field with its corresponding unit as follows:
    public class ValueWithUnit {
      public static String formatValueWithUnit(String value, String unit) {
        return value + " " + unit;
        }
      }
  3. Next, we prepare the configuration file. We create a new directory named xml in the project and prepare the XML file named ext-mapper-xpath-functions-config.xml.
    How to do it…
  4. In the configuration file, we define the function name, class name, and input and output parameters. We also add the description of the function.
    <?xml version = "1.0" encoding = "utf-8" ?>
    <soa-xpath-functions xmlns = "http://xmlns.oracle.com/soa/config/xpath" xmlns:ud = "http://www.oracle.com/XSL/Transform/java/org.packt.user.defined.ValueWithUnit">
      <function name = "ud:formatValueWithUnit">
      <className>org.packt.user.defined.ValueWithUnit</className>
        <return type = "string"/>
        <params>
          <param name = "value" type = "string"/>
          <param name = "unit" type = "string"/>
        </params>
        <desc/>
        <detail>
          <![CDATA[Function concatenates the value and unit.]]>
        </detail>
      </function>
    </soa-xpath-functions>
  5. We are now ready to pack the class file along with the configuration file into the JAR archive for the deployment. For that purpose, we create a new deployment profile for the JAR archive as shown in the following screenshot:
    How to do it…
  6. In the deployment profile dialog, we enter UserDefinedFunction as the name of the deployment profile and click on the OK button.
  7. We get the dialog to configure the deployment profile. From the tree, we select Filters from Project Output and deselect the xml part from the Files tab as shown in the following screenshot:
    How to do it…
  8. In the file group, we create a new group and name it config. We define the packaging type of the file group. To configure the file group, we perform the following actions on the following tabs:
    • config: In the target directory, we enter META-INF
    • Filters: In this tab, we deselect everything except for the xml directory
  9. We confirm the deployment profile configuration by clicking on the OK button.

Now we are ready to build the package and deploy it for usage in JDeveloper. At the project root, we right-click and select Deploy and UserDefined Functions…. The deployment action wizard appears and we confirm the dialog by clicking on Finish. We find the created JAR file in the deploy directory of the project.

We install the extension to JDeveloper by selecting the Tools menu and then selecting Preferences.

Under the SOA tab, we select the generated JAR file and click on the OK button as shown in the following screenshot:

How to do it…

Tip

In order to start using the user-defined functions in BPEL processes, we need to restart JDeveloper.

How it works…

The user-defined functions are composed of the Java class and the XML configuration file. The composition packed into the JAR file can be used as an extension function in JDeveloper and Oracle SOA Suite.

Depending on where we want to use the user-defined functions, we define the XML configuration file. The structure of the XML configuration is the same; however, the names differ depending on the area we want to use the functions in as follows:

Purpose of usage

XML configuration file name

Oracle BPEL engine

ext-bpel-xpath-functions-config.xml

Oracle Mediator

ext-mediator-xpath-functions-config.xml

XSLT Mapper

ext-mapper-xpath-functions-config.xml

Human workflow

ext-wf-xpath-functions-config.xml

All of above

ext-soa-xpath-functions-config.xml

The configuration file we used in this recipe is highlighted in the preceding table.

There's more…

After the restart, we can see that our user-defined function appears in JDeveloper. It can be picked up from the Component Palette and User Defined Extension Functions tab.

There's more…

Using the user-defined function

Next, we will show you how to use the user-defined function in JDeveloper:

  1. In JDeveloper, we open the XSLT mapper. From the Component Palette tab, we pick the user-defined function and put it on the mapper pane.
  2. By right-clicking on the new function and selecting Properties, we can enter the desired data as shown in the following screenshot:
    Using the user-defined function
  3. An excerpt of the code from the transformation file is as follows:
    <client:milleage>
      <xsl:value-of select = "ud:formatValueWithUnit(/ns0:TempElement/ns0:pieces[3], /ns0:TempElement/ns0:pieces[4])"/>
    </client:milleage>
  4. We see that our function will be called with two parameters to be concatenated.

Migrating the user-defined functions to the Oracle SOA server

Oracle SOA Suite provides a placeholder where we put the extensions. We can put the extension in place by executing the following steps:

  1. Open the command prompt.
  2. Change the directory to <Oracle_SOA_home>Oracle_SOA1soamodulesoracle.soa.ext_11.1.1 and copy the extension JAR file into this directory.
  3. We execute the ant command to rebuild the extension registry.
    C:ProgramsOracleMiddlewareOracle_SOA1soamodulesoracle.soa.ext_11.1.1>ant
    Buildfile: build.xml
    create-manifest-jar: [echo] Creating oracle.soa.ext at C:ProgramsOracleMiddlewareOracle_SOA1soamodulesoracle.soa.ext_11.1.1/oracle.soa.ext.jar;
    C:ProgramsOracleMiddlewareOracle_SOA1soamodulesoracle.soa.ext_11.1.1UserDefinedFunctions.jar;
    C:ProgramsOracleMiddlewareOracle_SOA1soamodulesoracle.soa.ext_11.1.1classes [jar] Updating jar;
    C:ProgramsOracleMiddlewareOracle_SOA1soamodulesoracle.soa.ext_11.1.1oracle.soa.ext.jar
    BUILD SUCCESSFUL
    Total time: 0 seconds
    C:ProgramsOracleMiddlewareOracle_SOA1soamodulesoracle.soa.ext_11.1.1>
  4. Finally, we start Oracle SOA Suite.
..................Content has been hidden....................

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