The JNDI API gives you a tool for accessing a variety of naming services (like RMI, DNS ,Corba Naming) and directory services (like NIS, LDAP, NDS) using Java. JNDI Interface enables you to locate and use objects stored in naming and directory services. For example, a J2EE deployment may use a naming or directory service as a repository to store objects like EJBs or JDBC DataSource objects. JNDI gives you the tools to create applications that can manage, retrieve, and use those objects from the services.
Naming and Directory Concepts
----------------------------
A name is simply a human-readable logical value for referring to an object in a system. A Naming Service provides a mechanism for giving names to objects so you can retrieve and use those objects without knowing the address / location of the object. Objects can be located on any machine accessible from your network, not necessarily the local workstation. The concept of a naming service is which names are associated with objects and objects are found based on their names. The chief function of a naming service is to map people-friendly names to objects, such as addresses, identifiers, or objects typically required and used by computer programs. For example: Domain Name Systems (DNS) maps machine names (google.com) to IP addresses (such as 192.18.97.71)
A variety of naming are extended with a directory services. A directory service associates names with objects and also allows objects to have attributes. It is possible not only look up an object by its name but also get the object's attributes or even search for the object based on its attributes.
For example a telephone company's directory service. It is used to map a subscriber's name to his address and telephone number. A computer's directory service is very much like a telephone company's directory service in that both can be used to store information such as a telephone number or address. The computer's directory service is much more powerful because it is available online and can be used to store a variety of information that can be utilized by users, programs, and even the computer itself and other computers.
The JNDI API consists of two packages:
•javax.naming
•javax.naming.directory
javax.naming :- The naming interface organizes information hierarchically and maps user friendly names to addresses or objects that are machine friendly, e.g. www.novell.com -> 137.65.2.5. It allows access to named objects through multiple name spaces.
The core interfaces and classes in this package are: Context , Binding , Name , NameParser
javax.naming.directory : - The directory interface provides access to directory objects which can contain attributes thereby providing attribute-based searching and schema support. The major interfaces in this package are DirContext, Attribute and Attributes
The following example illustrates to search Datasources Object with the name "jdbc/TestDataSource" in the naming services and returns the address / location of the object wheather it may be in local or remote location.
public static Connection getConnection(String strLookupName) throws CDRCFatalException {
Context ctxLookup = null;
DataSource dBSource = null;
Connection connection = null;
try {
Hashtable param = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
parms.put(Context.PROVIDER_URL, "iiop:///");
javax.sql.DataSource ds = null;
ctxLookup = new InitialContext(param);
dBSource =(javax.sql.DataSource) ctxLookup.lookup("jdbc/TestDataSource"); // jdbc/TestDataSource is JNDI Name
} catch (NamingException exc) {
System.out.println("INFO:Lookup fail of database " + exc.toString() + "The context is "+ ctxLookup);
throw new CDRCFatalException(exc.getMessage(), "01001");
}
try {
conn = dBSource.getConnection(username, password);
} catch (SQLException exc) {
System.out.println("Connection Fail of database " + exc.toString()+ "THe context is " + ctxLookup);
throw new CDRCFatalException(exc.getMessage(), "02006");
}
return connection;
}
0 comments:
Post a Comment