Java Naming and Directory Interface is an API designed for applications. It provides developers with a universal and unified interface for searching and accessing various naming and directory services, JDBC is built on the abstraction layer.
The available directories and services that can be accessed by JNDI are:
DNS, xnam, Novell Directory Service, LDAP (Lightweight Directory Access Protocol light Directory Access Protocol), the Registry, RMI, DSML V1 & V2, and NIS of the CORBA object service, file system, Windows XP/2000/NT/ME/9x.
JNDI advantages:
It contains a large number of naming and directory services and uses common interfaces to access different types of services;
You can connect to multiple naming or directory services at the same time;
Establish a logical association. The name can be associated with a Java object or resource without guiding the physical ID of the object or resource.
Java Naming and Directory Interface is an API designed for applications. It provides developers with a universal and unified interface for searching and accessing various naming and directory services, JDBC is built on the abstraction layer.
The available directories and services that can be accessed by JNDI are:
DNS, xnam, Novell Directory Service, LDAP (Lightweight Directory Access Protocol light Directory Access Protocol), the Registry, RMI, DSML V1 & V2, and NIS of the CORBA object service, file system, Windows XP/2000/NT/ME/9x.
JNDI advantages:
It contains a large number of naming and directory services and uses common interfaces to access different types of services;
You can connect to multiple naming or directory services at the same time;
Establish a logical association. The name can be associated with a Java object or resource without guiding the physical ID of the object or resource.
JNDI package:
Javax. Naming: Name 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 of different implementations.
The JNDI naming and service functions are used to provide enterprise-level APIs access to naming and services. For example, ejbs, JMS, JDBC 2.0, and RMI on IIOP use the name service of CORBA through JNDI.
JNDI and JDBC:
JNDI provides a unified method for searching and accessing services on the network. By specifying a resource name, the name corresponds to a record in the database or naming service, and returns the information required for database connection establishment.
Sample Code:
Try {
Context cntxt = new initialcontext ();
Datasource DS = (datasource) cntxt. Lookup ("JDBC/DPT ");
}
Catch (namingexception ne ){
...
}
JNDI and JMS:
Message Communication is a method used by software components or applications to communicate. JMS is a Java technology that allows applications to create, send, receive, and read messages.
Sample Code:
Try {
Properties Env = new properties ();
Initialcontext inictxt = new initialcontext (ENV );
Topicconnectionfactory connfactory = (topicconnectionfactory) inictxt. Lookup ("ttopicconnectionfactory ");
...
}
Catch (namingexception ne ){
...
}
Access a specific directory: for example, a person is an object and has several attributes, such as the name, phone number, email address, and zip code of the person. Getattributes () method
Attribute ATTR =
Directory. getattributes (personname). Get ("email ");
String email = (string) ATTR. Get ();
By using JNDI, the customer can use the object name or attribute to find the object:
Foxes = directory. Search ("O = Wiz, c = us", "Sn = Fox", controls );
Use JNDI to search for objects such as printers and databases and the printer example:
Printer printer = (printer) namespace. Lookup (printername );
Printer. Print (document );
Browse namespace:
Namingenumeration list = namespace. List ("O = widget, c = us ");
While (list. hasmore ()){
Nameclasspair entry = (nameclasspair) List. Next ();
Display (entry. getname (), entry. getclassname ());
}
References:
Http://java.sun.com/products/jndi/examples.html
Http://java.sun.com/products/jndi/serviceproviders.html
Common JNDI operations:
Common JNDI operations:
Void BIND (string sname, object);-binding: Process of associating names with objects
Void rebind (string sname, object);-rebind: Used to rebind an object with an existing name
Void unbind (string sname);-release: Used to release an object from the directory.
Void Lookup (string sname, object);-query: returns an object of the total directory.
Void Rename (string soldname, string snewname);-Rename: used to modify the name bound to the object name
Namingenumeration listbinding (string sname);-list: list of objects bound to a specific context
Namingenumeration list (string sname );
Code example: Get the name, class name, and bound object again.
Namingenumeration namenumlist = ctxt. listbinding ("cntxtname ");
...
While (namenumlist. hasmore ()){
Binding BND = (binding) namenumlist. Next ();
String sobjname = BND. getname ();
String sclassname = BND. getclassname ();
Someobject objlocal = (someobject) BND. GetObject ();
}