Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Gemini/Naming/Documentation/JNDI Clients

JNDI Clients

A JNDI Client, in the context of Gemini Naming, is usually a piece of code that consumes JNDI services. This can typically take one of two forms:

  • JNDI Services - A JNDI Client can use the services defined in the "JNDI Services" Chapter of the OSGi Enterprise Specification, v4.2, in order to obtain JNDI providers.

The following is an example of a JNDI Services client using the JNDIContextManager service to create a JNDI Context instance:

    // obtain JNDIContextManager service from the OSGi BundleContext
    ServiceReference serviceReference = 
	bundleContext.getServiceReference("org.osgi.service.jndi.JNDIContextManager");
    JNDIContextManager contextManager = (JNDIContextManager)
        bundleContext.getService(serviceReference);
		
    // create a context with the default environment setup
    Context initialContext = contextManager.newInitialContext();

    // JNDI Client can now access the naming system with the Context instance...
  • "Traditional" JNDI Access - A JNDI Client can use the javax.naming.InitialContext() constructor in order to gain access to the naming service. These clients typically follow the pattern of Java EE clients that create an InitialContext, and use that instance to interact with the naming system.


The following is an example of a JNDI Client that uses the standard JNDI API in order to create the javax.naming.InitialContext instance used to interact with the naming system:

    Hashtable environment = new Hashtable();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, 
		    "com.sun.jndi.rmi.registry.RegistryContextFactory");
		
    // verify that context can be created without any errors
    Context context = null;
    try {
        context = new InitialContext(environment);
  
        // JNDI Client can now access the naming system with the Context instance...
    } finally {
        context.close();
    }

Copyright © Eclipse Foundation, Inc. All Rights Reserved.