Chapter 9. The JNDI Naming Service
queue/IncomingOrdersand need not worry about any of the queue's configuration details.
ProductCatalogsession bean from the cluster without needing to know which machine it resides on. Whether it is a large clustered service, a local resource or an application component that is needed, the JNDI naming service provides the glue that lets code find the objects in the system by name.
9.1. An Overview of JNDI
javax.namingpackage. It contains five interfaces, 10 classes, and several exceptions. There is one key class,
InitialContext, and two key interfaces,
/"). The file's path is ordered from left to right. The pathname
/usr/jboss/readme.txt, for example, names a file
readme.txtin the directory
jboss, under the directory
usr, located in the root of the file system. JBoss Enterprise Application Platform naming uses a Unix-style namespace as its naming convention.
javax.naming.Nameinterface represents a generic name as an ordered sequence of components. It can be a composite name (one that spans multiple namespaces), or a compound name (one that is used within a single hierarchical naming system). The components of a name are numbered. The indexes of a name with N components range from 0 up to, but not including, N. The most significant component is at index 0. An empty name has no components.
scp. For example, the following command copies
localfile.txtto the file
tmpdirectory on host
scp localfile.txt ahost.someorg.org:/tmp/remotefile.txt
ahost.someorg.org:/tmp/remotefile.txtis a composite name that spans the DNS and Unix file system namespaces. The components of the composite name are
/tmp/remotefile.txt. A component is a string name from the namespace of a naming system. If the component comes from a hierarchical namespace, that component can be further parsed into its atomic parts by using the
javax.naming.CompoundNameclass. The JNDI API provides the
javax.naming.CompositeNameclass as the implementation of the
Nameinterface for composite names.
javax.naming.Contextinterface is the primary interface for interacting with a naming service. The
Contextinterface represents a set of name-to-object bindings. Every context has an associated naming convention that determines how the context parses string names into
javax.naming.Nameinstances. To create a name-to-object binding you invoke the bind method of a
Contextand specify a name and an object as arguments. The object can later be retrieved using its name using the
Contextlookup method. A
Contextwill typically provide operations for binding a name to an object, unbinding a name, and obtaining a listing of all name-to-object bindings. The object you bind into a
Contextcan itself be of type
Contextobject that is bound is referred to as a subcontext of the
Contexton which the bind method was invoked.
/usr, which is a context in the Unix file system. A file directory named relative to another file directory is a subcontext (commonly referred to as a subdirectory). A file directory with a pathname
jbosscontext that is a subcontext of
usr. In another example, a DNS domain, such as
org, is a context. A DNS domain named relative to another DNS domain is another example of a subcontext. In the DNS domain
jboss.org, the DNS domain
jbossis a subcontext of
orgbecause DNS names are parsed right to left.
188.8.131.52. Obtaining a Context using InitialContext
Contextinterface. Therefore, you need a way to obtain a
Contextfor the naming service you are interested in using. The
javax.naming.IntialContextclass implements the
Contextinterface, and provides the starting point for interacting with a naming service.
InitialContext, it is initialized with properties from the environment. JNDI determines each property's value by merging the values from the following two sources, in order.
- The first occurrence of the property from the constructor's environment parameter and (for appropriate properties) the applet parameters and system properties.
jndi.propertiesresource files found on the classpath.
jndi.propertiesfile, which allows your code to externalize the JNDI provider specific information so that changing JNDI providers will not require changes to your code or recompilation.
Contextimplementation used internally by the
InitialContextclass is determined at runtime. The default policy uses the environment property
java.naming.factory.initial, which contains the class name of the
javax.naming.spi.InitialContextFactoryimplementation. You obtain the name of the
InitialContextFactoryclass from the naming service provider you are using.
jndi.propertiesfile a client application would use to connect to a JBossNS service running on the local host at port 1099. The client application would need to have the
jndi.propertiesfile available on the application classpath. These are the properties that the JBossNS JNDI implementation requires. Other JNDI providers will have different properties and values.
Example 9.1. A sample jndi.properties file
### JBossNS properties java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=jnp://localhost:1099 java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces