The Java Naming and Directory Interface, or JNDI, is part of the new Java platform. It enables your application to be even more platform-independent. It enables a component to declare its need for a resource and for you to set up a your own configuration to provide a compatible resource. It is specifically useful with, but not limited to, data sources and Enterprise Java Beans (EJBs).
A component declares its need for a resource by invoking the JNDI subsystem at runtime. It makes a request for a resource by name. When JNDI is configured appropriately, a compatible resource is provided to the component. The component does not need to know (a) about the implementation of the resource and (b) how JNDI was configured.
JNDI is configured differently by different factories. A factory provides the initial JNDI context to an application, an instance of a virtual machine. The JNDI subsystem expects the name of a custom factory class to be available as a system property. Otherwise, an initial context cannot be created.
A custom factory provides the initial context. This means that a custom factory can use any mechanism to configure JNDI resources for components. It might depend on a properties file or an XML file. Either of these files should be external to the jar archive because they are modified to fit the environment where the components are deployed.
The JNDI subsystem is implemented in the
javax.naming package. It provides a generic resource mechanism, so that an instance of any class can be made available to a component.
JNDI is useful when working with an SQL service through the JDBC API. A component looks up an instance of
javax.sql.DataSource in JNDI instead of dealing with loading and configuring JDBC drivers directly. The name of the JDBC driver class is part of the data source. The JDBC URL is part of the data source. Even the account name and password can be part of the data source.
A data source is a connection factory. Once a component has a data source from JNDI, it can get a JDBC connection.
JNDI is useful when working with an EJB container. A component looks up an instance of a home interface in JNDI. A component does not need to know which machine is providing the EJB container.
How does a component lookup a resource in JNDI? It creates an initial context and then uses a JNDI-compatible URI to lookup a resource.
Content of these pages are owned and copyrighted by the poster.