ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/jndicntx.htm

165 lines
7.4 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!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>Obtain the initial JNDI context for the component</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jndicntx"></a>Obtain the initial JNDI context for the component</h5>
<p>To access an enterprise resource such as a data source or JavaMail session in a distributed computing environment, you can use the JNDI API to locate that object so that you can use it in your code. To do this, you must first obtain the initial context that contains the resource (a Java object).</p>
<p>You can obtain an initial JNDI context in one of two ways:</p>
<ul>
<li><a href="#get">Get an initial context using JNDI properties found in the current environment</a></li>
<li><a href="#set">Get an initial context by explicitly setting JNDI properties</a></li>
</ul>
<a name="get"></a>
<p><strong>Get an initial context using JNDI properties found in the current environment</strong></p>
<p>The current environment includes the Java system properties and properties defined in properties files found in the classpath. This code snippet illustrates how to obtain the initial context from these properties:</p>
<pre>
import javax.naming.Context;
import javax.naming.InitialContext;
// ... class declaration, method declaration code here ...
Context initialContext = new InitialContext();
</pre>
<a name="set"></a>
<p><strong>Get an initial context by explicitly setting JNDI properties</strong></p>
<p>In general, JNDI clients should assume that the correct environment is already configured. If this is the case, there is no need for you to explicitly set property values and pass them to the constructor of the InitialContext object. However, a JNDI client might need to access a namespace other than the one identified in its environment. In this event, you must explicitly set one or more properties used by the InitialContext constructor. Any property values you pass to the InitialContext constructor take precedence over settings of the equivalent properties found elsewhere in the client's environment.</p>
<p>You can use two different provider URL forms with WebSphere Application
Server's initial context factory:
<ul>
<li>A CORBA object URL (new for J2EE 1.3)</li>
<li>An IIOP URL</li>
</ul>
</p>
<p>CORBA object URLs are more flexible than IIOP URLs and are the recommended
URL format to use. CORBA object URLs are part of the OMG CosNaming Interoperable
Naming Specification. A corbaname URL, for example, can include initial context
and lookup name information and can be used as a lookup name without the need
to explicitly obtain another initial context.The IIOP URLs are the legacy
JNDI format, but are still supported by the WebSphere Application Server initial
context factory.</p>
<ul>
<li>
<p><strong>Using a CORBA object URL</strong></p>
<pre>...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
&quot;com.ibm.websphere.naming.WsnInitialContextFactory&quot;);
env.put(Context.PROVIDER_URL, &quot;corbaloc:iiop:myhost.mycompany.com:2809&quot;);
Context initialContext = new InitialContext(env);
...
</pre>
</li>
<li>
<p><strong>Using a CORBA object URL with multiple name server addresses</strong></p>
<p>CORBA object URLs can contain more than one bootstrap address. You can
use this feature when attempting to obtain an initial context from a server
cluster. You can specify the bootstrap addresses for all servers in the cluster
in the URL. The operation succeeds if at least one of the servers is running,
eliminating a single point of failure. There is no guarantee of any particular
order in which the address list will be processed. For example, the second
bootstrap address may be used to obtain the initial context even though the
server at the first bootstrap address in the list is available.</p>
<p>Multiple-address provider URLs should only contain the bootstrap addresses
of members of the same cluster. Otherwise, incorrect behavior may occur.</p>
<pre>...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
&quot;com.ibm.websphere.naming.WsnInitialContextFactory&quot;);
// All of the servers in the provider URL below are members of
// the same cluster.
env.put(Context.PROVIDER_URL,
&quot;corbaloc::myhost1:9810,:myhost1:9811,:myhost2:9810&quot;);
Context initialContext = new InitialContext(env);
...</pre>
</li>
<li>
<p><strong>Using a CORBA object URL from an non-WebSphere Application Server JNDI
implementation</strong></p>
<p>Initial context factories for CosNaming JNDI plug-in implementations other
than the WebSphere Application Server initial context factory most likely
obtain an initial context using the object key, <samp>NameService</samp>. When
you use such a context factory to obtain an initial context from a WebSphere
Application Server name server, the initial context is the cell root context.
Since system artifacts such as EJB homes associated with a server are bound
under the server's server root context, names used in JNDI operations must
be qualified. If you want to use relative names, ensure your initial context
is the server root context under which the target object is bound. In order
to make the server root context the initial context, specify a corbaloc provider
URL with an object key of <samp>NameServiceServerRoot</samp>.</p>
<p>This example shows a CORBA object type URL from a non-WebSphere Application
Server JNDI implementation. This example assumes full CORBA object URL support
by the non-WebSphere Application Server JNDI implementation. The object key
of <samp>NameServiceServerRoot</samp> is specified so that the initial context
will be the specified server's server root context.</p>
<pre>...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
&quot;<strong>com.somecompany.naming.TheirInitialContextFactory&quot;</strong>);
env.put(Context.PROVIDER_URL,
&quot;corbaname:iiop:myhost.mycompany.com:9810/<strong>NameServiceServerRoot</strong>&quot;);
Context initialContext = new InitialContext(env);
...</pre>
<p>If qualified names are used, you can use the default key of <samp>NameService</samp>.</p>
</li>
<li>
<p><strong>Using an IIOP URL</strong></p>
<p>The IIOP type of URL is a legacy format which is not as flexible as CORBA
object URLs. However, URLs of this type are still supported. The following
example shows an IIOP type URL as the provider URL.</p>
<pre>...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
&quot;com.ibm.websphere.naming.WsnInitialContextFactory&quot;);
env.put(Context.PROVIDER_URL, &quot;iiop://myhost.mycompany.com:2809&quot;);
Context initialContext = new InitialContext(env);
...
</pre></li>
</ul>
</body>
</html>