111 lines
4.8 KiB
HTML
111 lines
4.8 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
|
||
|
|
||
|
<title>Use JNDI</title>
|
||
|
</head>
|
||
|
|
||
|
<BODY>
|
||
|
<!-- Java sync-link -->
|
||
|
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
||
|
|
||
|
<h5><a name="wswsifjndi">Use JNDI</A></h5>
|
||
|
|
||
|
<p>This example task shows you how to use WSIF to bind a reference to a Web service, then look up the reference using JNDI.</p>
|
||
|
|
||
|
<p>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.</p>
|
||
|
|
||
|
<p>The following example shows how, using WSIF, you can bind a reference to a Web service then look up the reference using JNDI.</p>
|
||
|
|
||
|
<p><strong>Specifying the argument values for the Web Service</strong></p>
|
||
|
|
||
|
<p>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:</p>
|
||
|
|
||
|
<pre>public WSIFServiceRef(
|
||
|
String <em>WSDL</em>,
|
||
|
String <em>sNS</em>,
|
||
|
String <em>sName</em>,
|
||
|
String <em>ptNS</em>,
|
||
|
String <em>ptName</em>)
|
||
|
{
|
||
|
[...]
|
||
|
}</pre>
|
||
|
|
||
|
where
|
||
|
<ul>
|
||
|
<li><em>WSDL</em> is the location of the WSDL file containing the definition of the service.</li>
|
||
|
<li><em>sNS</em> is the full namespace for the service definition (null can be specified if only one service is defined in the WSDL file). </li>
|
||
|
<li><em>sName</em> is the local name for the service definition (null can be specified if only one service is defined in the WSDL file).</li>
|
||
|
<li><em>ptNS</em> is the full namespace for the port type within the service that you want to use (null can be specified if only one port type is available for the service).</li>
|
||
|
<li><em>ptName</em> is the local name for the port type (null can be specified if only one port type is available for the service).</li>
|
||
|
</ul>
|
||
|
<p>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 - </p>
|
||
|
|
||
|
<pre><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></pre>
|
||
|
|
||
|
<p>then you specify these argument values for WSIFServiceRef:</p>
|
||
|
<ul>
|
||
|
<li>WSDL is http://localhost/WSDL/Example.WSDL</li>
|
||
|
<li>sNS is http://hostname/namespace/abc</li>
|
||
|
<li>sName is ExampleService</li>
|
||
|
<li>ptNS is http://hostname/namespace/example</li>
|
||
|
<li>ptName is ExamplePT</li>
|
||
|
<li>Binding the service using JNDI</li>
|
||
|
</ul>
|
||
|
<p>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:</p>
|
||
|
|
||
|
<pre>[...]
|
||
|
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.
|
||
|
}
|
||
|
[...]</pre>
|
||
|
|
||
|
<p><strong>Looking up the service using JNDI</strong></p>
|
||
|
|
||
|
<p>This code fragment shows the lookup of a service using JNDI:</p>
|
||
|
<p><strong>Note:</strong> For legal information about this code example, see the <a href="codex.htm">Code example disclaimer</a>.</p>
|
||
|
<pre>[...]
|
||
|
try {
|
||
|
[...]
|
||
|
InitialContext ic = new InitialContext();
|
||
|
WSIFService myService = (WSIFService) ic.lookup("myContext/mySubContext/myServiceRef");
|
||
|
[...]
|
||
|
}
|
||
|
catch (NamingException e) {
|
||
|
// Handle error.
|
||
|
}
|
||
|
[...]</pre>
|
||
|
|
||
|
|
||
|
</body>
|
||
|
</html>
|