First Steps with Apache WSIF – Calling out to WSIF-alized SOAP WebServices and Java Classes

4

SOA, WebService and web service, WSDL, UDDI, BPEL and WSIF, Axis, SOAP, JAVA-RPC etc. etc. If Google has not found this blog by now… Seriously, these are all hot topics. Integration, service publication, orchestration – we are bombarded with all of these. For a workshop we will shortly organize at AMIS, UI have attempted to create some simple introductions to some of these concepts. One of them is WSIF and using it for decoupling Service providers and their implementation from the service consumers. Not by implementing all Services as SOAP WebServices, as you might expect, but by describing the Services using WSDL – even if they are not WebServices in the strict SOAP sense of the word – and having WSIF figure out how to make the call.

In this article I will describe the first steps you could make with Apache WISF to call Java based services – a fancy expression for some public static Java Methods – as if they were Web Services. The Web Service Invocation Framework (WSIF) allows us to call semi-WebServices from our Java application. These services are described through a WSDL but not asssociated with a SOAP binding.....

WSIF is an open-ended framework for invoking WSDL described services. As long as a WSDL description of the endpoint exists, it should be possible to use this framework to invoke that piece of software, whatever it may be. WSIF, an Apache technology originally developed by IBM alphaWorks as a part of its Web Services Toolkit, extends the Web services model by allowing you to describe services in WSDL, even if it’s not a Web service that communicates through SOAP. WSIF also allows you to map such a service to the actual implementation and protocol. In other words, you can bind the abstract description of any WebService-like service to a corresponding resource, which can communicate using one of the supported WSIF providers. WSIF supports WSDL descriptions for services offered by Java classes, EJB, JCA, HTTP GET and POST, and sockets. Oracle BPEL for example has extended the WSIF framework with a provider for PL/SQL procedure calls.

Why use WSIF 

Enterprise information systems usually consist of many different software pieces, such as legacy applications accessible though JCA, EJBs, Web services developed on different platforms, and so on. To integrate all these pieces you have to deal with different protocols. For example, if software migrates to a different server or has been upgraded to use a new technology, you have to upgrade the integration code. Or you expose all these services – the legacy APIs – as SOAP based WebServices. Then the Service Consumers only need to know how to invoke a SOAP WebService and you are in business. Writing a new application requires only SOAP knowledge and of course the implementation of the WebServices for all legacy services —unless you use WSIF.

WSIF allows many of these legacy services to be invoked by service consumers as if they were WebServices without them actually having been implemented as such. If you tell the WSIF framework through a somewhat extended WSDL document what the service looks like in functional terms and how it can be invoked in terms of protocol and physical location, the WSIF framework can translate a simple service call from the service consumer into the required native protocol call; it also transforms the service response back into a generic response format. So the service consumer needs to knowledge of the actual implementation of the service; the consumer does not need to know how to communicate with a SOAP WebService, nor to a JMS, an EJB or a JCA service. That is all taken care of by the WSIF framework and its protocol specific providers.

If now the service is moved to another physical location, only the WSDL needs to be changed, not the service consumer itself. If the service is implemented in a SOAP WebService instead of the former EJB implementation, only the WSDL needs to be changed and again not the service consumer. And we do not have to do anything on the service side either: we do not have to implemement a SOAP WebService frontend for each service.

WSIF offers other important benefits:

  • Invoking services through WSIF maintains the performance of native protocols. Thus, when invoking Java resources, native Java classes, EJBs, or any other resources, you do not have to pay the performance penalty of (SOAP) Web services.
  • WSIF enables automatic propagation of transactional contexts between invoked transaction-aware Java resources using Java Transaction API (JTA). That way Java resources can participate in distributed transactions.

WSIF is also used by WebService oriented frameworks like BPEL containers for calling local Java services: the BPEL process calls a partnerLink whose WSDL is in fact an extended WSIF WSDL with special bindings, for Java for example. The Oracle BPEL Process Manager knows how to invoke such a WebService (-like service), using the WSIF instead of a normal SOAP communication.

