JNDI learning

xiaoxiao2021-03-06  68

JNDI is a full name Java Naming and Directory Interface, including naming services and directory services. Naming Services will be associated with an object in order to access these objects through the corresponding name. The directory service is a naming service of its objects with attributes and names. Any name-based technology can provide services through JNDI as long as JNDI supports this technology. JNDI currently supports the technology including LDAP, CORBA Common Object Service (COS) name service, RMI, NDS, DNS, Windows registry, and more. Many J2EE technologies, including EJB relying on JNDI to organize and locate entities.

JDNI links the objects and names through the concept of binding. In a file system, the file name is bound to the file. In DNS, an IP address is bound to a URL. In the directory service, an object name is bound to an object entity.

A set of bindings in JNDI is referenced as a context. For example, each context provides a lookup operation that returns the corresponding object of the specified name. Each context provides a binding and removal of binding names to an object.

Uses of JNDI: 1. You can use JNDI to get the properties of the Object class, such as: attribute attr = directory.getattributes .get ("email"); string email = (string) Attr.get (); 2. You can search for objects such as: foxes = Directory.Search ("o = wiz, c = us", "sn = fox", controls); looking for the name of Fox called Fox in the Wiz department employee? 3. You can use JNDI to query the Naming / Directory service to query the objects like Printers and Databases, such as: Query PrinterPrinter Printer = (Printer) Namespace.lookup (Printer.Print (Document); 4. You can use the JNDI list of special levels of namespace, such as: namingenumeration list = namespace.list ("o = widget, c = us"); while (list.hasmore ()) {NameClasspair entry = (NameClasspair) list. Next (); display (entry.getname (), entry.getclassname ());} The address based on JNDI documentation is translated by JNDI document: http://java.sun.com/products/jndi/overview.html

To use JNDI, call javax.name. *;

Take a few examples to see, use these examples, you need to include fscontext.jar and provoderut.jar in your ClassPath, or like me, you can simply copy these two files to Java_Home / JRE / LIB / EXT, Java_Home here refers to the root directory of your Java 2SDK installation. These two JARs can go to http://java.sun.com/products/jndi/downloads/index.html to download. Example 1: An example of naming services Import java.util.hashtable; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception;

Public class lookup {public static void main (String [] args) {hashtable env = new hashtable (); string name = "c: /aaa.txt"; try {env.put (Context.initial_Context_Factory, "com.sun. jndi.fscontext.RefFSContextFactory "); // use the file system as service provider Context ctx = new InitialContext (env); Object obj = ctx.lookup (name); System.out.println (" the name " name " Is Bound To Object: " Obj);} catch (exception ex) {ex.printStackTrace ();}}} You can see that C: /aaa.txt has been bound to c: /aa.txt.

Example 2, an example of a directory service, Import javax.naming.binding;

Import javax.naming.namingenumerative;

Import javax.naming.context;

Import javax.naming.initialcontext;

Import javax.naming.namingexception;

Import java.util.hashtable;

Public class resolve2 {public static void main (string argv []) {

// the user limited company a file to lookup

IF (argv.length! = 1) {

System.err.Println ("USAGE: JAVA Resolve2");

System.exit (-1);

}

// Here We Use the File System Service Provider

Hashtable env = new hashtable ();

Env.put (Context.initial_Context_Factory, "com.sun.jndi.fscontext.fscontextfactory);

Env.put (Context.Provider_URL, Argv [0]);

Try {

// Create the Initial Context

CONTEXT CTX = New InitialContext (ENV); namingenumerative ne = ctx.listbindings ("");

While (ne.hasmore ()) {

Binding b = (binding) ne.next ();

System.out.println (B.GetName () " B.GetObject ());

}

// close the context

CTX.Close ();

} catch (namingexception e) {

System.err.Println ("Problem Looking Up" Argv [0] ":" E);

}

}

}

Method for running this program: java resolve2 file: /// uddi

转载请注明原文地址:https://www.9cbs.com/read-88000.html

New Post(0)