310 likes | 461 Vues
JNDI Unleashed. Peter Fischer Director of Technical Services. Java Conference 2000 March 29, 2000. JNDI API. Standard access for Java programs to naming and directory services and features abstraction over naming and directory services APIs
E N D
JNDI Unleashed Peter Fischer Director of Technical Services Java Conference 2000 March 29, 2000
JNDI API • Standard access for Java programs to naming and directory services and features • abstraction over naming and directory services APIs • Provides a key integration point between Java and enterprise naming and directory service middleware • Allows Java applications to access multiple directory services independent of implementation • Using JNDI applications can store and retrieve named Java objects of any type • Provides methods for performing standard directory operations • associating attributes with objects • searching for objects using those attributes as search keys • Consists of two API sets • JNDI API - application access to naming and directory services • JNDI SPI - interface that vendors use to wrap their implementation to conform to JNDI
JNDI API JNDI Architecture Java Applications JNDI Implementation Manager JNDI SPI CORBA COS LDAP RMI NDS WebLogic
Naming Service • Naming Service • Map names to objects or object references • Maps a logical, “easy-to-read” name to a physical endpoint Naming Service Logical Name Physical Name CustomerData URL=jdbc:odbc:customer ColorPrinter machine:xxx.yyy.zzz.aaa Database CustomerDatabase; Printer myPrinter; CustomerDatabase = lookup (“CustomerData”); myPrinter = lookup (“ColorPrinter”);
Directory Service • Directory Service • Extends a naming service by allowing association of attributes with an object • Allows lookup based upon attribute values Directory Service Attr. 1 // Search for objects based on attributes // Modifying attributes on directory // Store objects in directory Attr. 2 Dir Object Attr. 3 Attr. 4
Definitions • NameSpace • Set of names that are all unique • Compound Name • Sequence of naming elements that conform to the naming convention of a namespace • Composite Name • A name that spans multiple namespaces • Service Provider • Provides naming and directory functionality and supports the JNDI SPI
LDAP • Provides a light-weight version of the X.500 directory service that runs on TCP/IP. • Allows networked users access to information and resources in a simple and clean manner • Prides simple searching facility that allows you to search and modify entries based upon their position in the hierarchy – context • Attributes can be associated with entries -
JNDI Packages javax.naming classes and interfaces for accessing naming services. javax.naming.directory classes that extend javax.naming to provide access to directories javax.naming.event Classes and interfaces that support event notification in naming and directory services javax.naming.ldap Classes and interfaces that support LDAP v3 extensions and controls javax.naming.spi Classes and interfaces that support the addition of new services providers
JNDI Constructs • Context • Specifies naming context operations • Interface that is implemented by InitialContext and InitialDirContext classes • NameClassPair • Contains an object’s name and the name of the object’s class • Binding • Contains the name of the bound object, name of the object’s class, and the object • Reference • Represents an object that is not stored in the directory service • JNDI Environment • Contains the setting for JNDI run-time • Directory Object • Represents the variety of information in a computing environment • Provides operations for creating attributes, adding, removing and modifying attributes associated with the directory object
JNDI Environment Variables • Environment properties can be specified in: • Hashtable that is programmatically setup • Read in from a stored JNDI resource file that stores name/value pairs • Application resource files located in CLASSPATH • Environment is passed into InitialContext and InitialDirContext constructors
Working with References Customer customer; CustomerDatabase = lookup (“CustomerData”); customer = (Customer)lookup (“Fred Jones”); Naming Service locate Logical Name Physical Name Fred Jones physical address reference … … Instantiate Object Repository
NamingEnumeration Context Name Naming Package InitialContext CompositeName CompoundName NameClassPair Binding
Programming Model • Read in Environment • Configuration of the JNDI environment • Everything starts with a Context • Either InitialContext or DirContext • Use Context to add, delete, rename and move entries
JNDI Context Operations // Step 1 - Setup Environment using File System provider Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put (Context.PROVIDER_URL, "file:/company"); // Step 2 - Get InitialContext Context ctx = new InitialContext (env); // Step 3 – Add an object File myFile; // Create the physical file and add info Ctx.bind (“marketing/brochure.txt”, myFile); // Step 4 – Locate an object File myFile = (File)ctx.lookup (“marketing/brochure.txt”); Setup the environment for a specific service provider Create the initial context Uses the jndi environment Adding a file uses bind method Locating a specific file uses lookup method
JNDI Context Operations // Step 1 - Setup Environment Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put (Context.PROVIDER_URL, "file:/company"); // Step 2 - Get InitialContext Context ctx = new InitialContext (env); // Rename an object ctx.rename ("Feb", "Feb2000"); // Create an object ctx.createSubcontext ("Feb2000"); // Delete an object ctx.unbind ("Feb2000"); Setup the environment for a specific service provider Create the initial context Renaming a specific file uses bind method Creating a file directory uses createSubcontext method Deleting a file uses unbind method
JNDI Context Operations – List Entries // Step 1 - Setup Environment Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put (Context.PROVIDER_URL, "file:/company"); // Step 2 - Create initial context Context ctx = InitialContext(env); // Step 3 - setup list criteria NamingEnumeration list = ctx.list (“presentations"); // Step 4 - Process List while (list.hasMore()) { NameClassPair entry = (NameClassPair)list.next(); System.out.println(entry); } Setup the environment for a specific service provider Create the initial context Uses the jndi environment Use the list method to return a NamingEnumeration for a certain directory Each entry in NamingEnumeration is a NameClassPair
JNDI Context Operations – List Bindings // Step 1 - Setup Environment Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put (Context.PROVIDER_URL, "file:/company"); // Step 2 - Create initial context Context ctx = InitialContext(env); // Step 3 - setup list criteria NamingEnumeration bindings = ctx.listBindings ("presentation"); // Step 4 - Process List while (list.hasMore()) { Binding binding= (Binding)bindings.next(); System.out.println(binding.getName() + ":" + binding.getObject()); } Setup the environment for a specific service provider Create the initial context Uses the jndi environment Use the listBindings method to return a NamingEnumeration for a certain directory Each entry in NamingEnumeration is a Binding
Service Provider Differences // Step 1 - Setup Environment Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put (Context.PROVIDER_URL, “ldap://marketing.qes.com"); // Step 2 - Get InitialContext Context ctx = new InitialContext (env); // Step 3 – Add an object File myFile; // Create the physical file and add info Ctx.bind (“cn=marketing/brochure.txt”, myFile); // Step 4 – Locate an object File myFile = (File)ctx.lookup (“cn=marketing/brochure.txt”); // Step 1 - Setup Environment Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put (Context.PROVIDER_URL, "file:/company"); // Step 2 - Get InitialContext Context ctx = new InitialContext (env); // Step 3 – Add an object File myFile; // Create the physical file and add info Ctx.bind (“marketing/brochure.txt”, myFile); // Step 4 – Locate an object File myFile = (File)ctx.lookup (“marketing/brochure.txt”);
JNDI and JDBC - Storing JDBC URLs // Step 1 - Setup Environment env.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.T3InitialContextfactory"); env.put (Context.PROVIDER_URL, "t3://marketing:7001"); env.put (Context.SECURITY_PRINCIPAL user); env.put (Context.SECURITY_CREDENTIALS, password); // Step 2 - Create initial context Context ctx = InitialContext(env); // Step 3 - Store the URL using JNDI String strJDBCUrl = new String; strJDBCUrl = "jdbc:inetdae:marketing:1433database=marketing"; ctx.bind ("MarketingDB", strJDBCUrl); Using weblogic jndi provider Store the URL using MarketingDB as logical name
JNDI and JDBC - Retrieving JDBC URLs // Step 1 - Setup Environment env.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.T3InitialContextfactory"); env.put (Context.PROVIDER_URL, "t3://marketing:7001"); env.put (Context.SECURITY_PRINCIPAL user); env.put (Context.SECURITY_CREDENTIALS, password); // Step 2 - Create initial context Context ctx = InitialContext(env); // Step 3 - Retrieve the URL using JNDI String strJBDCUrl = new String; strJDBCUrl = (String)ctx.lookup ("MarketingDB"); // Step 4 - Open the database Connection con = DriverManager.getConnection (strJDBCUrl, env.SECURITY_PRINCIPAL env.SECURITY_CREDENTIALS); Using weblogic jndi provider Lookup the MarketingDB URL Pass the URL to JDBC To create connection
DirContext Context java.io.Serializable Attribute Attributes Directory Package BasicAttribute BasicAttributes ModificationItem SearchControls InitialContext InitialDirContext NameClassPair Binding SearchResult
Read Attributes from a Directory Entry // Step 1 - Setup Environment env.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.T3InitialContextfactory"); env.put (Context.PROVIDER_URL, "t3://marketing:7001"); env.put (Context.SECURITY_PRINCIPAL user); env.put (Context.SECURITY_CREDENTIALS, password); // Step 2 - Create initial context Context dirCtx = InitialDirContext(env); // Step 3 - setup list criteria Attributes attrList = dirCtx.getAttributes (“cn=name“, “ou=marketing”); // Step 4 - Process List while (attrList.hasMore()) { Attribute attr = (Attribute )attrList.next(); for (NamingEnumeration ne = attr.getAll() ; ne.hasMore(): System.out.println( “value: “ + ne.next() ) ); } Use getAttributes to get attributes for an entry Use next and hasMore to loop through attributes
Search Entries by Attributes // get input from user String strLastName; strLastName = … // Step 1 – Specify attributes to match // Search by Last Name Attributes matchAttrs = new BasicAttributes (true); matchAttrs.put(new BasicAttribute(“sn”, strLastName) ); // Step 2 – Search for objects within a context NamingEnumeration results = ctx.search(“ou=Marketing”, matchAttr); // Step 3 - Process List while (result.hasMore()) { SearchResult result = (SearchResult )result.next(); System.out.println( “name: “ + result.getName()) ); printAttributes (result.getAttributes()); } Use search and pass the attribute to search on Use next and hasMore to loop through results
Working with Events • Uses an event model similar to that of AWT and JavaBeans • Can be used for the following • Notification of namespace changes • adding, changing, renaming objects • Notification of object changes • Changing object
ObjectChangeListener NamespaceChangeListener java.io.Serializable EventContext NamingListener Event Package Java.util.EventObject NamingEvent • Uses an event model similar to that of AWT and JavaBeans • Can be used for the following • Notification of namespace changes • adding, changing, renaming objects • Notification of object changes • Changing object
NamespaceChangeListener Context Object Event Event ObjectChangeListener Working with Events NamingListeners Register Register
Wrap Up • JNDI is full-featured • We touched the tip of the iceberg • Storage of objects • Can store all types of objects • Location Service for system objects • Store JBDC URLs • EJB • Event support provide event-driven programming and notifications • Create “proactive environments” that respond to events
Questions Peter Fischer Director of Technical Services 115 Route 46 Suite A4 Mountain Lakes, New Jersey 07046 Phone - 973-263-0722 email - pfischer@qtrg.com