Advantages
contains a large number of naming and directory services, using universal interfaces to access different kinds of services;
can connect to multiple naming or directory services at the same time On;
Establish logic associations, allowing the names with Java objects or resources without having to know the physical ID of the object or resource.
JNDI package:
javax.naming: naming operation;
javax.naming.directory: directory operation;
javax. Naming.Event: Request event notification in the named directory server;
javax.naming.ldap: provides LDAP support;
javax.naming.spi: Allows dynamic insertion different implementation.
Using JNDI's naming and service feature to meet the enterprise API access to named services, such as EJB, JMS, JDBC 2.0, and RMI on the IIOP, RMI, using CORBA naming services through JNDI.
Architecture
JNDI architecture provides a set of standard independent named system-based APIs, which are built on top of the name associated with the naming system. This layer helps separate the application with the actual data source, so regardless of the application access, LDAP, RMI, DNS, or other directory services. In other words, JNDI can use the specific implementation of the directory service, as long as the directory service provides an interface (or driver), you can use the directory.
About JNDI's attention is that it provides an Application Programming Interface (API) and Service Provider Interface (SPI). The true meaning of this is that allows the application to interact with the naming service or directory service, and must have this service JNDI service provider, which is where JNDI SPI works. Service providers are basically a class that implements JNDI interfaces for a variety of specific naming and directory services - very similar to the JDBC driver to achieve a JDBC interface. As an application developer, we don't have to worry about the specific implementation of JNDI SPI. Just confirm that every name or directory service you want to use has a service provider.
component
1, javax.naming: The class and interface containing the naming service is included. For example, it defines the Context interface, which is the entry of the naming service execution query.
2, javax.naming.directory: The expansion of the name package provides the class and interface of the access to the directory service. For example, it adds a new class to attributes, providing the DirContext interface representing the directory context to define the method of checking and updating the properties of the directory object.
3, javax.naming.event: Support for event notifications when accessing naming and directory services are provided. For example, define the Namingevent class, this class is used to indicate the event generated by the named / directory service define the NamingListener interface that listens to Namingevents.
4, javax.naming.ldap: This package provides support for the Operations and control of the LDAP version 3, universal package javax.naming.directory does not include these operations and control.
5, javax.naming.spi: This package provides a way to add support for access naming and directory services through javax.naming and related package dynamics. This package is provided for developers who are interested in creating a service provider.
Uployed
Naming or Directory Service Enables users to centrally store common information, this is important in web applications, as this makes such applications more coordinated and easier to manage. For example, the printer can be stored in a directory service to be used by applications related to the printer.
We all use a naming service every day. The object in the naming system can be the name in the DNS record, the user profile in the EJB component (Enterprise JavaBeans Component, LDAP (Lightweight Directory Access Protocol).
Directory service is a nature expansion of naming services. The key difference between the two is that the objects in the directory service can have attributes (for example, the user has an email address), and the object is not attribute in the naming service. Therefore, in the directory service, you can search objects based on the property. JNDI allows you to access files in the file system, locate remote RMI registration objects, access directory services such as LDAP, locate EJB components on the network.
For applications like LDAP clients, applying Launcher, class browsers, network management utilities, and even address thin, JNDI is a good choice.
Component
JNDI provides a unified way to find and access services on the network. By specifying a resource name, the name corresponds to a record in a database or naming service, while returning the information necessary for the database connection establishment.
JNDI mainly has two components: application programming interface and service provider interface. The application programming interface provides the functionality of the Java application to access various named naming and directory services, and the service provider interface provides the functionality of any service provider.
code example:
try {
context ();
DataSource DS = (Datasource) CNTXT.lookup ("JDBC / DPT");
}
catch (namingexception ne) {
...
}
Technical Application
Message Communication is a method of communicating with software components or applications. JMS is a Java technology that allows applications to create, send, receive, and read messages.
code example:
try {
Properties env = new proties ();
initialcontext INICTXT = New InitialContext (ENV);
TopicConnectionFactory ConnFactory = (TopicConnectionFactory) INICTXT.LOOKUP ("TtopicConnectionFactory");
...
}
catch (Namingexception) Ne) {
...
}
Access a specific directory: For an example, people are an object, he has several properties, such as this person Name, phone number, email address, postal code and other attributes. Through GetAttributes () method
attribute attr = directory.getattributes ("email");
string email = (string) attr.get ();
Find objects by using JNDI to use the object's name or property:
foxes = Directory.Search ("o = wiz, c = us", "sn = fox", Controls
Find an object such as a printer and database by using JNDI, find an example of the printer:
printer printer = (printer) Namespace.lookup (Printername);
printer.print (document);
Browse Name Space:
namingenumerative list = namespace.list ("o = widget, c = us");
while (list.hasmore ()) {
NameClasspair entry = (NameClasspair) list.next ();
display (entry.getname (), entry.getclassname ());
}
Common operation
void bind (string sname, object object); - Bind: Procedure associated with the same object
void rebind (String Sname, Object Object); - Re-binding: Use the object to resin the same name existing name
void unbind (String Sname); - Release: Used to release the object from the directory
object lookup (String sname); - Find: Return to an object in the directory
void rename (String Soldname, String SNEWNAME); - Rename: Use to modify the name of the object name Bind
namingenumerative listbinding (String sname); - List: Return to Binding list list list
NamingeNumeration List (String Sname);
code example: Recent name, class name, and binding object.
namingenumerative namenumlist = ctxt.listbinding ("cntxtName");
...
while (Namenumlist.hasmore ()) {
< P> Binding Bnd = (binding) Namenumlist.next ();string SobjName = Bnd.getName ();
string sclassname = Bnd.getClassName ();
SomeObject objlocal = (someobject) Bnd.getObject ();
}