Use this procedure to access objects in the naming system by creating an InitialContext instance.
To create an InitialContext that provides client access to the JNDI Registry Service through the J2EE Engine as a name service provider, you must use one of the JNDI Registry Service factories and the URL of any J2EE Engine cluster as properties passed to the constructor of the InitialContext. The available factories are:
●
com.sap.engine.services.jndi.InitialContextFactoryImpl
Grants access to the naming system of an arbitrary server process if the
client is a remote one, and to the current server process if the client is
running on the same server. The operations that are processed through it are
referred only to this server process. That is, the objects are available only
on the specified server process and are not available on the other server
processes in the cluster.
This is only valid for server clients. The remote clients always perform global naming operations.
●
com.sap.engine.services.jndi.InitialReplicatingContextFactoryImpl
Grants a Context through which all operations
are replicated in the cluster. That is, the objects/contexts that are
bound/created are available in the whole cluster.
The InitialContext factory uses different properties to customize the InitialContext for a specific environment. These properties are passed to the factory using a Hashtable as a parameter of the InitialContext constructor. To obtain an InitialContext as an external client, you must use the following properties:
·
java.naming.provider.url or Context.PROVIDER_URL
Specifies the URL of the cluster. The value of the PROVIDER_URL property contains the host and
the P4 port on the dispatcher. By using the “#” sign you can also
add a clusterID. This way, you will be able to use the JNDI Registry Service
of the server process specified by the clusterID.
·
java.naming.security.principal or Context.SECURITY_PRINCIPAL
Specifies the identity of the principal (user) for security purposes.
The default value is
"Guest”.
·
java.naming.security.credentials or Context.SECURITY_CREDENTIALS
Specifies the password for the principal.
The JNDI Registry Service uses the J2EE Engine Security Provider Service to authenticate the users. If the Context.SECURITY_PRINCIPAL and Context.SECURITY_CREDENTIALS are not specified you will be logged in the system as Guest. This is valid only for the default configuration. For more information, see Integration with Other Services.
Context ctx = null; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl"); env.put(Context.PROVIDER_URL, "localhost:50004");
try { ctx = new InitialContext(env); } catch (NamingException ne) { ne.printStackTrace() } finally { try { ctx.close(); } catch (Exception e) { e.printStackTrace() } } |
It is recommended that you close the context when you have finished using it.
If the client needs bindings to be replicated across the JNDI tree of each server process within the cluster, it has to use com.sap.engine.services.jndi.InitialReplicatingContextFactoryImpl instead of com.sap.engine.services.jndi.InitialContextFactoryImpl.
This is only valid for server clients. The remote clients always perform global naming operations.
Clients that are running on the server side do not need to specify any properties. They simply use the InitialContext constructor without parameters:
Context ctx = new InitialContext(); |
It is not advisable for server-side clients to try to specify security credentials and principles when getting new InitialContext.
If a provider URL is not specified, the context will be created by the JNDI Registry Service of the same server process. A client that is running on a certain server process in the cluster can get context to the naming system of another cluster by specifying provider URL and setting a specific property with name “force_remote” and value “true”.
Context ctx = null; Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, "10.55.161.251:50004"); env.put(“force_remote”, “true”); env.put(Context.SECURITY_PRINCIPAL, "MyUserName"); env.put(Context.SECURITY_CREDENTIALS, "MyPassword");
try { ctx = new InitialContext(env); } catch (NamingException ne) { e.printStackTrace() } |
The MyUserName and MyPassword entries in the example above are the values that the application user passes to the application. These values are not the hard-coded values for the actual user name and corresponding password.
The port specified in the provider URL is the P4 port of the dispatcher. Although the client is an application, if it wants to use the naming system of a different Java instance it has to specify security credentials and principles. In the above example the application is a remote client for the JNDI Registry Service of a different Java instance.
See also:
RMI-P4 Specific InitialContext Properties