Back in 2001 I was giving talks on how to use Java Naming and Directory Interface effectively.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
– Application Service Provider for the Non-Profit industry
– Pure Java development– Http://www.4charity.com– Locations:
• San Francisco,CA • Honolulu, HI
– We’re Hiring…
Copyright 2000 SyncTank Solutions, Inc.
Why JNDI?You can:• Develop networked application.• Find remote objects.• Interface with distributed systems.• Interact with non-Java, networked
systems
3
Copyright 2000 SyncTank Solutions, Inc.
Presentation Goals• Review of basic JNDI.• Using JNDI for local service resolution.• JNDI as an alternative to static
constants.• Using Custom Contexts with JNDI.• Implementing Singletons.• Using RMI and IIOP.• Using Events in JNDI.
Copyright 2000 SyncTank Solutions, Inc.
Who needs JNDI?• If you want to use J2EE, you need
JNDI.• JNDI is the standard way to access
distributed and local resources for enterprise applications.
4
Copyright 2000 SyncTank Solutions, Inc.
What is JNDI?• JNDI is a set of interfaces.• Provides a programming interface,
allowing for Discovery and Registration.• Standards-based approach, providing
common API to a searchable structure of objects.
• Naming Services• Directory Services
Copyright 2000 SyncTank Solutions, Inc.
What is a Naming Service?• Provides the ability to map a name or
identifier to objects or services.• A tree-like structure, nodes can be
associated with other nodes or objects.• Objects are bound into the JNDI tree.• Objects can be resolved by performing
a lookup using the composite name.
5
Copyright 2000 SyncTank Solutions, Inc.
Naming
JNDI Tree lives in the Server as a collection of
Named Object References
Object/ Service
Reference
Copyright 2000 SyncTank Solutions, Inc.
What is a Directory Service?• Provides a structure containing objects.• Allows access to object attributes.• Enables searching the structure using
these attributes.• Think of a computer’s file system or an
SQL database.• Simple JNDI has one attribute, the
object’s name.
6
Copyright 2000 SyncTank Solutions, Inc.
JNDI is Like a Database• Start with a database instance.• Use a tablespace or ownership area.• Access a table in the tablespace.• Access a row in the table.• Access a column in the row.
sqlplus scott@beq-local
“SELECT name FROM scott.emp;”
Copyright 2000 SyncTank Solutions, Inc.
JNDI is Like a File System
• Start with a mounted drive.• Use a subdirectory.• Access a subdirectory.• Access a file.
“C:/data/your_client/financials.xls”
7
Copyright 2000 SyncTank Solutions, Inc.
Using JNDI• Start with an Initial Context.• Navigate to a Sub-Context.• Drill-down through other sub-contexts.• Access the object or service.
new InitialContext().lookup(“name”);
Copyright 2000 SyncTank Solutions, Inc.
Comparisons
JNDI File System RDBMS
Initial Context Mount Point (c:\)Database instance
SubContext Subdirectory TablespaceSubContext Subdirectory TableObject File Data (row)rmi://myserver:8080/myobjects.example.object
c:\data\your_client\financials.xls
SELECT * FROM Demo.Employee
8
Copyright 2000 SyncTank Solutions, Inc.
JNDI Examplepackage com.synctank.labs.jndi;import javax.naming.*;public class TestJNDI {
public static void main(String _arg[]){Object o = null; InitialContext ctx= null;try{
ctx = new InitialContext(System.getProperties());o = ctx.lookup("myobjects.example.object");String s = (String)o; System.out.println("Found a string: "+s);
} catch (ClassCastException e) {System.out.println("Found a "+o.getClass().getName() +": "+o.toString() );
} catch (NamingException ne) {System.out.println("We have a problem!"); ne.printStackTrace();
} finally {try { ctx.close(); }catch (Exception e ) {}
}} }
Copyright 2000 SyncTank Solutions, Inc.
JNDI Example• Primary classes are located in the
javax.naming package.• Primarily use Context objects:
– javax.naming.Context
– javax.naming.InitialContext
– javax.naming.directory.InitialDirContext
– javax.naming.ldap.InitialLdapContext
• Use a context that matches your needs.
9
Copyright 2000 SyncTank Solutions, Inc.
JNDI Providers• JNDI Providers are the services that will be
accessed.
• More on the Service Provider Interface Later.
• Most application servers come with some Naming service.
• J2SDK v. 1.3 includes code for many interfaces.
• Many are downloadable, for free, at http://java.sun.com/products/jndi/serviceproviders.html#12
Copyright 2000 SyncTank Solutions, Inc.
Starting the Service• FS service doesn’t need to be started!• RMI Registry (JNDI Service)
“ejs://ejs.ejs.com:666");Context ctx = new InitialContext(p);SkinnyAppp app = (SkinnyApp)ctx.lookup(“SkinyApp.fore”);
13
Copyright 2000 SyncTank Solutions, Inc.
Resolving Objects• Once you have a context any lookup is a relative search.• user.russell.castagnaro.wallet can be resolved from:
– The initial context as ctx.lookup(“user.russell.castagnaro.wallet”);
– A subcontext “user.russell” as ctx.lookup(“castagnaro.wallet”);
• Access a sub-context using the lookup method from any context.Context ctx = new InitialContext(p); Context sub = ctx.lookup("user");sub = sub.lookup("russell"); sub = sub.lookup(”castagnaro");Object o = sub.lookup("wallet");
Copyright 2000 SyncTank Solutions, Inc.
JNDI Perusal• It is possible to dynamically access all of
the objects bound into the JNDI tree with two overloaded methods:– public abstract NamingEnumeration list(java.lang.String) throws
NamingException;– public abstract NamingEnumeration list(javax.naming.Name) throws
NamingException;– public abstract NamingEnumeration listBindings(java.lang.String) throws
NamingException;– public abstract NamingEnumeration listBindings(javax.naming.Name)
throws NamingException;
• There is a very large difference between the two methods!
14
Copyright 2000 SyncTank Solutions, Inc.
JNDI Perusal
• listBindings()– Returns an enumeration of Binding objects.– Bindings contain the actual object* from the tree!– May take a long time!
• list()– Returns an Enumeration of NameClassPair objects.– Access to the Name and class, but not the object.– Faster than the alternative, like a ‘shallow get.’
* Remote objects are an exception.
Copyright 2000 SyncTank Solutions, Inc.
JNDI Perusal Example Codepublic static void list(Context ctx,StringBuffer sb) {
Hashtable ht = new Hashtable();ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");ht.put(Context.PROVIDER_URL, "t3://localhost:7001");try {
Context ic = new InitialContext(ht);//serializable, an actual copy put into the treeic.bind("Object1",new String("Object1"));ic = ic.createSubContext(“foo”);//remote, a copy of the stub placed in the treeic.bind("Trivial",new TrivialImpl("Trivial!"));
Object o = Class.forName(className).newInstance();Context ctx = new InitialContext();ctx.rebind(jndiName,o);log("StartupServlet has bound "+ className +" into”+ the JNDI tree as "+ jndiName);
} catch (Exception ne) {throw new ServletException(ne);
}}
20
Copyright 2000 SyncTank Solutions, Inc.
Singletons• You need one and only one instance of
a particular class.• You want to provide access to
distributed applications/ services.• Don’t make static methods! Make the
class an RMI object.• Bind the object’s implementation into
the JNDI tree.• Essentially what happens for EJBHome
interfaces.
Copyright 2000 SyncTank Solutions, Inc.
CORBA Objects• When accessing CORBA objects, things
used to be more complicated.• RMI over IIOP, included in Java 2,
solves this problem.• Narrowing the object maps it from a
CORBA object to a Java object auto-magically.
21
Copyright 2000 SyncTank Solutions, Inc.
Narrowing• If you access services using RMI over
IIOP (WebSphere, Iona, etc.).• If you use HomeHandle or Handle
objects (EJB 1.1). • Use PortableObject.narrow()• Pass an Object and the class you
expect from the remote method call.
Copyright 2000 SyncTank Solutions, Inc.
Class NarrowingObjectOutputStream stream = ...;Account account = ...;Handle handle = account.getHandle();stream.writeObject(handle);// A client can read the handle from stable storage, and use the
Events• JNDI Events require the use of a different
type of context.• New Context interfaces:
– EventContext for standard naming services
– EventDirContext for LDAP service
• Access Contexts in a familiar way:EventDirContext edc = (EventDirContext)
(new InitialDirContext(env).lookup("ou=People"));
24
Copyright 2000 SyncTank Solutions, Inc.
JNDI Gotchas• Use rebind() wisely.
• Know the URL and InitialContextFactory class name.
• When using advanced features (LDAP, etc.) place the correct resources in your classpath..\lib\rmiregistry.jar;.\lib\providerutil.jar;
.\lib\jndi.jar;.\lib\jaas.jar;.\lib\ldap.jar;
.\lib\ldapbp.jar;.\lib\fscontext.jar
• J2EE has made deploying JNDI services much easier, look for vendors to support advanced features 2Q 2000.
Copyright 2000 SyncTank Solutions, Inc.
Review• JNDI is used for all advanced J2EE
features.• JNDI provides an easy method of accessing
remote or local resources.• Use bind() or rebind() to register objects.• Use lookup() to resolve objects.• Use your server vendors startup facilities for
initial JNDI registration.
25
Copyright 2000 SyncTank Solutions, Inc.
Finally• Thanks for attending.• Live in Hawaii or Bay Area? Do you like
this stuff? Email your resume to us!• Source Code Available: