JNDI est système d’annuaire générique. Il sait gérer “out-of-box” : LDAP, DNS, NIS, NDS, RMI et CORBA.
packages JNDI
javax.naming
Manipulation des associations.
javax.naming.directory
Manipulation des répertoires.
javax.naming.event
Traitement des évènements en provenance de l’annuaire.
javax.naming.ldap
API spécialisée pour les annuaires compatibles LDAP.
javax.naming.spi
API de programmation des pilotes JNDI.
Bind d’un objet
1234567891011121314151617181920212223242526
importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.naming.NamingException;publicclassBind{publicstaticStringBIND="drazzib/test";publicstaticvoidmain(String[]args){Objectobject="DATA";try{ContextinitialContext=newInitialContext();initialContext.bind(BIND,object);System.out.println("Bound object to name: "+BIND);}catch(NamingExceptione){System.err.println("Unable to bind ("+object+") to the name '"+BIND+"'");e.printStackTrace();}}}
Lookup d’un objet
1234567891011121314151617181920212223242526272829
importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.naming.NamingException;publicclassLookup{publicstaticvoidmain(String[]args){try{ContextinitialContext=newInitialContext();Objectobject=initialContext.lookup(Bind.BIND);if(objectinstanceofjava.lang.String){Strings=(String)object;System.out.println("Looked up this object: "+s);}else{System.err.println("Object is not of type "+"java.lang.String");}}catch(NamingExceptione){System.err.println("Unable to find an object bound to the name "+Bind.BIND);}}}