With the Oracle Service Bus (OSB) it is possible to extend the functionality by using java callouts.
Java callouts are static operations implemented in Java and packaged in a jar file. The jar file and all dependencies have to be included into the OSB project. Then it is possible to select the operation in the java callout node.
Basic types like int and String are supported for the parameters and the return value of the callout (the full list and a good tutorial is available at Simple Java Callout Example). But much more interesting, because in OSB everything is about XML processing, is to use XML snippets for parameter and return value.
First we create a proxy service with a java callout.
Then we implement the java class for the java callout.
The static method called by the OSB:
1 2 3 4 5 6 |
<div><span style="color:#008080;">1</span> <span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">static</span><span style="color:#000000;"> XmlObject test(String name, </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> XmlObject inputXmlData) { </span><span style="color:#008080;">2</span> <span style="color:#000000;"> Element inputElement </span><span style="color:#000000;">=</span><span style="color:#000000;"> (Element) inputXmlData.getDomNode().getFirstChild(); </span><span style="color:#008080;">3</span> <span style="color:#000000;"> processInput(inputElement); </span><span style="color:#008080;">4</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> XmlObject result </span><span style="color:#000000;">=</span><span style="color:#000000;"> createOutput(); </span><span style="color:#008080;">5</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> result; </span><span style="color:#008080;">6</span> <span style="color:#000000;">}</span></div> |
We need a method processing the input XmlObject and doing something with the containing elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<div><span style="color:#008080;"> 1</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">static</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> processInput(Element inputElement) { </span><span style="color:#008080;"> 2</span> <span style="color:#000000;"> </span><span style="color:#008000;">//</span><span style="color:#008000;"> Iterate over input</span><span style="color:#008000;"> </span><span style="color:#008080;"> 3</span> <span style="color:#008000;"></span><span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> NodeList fieldsNodeList </span><span style="color:#000000;">=</span><span style="color:#000000;"> inputElement.getChildNodes(); </span><span style="color:#008080;"> 4</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">for</span><span style="color:#000000;"> (</span><span style="color:#0000FF;">int</span><span style="color:#000000;"> i </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">; i </span><span style="color:#000000;">&</span><span style="color:#000000;">lt; fieldsNodeList.getLength(); i</span><span style="color:#000000;">++</span><span style="color:#000000;">) { </span><span style="color:#008080;"> 5</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> Node fieldsNode </span><span style="color:#000000;">=</span><span style="color:#000000;"> fieldsNodeList.item(i); </span><span style="color:#008080;"> 6</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (fieldsNode.getNodeType() </span><span style="color:#000000;">==</span><span style="color:#000000;"> Node.ELEMENT_NODE) { </span><span style="color:#008080;"> 7</span> <span style="color:#000000;"> String nodeName </span><span style="color:#000000;">=</span><span style="color:#000000;"> fieldsNode.getLocalName(); </span><span style="color:#008080;"> 8</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (nodeName </span><span style="color:#000000;">==</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">) { </span><span style="color:#008080;"> 9</span> <span style="color:#000000;"> nodeName </span><span style="color:#000000;">=</span><span style="color:#000000;"> fieldsNode.getNodeName(); </span><span style="color:#008000;">//</span><span style="color:#008000;"> Depends on XML library</span><span style="color:#008000;"> </span><span style="color:#008080;">10</span> <span style="color:#008000;"></span><span style="color:#000000;"> } </span><span style="color:#008080;">11</span> <span style="color:#000000;"> </span><span style="color:#008000;">//</span><span style="color:#008000;"> Do something with it</span><span style="color:#008000;"> </span><span style="color:#008080;">12</span> <span style="color:#008000;"></span><span style="color:#000000;"> } </span><span style="color:#008080;">13</span> <span style="color:#000000;"> } </span><span style="color:#008080;">14</span> <span style="color:#000000;">}</span></div> |
For the return value we create a new XmlObject containing e single resultElement element. Inside this element the real return values are located.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<div><span style="color:#008080;"> 1</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">static</span><span style="color:#000000;"> XmlObject createOutput() { </span><span style="color:#008080;"> 2</span> <span style="color:#000000;"> </span><span style="color:#008000;">//</span><span style="color:#008000;"> Create XmlObject and wrapping element</span><span style="color:#008000;"> </span><span style="color:#008080;"> 3</span> <span style="color:#008000;"></span><span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> XmlObject result </span><span style="color:#000000;">=</span><span style="color:#000000;"> XmlObject.Factory.newInstance(); </span><span style="color:#008080;"> 4</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> Node resultDomNode </span><span style="color:#000000;">=</span><span style="color:#000000;"> result.getDomNode(); </span><span style="color:#008080;"> 5</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> Document resultDocument </span><span style="color:#000000;">=</span><span style="color:#000000;"> resultDomNode.getOwnerDocument(); </span><span style="color:#008080;"> 6</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> Element resultElement </span><span style="color:#000000;">=</span><span style="color:#000000;"> resultDocument.createElement(</span><span style="color:#000000;">"</span><span style="color:#000000;">resultElement</span><span style="color:#000000;">"</span><span style="color:#000000;">); </span><span style="color:#008080;"> 7</span> <span style="color:#000000;"> resultDomNode.appendChild(resultElement); </span><span style="color:#008080;"> 8</span> <span style="color:#000000;"> </span><span style="color:#008000;">//</span><span style="color:#008000;"> Create result nodes</span><span style="color:#008000;"> </span><span style="color:#008080;"> 9</span> <span style="color:#008000;"></span><span style="color:#000000;"> </span><span style="color:#0000FF;">for</span><span style="color:#000000;">(</span><span style="color:#0000FF;">int</span><span style="color:#000000;"> i</span><span style="color:#000000;">=</span><span style="color:#000000;">0</span><span style="color:#000000;">; i</span><span style="color:#000000;">&</span><span style="color:#000000;">lt;</span><span style="color:#000000;">10</span><span style="color:#000000;">; i</span><span style="color:#000000;">++</span><span style="color:#000000;">) { </span><span style="color:#008080;">10</span> <span style="color:#000000;"> String name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">test</span><span style="color:#000000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> i; </span><span style="color:#008080;">11</span> <span style="color:#000000;"> String value </span><span style="color:#000000;">=</span><span style="color:#000000;"> Integer.toString(i); </span><span style="color:#008080;">12</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">final</span><span style="color:#000000;"> Element element </span><span style="color:#000000;">=</span><span style="color:#000000;"> resultDocument.createElement(name); </span><span style="color:#008080;">13</span> <span style="color:#000000;"> resultElement.appendChild(element); </span><span style="color:#008080;">14</span> <span style="color:#000000;"> element.appendChild(resultDocument.createTextNode(value));; </span><span style="color:#008080;">15</span> <span style="color:#000000;"> } </span><span style="color:#008080;">16</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> result; </span><span style="color:#008080;">17</span> <span style="color:#000000;">}</span></div> |
After compiling this class to a jar file and adding the jar file, xmlbeans and stax-api to the OSB project we can select our implemented method inside the java callout.
Finally we can deploy the whole thing to OSB and test it with the debug window.
Bernhard Mähr @ OPITZ-CONSULTING published at http://thecattlecrew.wordpress.com/