Use JNDI

This example task shows you how to use WSIF to bind a reference to a Web service, then look up the reference using JNDI.

You access a Web service through information given in the WSDL document for the service. If you do not know where to find the WSDL document for the service, but you know that it has been registered in a UDDI registry, you look it up in the registry. Java programs access java objects and resources in a similar manner, but using a JNDI interface.

The following example shows how, using WSIF, you can bind a reference to a Web service then look up the reference using JNDI.

Specifying the argument values for the Web Service

The Web service is represented in WSIF by an instance of the org.apache.wsif.naming.WSIFServiceRef class. This simple Referencable object has the following constructor:

public WSIFServiceRef(
        String WSDL,
        String sNS,
        String sName,
        String ptNS,
        String ptName)
{
    [...]
}
where

For example, if the WSDL file for the Web service is available from the URL http://localhost/WSDL/Example.WSDL and contains these service and port type definitions -

<definitions targetNamespace="http://hostname/namespace/example"
               xmlns:abc="http://hostname/namespace/abc"
[...]
    <portType name="ExamplePT">
      <operation name="exampleOp">
        <input name="exampleInput" message="tns:ExampleInputMsg"/>
      </operation>
    </portType>
[...]
    <service name="abc:ExampleService">
[...]          
    </service>
[...]
  </definitions>

then you specify these argument values for WSIFServiceRef:

To bind the service reference in the naming directory using JNDI, you can use the WebSphere Application Server - Express JndiHelper com.ibm.websphere.naming.JndiHelper class as follows:

[...]
    import com.ibm.websphere.naming.JndiHelper;
    import org.apache.wsif.naming.*;
[...]
    try {
       Context startingContext = new InitialContext();
       WSIFServiceRef ref = new WSIFServiceRef("http://localhost/WSDL/Example.WSDL,
                                               "http://localhost/WSDL/Example.WSDL",
                                               "http://hostname/namespace/abc"
                                               "ExampleService",
                                               "http://hostname/namespace/example",
                                               "ExamplePT");
       JndiHelper.recursiveRebind(startingContext, "myContext/mySubContext/myServiceRef", ref);
    
    }
    catch (NamingException e) {
       // Handle  error.
    }
[...]

Looking up the service using JNDI

This code fragment shows the lookup of a service using JNDI:

Note: For legal information about this code example, see the Code example disclaimer.

[...]
    try {
[...]
       InitialContext ic = new InitialContext();
       WSIFService myService = (WSIFService) ic.lookup("myContext/mySubContext/myServiceRef");
[...]
    }
    catch (NamingException e) {
       // Handle error.
    }
[...]