JNDI Introduction & Simple Example

zhaozj2021-02-12  164

JNDI: The Java Naming and Directory Interface

What is JNDI?

The Java Naming and Directory Interface is a unified API interface that accesss different names and directory services.

Different services use different name formats.

Java programs need to access databases, files, directories, objects, and networks in the same format.

JNID has two partial interfaces: an application interface and an interface for providing services. Use the API to access the name or directory service in a new service, using the SPI to provide services.

JNDI structure

Naming Services

The name service provides a method, mapping identifier to entities or objects.

Several basic terms you need to know:

Binding: Binding is to link an indivisible name ("Atom" name) with an object. Like DNS, we use the name www.yahoo.com to connect with IP address 216.32.74.53, and a file object is linked with file name AFILE.TXT.

Name space; name space contains a set of names, but each name in the namespace is unique. A file directory is a simple name space, such as the directory C: / Temp, in this directory, can not have two files of the same name, but two files in different directories may have the same name.

Composite Name: Compound name is the only name made of namespace, with one or more "atom" names, depending on the namespace where the name is located. The file path is a composite name. For example, we can see it with C: /TEMP/myfile.txt, we can see, this name is by the root name (C: /), temporary directory name (TEMP) and a file name (MyFile.txt ) Make up, these three names are complicated to represent a unique name.

Combined name: The combination name can span multiple namespaces. A URL is a combination name. If you see http://www.npu.edu/index.htm, you use the HTTP service to connect to the server, then use another name space /index.htm to access a file.

Directory service

Directory Services provides a set of distributive directory objects with searchable capabilities.

The object stored in the directory service can be any object that can be described in a set of attributes, each of which can describe the ability of the object via a set of properties. For example, a Person object may have attributes such as Height, Hair Color, Age, SEX. Directory services can also provide the ability to search as required. If we can use the Person's AGE attribute, search the Person object between 20-25 years, the directory service will return the eligible persons object. This is often referred to as a content-based search.

Use JNDI in the client:

u Create an instance of java.util.hashtable or java.util.Properties.

u Add variable to the HashTable or Properties object:

JNDI Class class name provided by Naming Server.

Contains the URL of the Aming Server position.

Safety trust.

U Create an initialcontext object via a HashTable or Properites or JNDI property file.

Example:

Import java.util. *;

Import javax.naming. *;

...

Env.put (Context.initial_Context_Factory, "WebLogic.jndi.wlinitialContextFactory"); env.put (Context.Provider_URL, "T3: // localhost: 7001");

InitialContext CTX = New InitialContext (ENV);

The corresponding constant of the environment variable shows the java.naming.factory.initial context.initial_context_factory context factory name, given by the service provider. Java.naming.provider.URL Context.Provide_URL initializes the address. Java.naming.security. Principal context.security_principal service user information. Java.naming.security. Credentials context.security_credential password.

More configuration examples:

Hashtable env = new hashtable ();

env.put (Context.Initial_Context_Factory,

"WebLogic.jndi.wlinitialContextFactory");

Env.put (Context.Provider_URL, "T3: // localhost: 7001");

Env.put (Context.Security_Principal, "System");

Env.put (Context.Security_credentials, "Password Here");

Properties env = new property ();

Env.setProperties ("java.naming.factory.initial",

"WebLogic.jndi.wlinitialContextFactory");

Env.SetProperties ("java.naming.provider.URL", "T3: // localhost: 7001");

Env.setProperties ("java.naming.security.principal", "Tommy");

Env.SetProperties ("java.naming.security.credentials", "password here");

Create InitialContext:

Class name: javax.naming.initialcontext

Interfaces That IT ITEments: javax.naming.context

Constructionors:

PublicinTialContext ();

Public InitialContext (HashTable Configuration);

Public InitialContext (Properties Configuration);

All of the above methods may throw Namingexception.

An example of a binding:

Public static initialcontext getinitialcontext () throws namingexception {

Hashtable env = new hashtable ();

env.put (Context.Initial_Context_Factory,

"WebLogic.jndi.wlinitialContextFactory"); Env.Put (Context.Provider_URL, "T3: // localhost: 7001");

InitialContext Context = New InitialContext (ENV);

Return context;

}

// Obtain the initial context

InitialContext InitialContext = GetInitialContext ();

// CREATE A Bank Object.

Bank mybank = new bank ();

// bind the object inTo the JNDi Tree.

InitialContext.Rebind ("Thebank", MyBank;

An example of a LOOKUP:

Public static initialcontext getinitialcontext () throws namingexception {

Hashtable env = new hashtable ();

env.put (Context.Initial_Context_Factory,

"WebLogic.jndi.wlinitialContextFactory");

Env.put (Context.Provider_URL, "T3: // localhost: 7001");

InitialContext Context = New InitialContext (ENV);

Return context;

}

// Obtain the initial context

InitialContext InitialContext = GetInitialContext ();

// Lookup an existing bank object.

Bank Mybank = (Bank) InitialContext.lookup ("THEBANK");

Possible Namingexception:

AuthenticationException

CommunicationException

INVALIDNAMEEXCEPTION

NameNotFoundException

NOINITIALCONTEXTEXCEPTION

Enumerate all name objects:

Namingenumeration Declaration:

Public interface namingenumerative extends enumeration {

Public boolean hashmore () throws namingexception;

Public Object next () throws namingexception;

Public void close () throws namingexception; // jndi 1.2

}

Try {

...

Namingenumeration enum = ctx.list ("");

While (enum.hasmore ()) {

NameClasspair NCP = (NameClasspair) Enum.next ();

System.out.println ("JNDI Name IS:" NCP.GETNAME ());

}

}

Catch (Namingexception E) {...}

Last example:

Import java.util. *;

Import javax.naming. *;

Import javax.naming.directory. *;

Import java.io. *;

Public class listall {public static void main (java.lang.string [] args) {

Hashtable env = new hashtable ();

env.put (Context.Initial_Context_Factory,

"WebLogic.jndi.wlinitialContextFactory");

Env.put (Context.Provider_URL, "T3: // localhost: 7001");

Try {

InitialContext CTX = New InitialContext (ENV);

Namingenumeration enum = ctx.listbindings ("");

While (enum.hasmore ()) {

Binding binding = (binding) enum.next ();

Object obj = (object) binding.getObject ();

System.out.println (OBJ);

}

} catch (namingexception e) {

System.out.println (e);

}

} // end main

} // end list

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

New Post(0)