!--a11y-->
Creating an InitialContextUse this procedure to access objects in the naming system by creating an InitialContextinstance.
To create InitialContext, which provides client access to the JNDI Registry Service through the SAP 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 IP and port, but using the “#” sign you can also add a clusterID. This way, you will be able to use the JNDI Registry Service of the server 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 "Anonimous”.
· java.naming.security.credentials or Context.SECURITY_CREDENTIALS – specifies the password for the principal. The default value is an empty string.
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 like Anonymous.

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 application 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 default and will connect to the JNDI Registry Service of this server process. Of course, 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 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 server process 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 server process.
See also:
Security Provider
Service in the Administration Manual
P4 Provider
Service in the Administration Manual