WSIF is also used for easy testing of the WSDL and implementation of a service that is to be deployed as a "real" SOAP web service (without the actual overhead of the SOAP container). If you have a piece of Java that you are going to publish as SOAP WebService, you can test the WSDL and the invocation of that Java functionality through WSIF without having to set up a SOAP container and actually deploying the WebService.

In theory, the WSIF framework can easily be extended with additional Providers that could take care of calls to services implemented in other technology/protocol combinations than the ones currently supported in the Apache WSIF distribution – SOAP, EJB, Java, JMS and JCA. I have not been able to locate (m)any additional providers and at first glance the instruction for creating your own provider did not strike as easy. However, it seems an entertaining challenge for a long winter night sometime.

Creating a WSIF client consuming a SOAP WebService

We will embark on our WSIF tour in the most obvious way: calling regular SOAP WebServices. For this example I will make use of the OTN GUIDService (see OTN’s Hosted Services). This WebService Generates an identifier that is guaranteed to be unique, universally. It is the simplest WebService one can imagine: no input parameters and a single string as output.

  1. I start by downloading the Apache WSIF release 2.0 . I extract the zip onto my local hard drive.
  2. Retrieve the WSDL document for this WebService and store it on my file system as OTNGUIDGenerator.wsdl. The WSDL is at: http://webservices.oracle.com/ws/guid/oracle.ws.OTNGUIDGenerator?WSDL
  3. Next, I take the StockQuote example from the Apache Samples directory (WSIF_HOME\samples\clients\stockquote\main.java) to get inspired by and nick some code from
  4. Now it’s time to fire up my IDE (well, you do not really need it, but I am a lazy type of guy). I have used both Eclipse 3.1 and JDeveloper 10.1.3 for this exercise. I create a new project, add the OTNGUIDGenerator.wsdl file. I also add all libraries in WSIF_HOME\lib and WSIF_HOME\build\lib to the project’s Classpath – just to be sure.
  5. I create a new Java class – nl.amis.identity.IdentityManager. It provides a public static method getIdentity() that takes no input and returns a unique String. It will make use of the Oracle GUIDWebService – if that one is available.
  6. The getIdentity() method calls a private method getGUIDResult() that in turn uses the WSIF framework to make the call to the SOAP based WebService. It is very important to note that this method does not refer in any way to SOAP specific aspects, neither the URL nor aspects of the SOAP:Binding element. The method looks like this:
    private static String getGUIDResult(String portName) {<br />        String result = &quot;&quot;;<br />        try {<br />            // create the ServiceFactory<br />            WSIFServiceFactory factory = WSIFServiceFactory.newInstance();<br />            // create a Service based on the Service and PortType combination <br />            // in the specified WSDL<br />            WSIFService service =<br />                factory.getService(&quot;OTNGUIDGenerator.wsdl&quot;<br />                                  , null, null<br />                                  , &quot;http://webservices.oracle.com/wstns/oracle/ws/OTNGUIDGenerator&quot;  // portType namespace<br />                                  , &quot;OTNGUIDGeneratorPortType&quot;  // portType <br />                                  );<br />            WSIFPort port = service.getPort();<br />            // Create the operation object for the getGUID operation in the WSDL<br />            WSIFOperation operation =<br />                port.createOperation(&quot;getGUID&quot;, &quot;getGUID0Request&quot;,&quot;getGUID0Response&quot;);<br /><br />            WSIFMessage input = operation.createInputMessage();<br />            WSIFMessage output = operation.createOutputMessage();<br />            WSIFMessage fault = operation.createFaultMessage();<br />            // invoke the Service<br />            boolean success =<br />                operation.executeRequestResponseOperation(input, output, fault);<br />            String guidResult = (String)output.getObjectPart(&quot;return&quot;);<br />            if (success == true) {<br />                result = guidResult;<br />            } else {<br />                System.out.println(&quot;No Result..Unable to execute the Operation!!&quot;);<br />            }<br /><br />        } catch (WSIFException we) {<br />            System.out.println(&quot;Got exception from WSIF, details:&quot;);<br />            // we.printStackTrace();<br />        } catch (Exception e) {<br />            e.printStackTrace();<br />        }<br />        return result;<br />    }<br />

This method asks WSIF to call a Service – which happens to be a SOAP WebService. It refers to the WSDL document that defines a single service that contains a PortType called OTNGUIDGeneratorPortType. One of the operations on this PortType (in fact the only one) is called getGUID in the WSDL. We tell WSIF to create the inputMessage (which is empty) and initialize the output and fault (exception) parts. Next the service request is made – that is: this method calls upon WSIF to call the specified operation on the indicated port type on the current service. WSIF interprets that information in the context of the only port it can find in the WSDL: a SOAP Port linked to the URL http://webservices.oracle.com/ws/guid/oracle.ws.OTNGUIDGenerator.

The WSDL document looks as follows:

&lt;definitions<br />     name=&quot;OTNGUIDGenerator&quot;<br />     targetNamespace=&quot;http://webservices.oracle.com/wstns/oracle/ws/OTNGUIDGenerator&quot;<br />     xmlns=&quot;http://schemas.xmlsoap.org/wsdl/&quot;<br />     xmlns:tns=&quot;http://webservices.oracle.com/wstns/oracle/ws/OTNGUIDGenerator&quot;<br />     xmlns:ns1=&quot;http://webservices.oracle.com/wstns/oracle/ws/OTNGUIDGenerator/schema&quot;<br />&gt;<br />    &lt;types&gt;<br />        &lt;schema targetNamespace=&quot;http://webservices.oracle.com/wstns/oracle/ws/OTNGUIDGenerator/schema&quot;<br />             xmlns=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;/&gt;<br />    &lt;/types&gt;<br />    &lt;message name=&quot;getGUID0Request&quot;/&gt;<br />    &lt;message name=&quot;getGUID0Response&quot;&gt;<br />        &lt;part name=&quot;return&quot; type=&quot;xsd:string&quot;/&gt;<br />    &lt;/message&gt;<br />    &lt;portType name=&quot;OTNGUIDGeneratorPortType&quot;&gt;<br />        &lt;operation name=&quot;getGUID&quot;&gt;<br />            &lt;input name=&quot;getGUID0Request&quot; message=&quot;tns:getGUID0Request&quot;/&gt;<br />            &lt;output name=&quot;getGUID0Response&quot; message=&quot;tns:getGUID0Response&quot;/&gt;<br />        &lt;/operation&gt;<br />    &lt;/portType&gt;<br /><br />    &lt;binding name=&quot;OTNGUIDGeneratorBinding&quot; type=&quot;tns:OTNGUIDGeneratorPortType&quot;&gt;<br />        &lt;soap:binding style=&quot;rpc&quot; transport=&quot;http://schemas.xmlsoap.org/soap/http&quot;/&gt;<br />        &lt;operation name=&quot;getGUID&quot;&gt;<br />            &lt;soap:operation style=&quot;rpc&quot; soapAction=&quot;&quot;/&gt;<br />            &lt;input name=&quot;getGUID0Request&quot;&gt;<br />                &lt;soap:body use=&quot;encoded&quot; namespace=&quot;oracle.otn.ws.emarket.OTNGUIDGenerator&quot; encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;/&gt;<br />            &lt;/input&gt;<br />            &lt;output name=&quot;getGUID0Response&quot;&gt;<br />                &lt;soap:body use=&quot;encoded&quot; namespace=&quot;oracle.otn.ws.emarket.OTNGUIDGenerator&quot; encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;/&gt;<br />            &lt;/output&gt;<br />        &lt;/operation&gt;<br />    &lt;/binding&gt;<br /><br />    &lt;service name=&quot;OTNGUIDGenerator&quot;&gt;<br />      &lt;port name=&quot;OTNGUIDGeneratorSoapPort&quot; binding=&quot;tns:OTNGUIDGeneratorBinding&quot;&gt;<br />          &lt;soap:address location=&quot;http://webservices.oracle.com/ws/guid/oracle.ws.OTNGUIDGenerator&quot;/&gt;<br />      &lt;/port&gt;<br />    &lt;/service&gt;<br />&lt;/definitions&gt;<br /><br />

The best way to read this WSDL is probably starting from the bottom: a single service called OTNGUIDGenerator that contains a single port called OTNGUIDGeneratorSoapPort that is associated with the SOAP binding tns:OTNGUIDGeneratorBinding. This binding can be seen above the service element. It defines the binding’s protocol (SOAP over HTTP) and specifies the single getGUID operation. This operation can be seen as the implementation of the operation getGUID that is defined on the portType element. The portType is the functional, generic, protocol-free description of the functionality of the Web Service. It defines the operations and specifies the input message and output message for each operation. These messages are defined – their structure that is – in message elements.

We can now write a simple Java application to use our getGUIDResult() method and get hold of unique identities, without any knowledge of the fact that an external service was used to provide that identity. And what’s even more surprising: the getGUIDResult() method itself knows that it used WSIF to call an external service, but it does not know that it was in fact a SOAP Web Service. The Service consumer only knows functional details about the service.

To prove this point a little further, read on.

Extending the WSIF client for also consuming a Java WebService

We would like our getGUIDResult() to also return sensible output if we are offline. So even if the WebService it not available, should the getGUIDResult() method return a value. To facilitate this, we create our own Identity Service, a simple Java Class that returns a String (that happens to be not quite so unique…)

package nl.amis.soa;<br /><br />public class IdentityGenerator {<br />    public IdentityGenerator() {<br />    }<br /><br />    /**<br />     * This method will return a virtually unique string. It cvan be used as a <br />     * unique identifier within the current JVM Session.<br />     * <br />     * @return Unique String<br />     */<br />    public String getFreshIdentity() {<br />     return &quot;X&quot;;<br />   }<br />}<br /><br />

And now for the climax in this story: we add little extra information to the WSDL document, a WSIF specific extension, that defines an additional port for an extra binding of the portType OTNGUIDGeneratorPortType. We already have a binding of the portType to the soap implementation that Oracle provides on OTN. We add a binding to the Java based implementation in our own class IdentityGenerator. We define this Java binding in the WSDL file like this:

   &lt;!-- Java binding --&gt;<br />   &lt;binding name='IndentityGeneratorJavaBinding' <br />            type='tns:OTNGUIDGeneratorPortType'&gt;<br />    &lt;java:binding/&gt;<br />    &lt;format:typeMapping encoding=&quot;Java&quot; style=&quot;Java&quot;&gt;<br />      &lt;format:typeMap typeName=&quot;xsd:string&quot; formatType=&quot;java.lang.String&quot; /&gt;<br />    &lt;/format:typeMapping&gt;<br />    &lt;operation name=&quot;getGUID&quot;&gt;<br />      &lt;java:operation<br />         methodName=&quot;getFreshIdentity&quot;<br />         methodType=&quot;instance&quot; /&gt;<br />      &lt;input/&gt;<br />    &lt;/operation&gt;<br />   &lt;/binding&gt;<br /><br />

In this binding, we indicate the type: java:binding (WSIF also supports EJB, JMS and JCA in addition to SOAP). We specify that the getGUID operation that is defined in the portType is mapped to the Java method getFreshIdentity.

We also add two namespaces:

             xmlns:format=&quot;http://schemas.xmlsoap.org/wsdl/formatbinding/&quot;<br />             xmlns:java=&quot;http://schemas.xmlsoap.org/wsdl/java/&quot;<br /><br />

and of course the port-definition:

    &lt;service name=&quot;OTNGUIDGenerator&quot;&gt;<br />      &lt;port name=&quot;OTNGUIDGeneratorSoapPort&quot; binding=&quot;tns:OTNGUIDGeneratorBinding&quot;&gt;<br />          &lt;soap:address location=&quot;http://webservices.oracle.com/ws/guid/oracle.ws.OTNGUIDGenerator&quot;/&gt;<br />      &lt;/port&gt;<br />      &lt;port name=&quot;IdentityJavaPort&quot; binding=&quot;tns:IndentityGeneratorJavaBinding&quot;&gt;<br />        &lt;java:address className=&quot;nl.amis.soa.IdentityGenerator&quot;/&gt;<br />      &lt;/port&gt;<br />    &lt;/service&gt;<br />&nbsp;

With all this in place, we can make a tiny change to the service consumer code. We check the result of our service call and if the result suggests that the peferred service-port was not available, we use the fallback-port; that results in a call to the Java implementation when the SOAP WebService is not available:

package nl.amis.identity;<br /><br />import org.apache.wsif.WSIFException;<br />import org.apache.wsif.WSIFMessage;<br />import org.apache.wsif.WSIFOperation;<br />import org.apache.wsif.WSIFPort;<br />import org.apache.wsif.WSIFService;<br />import org.apache.wsif.WSIFServiceFactory;<br /><br /><br />public class IdentityManager {<br />    public IdentityManager() {<br />    }<br /><br />    private static String SOAP_PORT = &quot;OTNGUIDGeneratorSoapPort&quot;;<br />    private static String JAVA_PORT = &quot;IdentityJavaPort&quot;;<br /><br />    private static String getGUIDResult(String portName) {<br />        String result = &quot;&quot;;<br />        try {<br />            // create the ServiceFactory<br />            WSIFServiceFactory factory = WSIFServiceFactory.newInstance();<br />            // create a Service based on the Service and PortType combination <br />            // in the specified WSDL<br />            WSIFService service =<br />                factory.getService(&quot;OTNGUIDGenerator.wsdl&quot;<br />                                  , null, null<br />                                  , &quot;http://webservices.oracle.com/wstns/oracle/ws/OTNGUIDGenerator&quot;  // portType namespace<br />                                  , &quot;OTNGUIDGeneratorPortType&quot;  // portType <br />                                  );<br />            WSIFPort port;<br />            if (portName == null)<br />                port = service.getPort();<br />            else<br />                port = service.getPort(portName);<br />            // Create the operation object for the getGUID operation in the WSDL<br />            WSIFOperation operation =<br />                port.createOperation(&quot;getGUID&quot;, &quot;getGUID0Request&quot;,&quot;getGUID0Response&quot;);<br /><br />            WSIFMessage input = operation.createInputMessage();<br />            WSIFMessage output = operation.createOutputMessage();<br />            WSIFMessage fault = operation.createFaultMessage();<br />            // invoke the Service<br />            boolean success =<br />                operation.executeRequestResponseOperation(input, output, fault);<br />            String guidResult = (String)output.getObjectPart(&quot;return&quot;);<br />            if (success == true) {<br />                result = guidResult;<br />            } else {<br />                System.out.println(&quot;No Result..Unable to execute the Operation!!&quot;);<br />            }<br /><br />        } catch (WSIFException we) {<br />            System.out.println(&quot;Got exception from WSIF, details:&quot;);<br />            // we.printStackTrace();<br />        } catch (Exception e) {<br />            e.printStackTrace();<br />        }<br />        return result;<br />    }<br /><br />    /**<br />     * This method will return a virtually unique string. It cvan be used as a <br />     * unique identifier within the current JVM Session.<br />     * <br />     * @return Unique String<br />     */<br />    public static String getIdentity() {<br />        // note: this method will make use of either the OTNGUIDGenerator based <br />        //       service - when available - and else the local java based <br />        //       IdentityGenerator service<br />        String identity;<br />        identity = getGUIDResult(SOAP_PORT);<br />        if ((identity == null) || (identity.length() &lt; 2)) {<br />            // alternative approach<br />            identity = getGUIDResult(JAVA_PORT);<br />        }<br />        return identity;<br />    }<br /><br />    public static void main(String[] args) {<br />     System.out.println(&quot;Your new unique Identity is &quot; + IdentityManager.getIdentity());<br />    }<br /><br />}<br />&nbsp;

The result of running class IdentityManager is something like:

 

Resources

The sources for this introduction, WSIFIntro.zip , packaged as JDeveloper 10.1.3 Project (note: you will have tp download Apache WSIF 2.0 yourself) 

Web Service Invocation Framework – Adding wheels to Web services By: Anshuk Pal Chaudhari; Sandeep Gaikwad; Ambar Verma; V. Niranjan, SOA WebServices Journal (Sep. 23, 2005)

Apache WSIF Project

Using Java from BPEL with WSIF, Anthony Reynolds, December 15, 2004

WSIF, webservice-a-lize uw applicaties – WhiteHorses, September 2004 

Using WSIF for Integration by Matjaz B. Juric, part 4 of SOA Best Practices: The BPEL Cookbook on OTN (Oracle Technology Network)

How to write a WSIF provider – Apache WSIF Project Site

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

4 Comments

  1. Great entry, nice clear example of using client side access.
    Latest JDeveloper release now supports generation of WSIF binding for Java as well as SOAP 1.1 and 1.2.
    As Sandor says, in Oracle BPEL the use-case is usually the other way round to this, as we use WSIF to invoke all interfaces, SOAP, Java or JCA – hence good to see another use for a very powerful level of indirection.

  2. Sandor Nieuwenhuijs on

    One remark about the use of WSIF in Oracle BPEL Process Manager: all Service calls
    are done using WSIF, normally using the SOAP binding, and as soon as you use one
    of the adapters (e.g. DB for PL/SQL, File, etc), we will use the JCA binding.
    There are also two interesting blog entries from my colleague in the UK about WSIF
    and BPEL:
    http://www.orablogs.com/reynolds/archives/2005_09.html
    and
    http://www.orablogs.com/reynolds/archives/000747.html
    There is also an interesting SOA Cookbook on this subject on OTN:
    http://www.oracle.com/technology/pub/articles/bpel_cookbook/index.html
    (see #4)

  3. Aino Andriessen on

    The above example requires a lot of libraries. Organizing them can be a tedious job, that can be made a lot easier with the use of Maven. This archive contains a Maven2 and JUnit enabled update of the example.

    Unfortunately, some licences do not allow libraries to be part of the central Maven repositories. That’s, for example, the case for many javax libraries. So you’ll have to manually download and add activation.jar (v1.0.2), mail.jar (v1.3.2) and j2ee.jar (any version?!) to your local repository:
    mvn install:install-file -Dfile=mail.jar -DgroupId=javax.mail -DartifactId=mail -Dversion=1.3.2 -Dpackaging=jar
    mvn install:install-file -Dfile=activation.jar -DgroupId=javax.activation -DartifactId=activation -Dversion=1.0.2 -Dpackaging=jar
    mvn install:install-file -Dfile=j2ee.jar -DgroupId=javax.j2ee -DartifactId=j2ee -Dversion=1.4 -Dpackaging=jar

    You can now run the untitests on the commandline with ‘mvn test’ or generate a project site with ‘mvn site’.

    It is also very easy to generate Eclipse project files with ‘mvn eclipse:eclipse’, but make sure that you define in Eclipse the M2_REPOS variable that points to the maven repository root. Unfortunately maven cannot (yet?) generate the JDeveloper .jpr file, so I’ve included mine in the archive. Beware, it uses relative references to the files in my own maven repository, so you’d probably have to change these.