java naming and directory interface
DESCRIPTION
Java Naming and Directory Interface . JNDI. Topics. Naming and Directory Services JNDI Overview Features and Code Samples JNDI Providers References. Naming Service. Associate names with objects (bind) Retrieve objects by their name (lookup) Examples: RMI Registry - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/1.jpg)
Java Naming and Directory Interface
JNDI
![Page 2: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/2.jpg)
v001025 JNDI 2
Topics
Naming and Directory ServicesJNDI OverviewFeatures and Code SamplesJNDI ProvidersReferences
![Page 3: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/3.jpg)
v001025 JNDI 3
Naming Service
Associate names with objects (bind)Retrieve objects by their name (lookup)Examples: RMI Registry CORBA Naming Service (COSNaming) Domain Name Service (DNS) File system
![Page 4: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/4.jpg)
v001025 JNDI 4
Contexts
Contains a set of bindings and lookup operationsHas its own naming conventionExamples File system directory: “/” DNS domain: “edu” LDAP: “c=us”
![Page 5: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/5.jpg)
v001025 JNDI 5
Names
Logical identifier assigned to an objectNaming convention defined by Naming SystemExamples /dir/dir2/file for UNIX drive:\dir\string for DOS cn=dan, o=ISP for LDAP www.cise.ufl.edu for DNS
![Page 6: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/6.jpg)
v001025 JNDI 6
Names (Cont)
Atomic Name name used in a binding Ex. filename in a directory
Compound Name sequence of atomic names /usr/bin/ls
![Page 7: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/7.jpg)
v001025 JNDI 7
Names (Cont)
Composite Nameapplies multiple naming systemshttp://www.apl.jhu.edu/~weimer/
URL scheme id: httpDNS: www.apl.jhu.eduUNIX and webserver: /usr/weimer/public_html
![Page 8: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/8.jpg)
v001025 JNDI 8
Directory Service
Can obtain an object with attributes and search for objects based on attributesCurrent Directory Systems X.500 - ISO standard. Complex. LDAP - Simplified X.500 over TCP/IP NDS - Novell Directory Services. NIS - Directory service for Solaris
![Page 9: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/9.jpg)
v001025 JNDI 9
Attributes
Common LDAP Attributesc = Countryo = Organizationou = Organizational Unitcn = Common Name (typically first or full name)sn = User’s surname
![Page 10: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/10.jpg)
v001025 JNDI 10
Naming and Directory Targets
Resourcesprintersmachines
UsersJava Objects !
![Page 11: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/11.jpg)
v001025 JNDI 11
Naming Vs. Directory
Naming Services simply assign logical names to addresses or objects localhost => 127.0.0.1 white pages
Directory Services add attributes and attribute-based searching find all hosts that are Sparc Solaris and have available disk
space yellow pages
![Page 12: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/12.jpg)
v001025 JNDI 12
JNDI Goals
Provide a consistent API to access different naming and directory services. Clients learn one API.
Different naming and directory systems can be combined into one logical systemNew naming service implementations can be added with no client modifications
![Page 13: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/13.jpg)
v001025 JNDI 13
JNDI Architecture Overview
JNDI API
JNDI Service Provider Interface
Java Client Application
JNDI Naming Manager
DNS LDAP CORBARMI Anything
![Page 14: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/14.jpg)
v001025 JNDI 14
JNDI Architecture
Application Programming Interface (API)API for client programmerUnifying interface
Service Provider Interface (SPI)For vendors enabling JNDI access to their
naming/directory service
![Page 15: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/15.jpg)
v001025 JNDI 15
JNDI API
Included in Java 2 SDK v 1.3Have to download for JDK 1.1 and SDK 1.2Packages javax.naming javax.naming.directory javax.naming.event javax.naming.ldap javax.naming.spi
![Page 16: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/16.jpg)
v001025 JNDI 16
Service Providers
Implementation for underlying productsIncluded in Java 2 SDK 1.3LDAPCOSNamingJNDI over RMI Registry
Have to download for previous versionshttp://java.sun.com/products/jndi/
![Page 17: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/17.jpg)
v001025 JNDI 17
Common JNDI Tasks
Obtain reference to initial contextContext OperationsList children of a contextBind names to objectsLookup objects via name Create/destroy contextsNote: NamingException can be thrown
from most Context operations
![Page 18: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/18.jpg)
v001025 JNDI 18
JNDI Contextsjavax.naming.Context
bind(String name, Object obj)createSubcontext(String name) : Contextlist(String name) : NamingEnumerationlistBindings(String name) : NamingEnumerationlookup(String name) : Object
<<abstract>>
javax.naming.InitialContext
InitialContext()InitialContext(java.util.Hashtable env)
![Page 19: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/19.jpg)
v001025 JNDI 19
Obtain Initial Context
import javax.naming.*;
java.util.Properties props = new java.util.Properties();props.put(Context.INITIAL_CONTEXT_FACTORY, “com.sun.jndi.fscontext.RefFSContextFactory”);props.put(Context.PROVIDER_URL, “file:///”);
Context initContext = new InitialContext( props );
![Page 20: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/20.jpg)
v001025 JNDI 20
Initial Context
Starting point in the namespaceAll operations performed are relative to the initial contextSpecify service provider with property
props.put(Context.INITIAL_CONTEXT_FACTORY”,“com.sun.jndi.fscontext.RefFSContextFactory” );
![Page 21: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/21.jpg)
v001025 JNDI 21
Initial Context (Cont)Specify provider-specific propertiesLDAPprops.put(Context.PROVIDER_URL, “ldap://host:port”);props.put(Context.SECURITY_PRINCIPAL, “user” );props.put(Context.SECURITY_CREDENTIALS, “password”);
File Systemprops.put(Context.PROVIDER_URL, “file://tmp/”);
CreateInitialContext initContext = new InitialContext( props );
![Page 22: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/22.jpg)
v001025 JNDI 22
List childrenNamingEnumeration children = initContext.list(“”);while( children.hasMore() ){
NameClassPair nc = (NameClassPair) children.next();System.out.println(nc.getName());System.out.println(nc.getClassName());
}
list() returns a NamingEnumeration of NameClassPair objectslistBindings() returns a NamingEnumeration of Binding objects
![Page 23: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/23.jpg)
v001025 JNDI 23
Binding Names to an Object
Bind name to object within a context
File f = new File(“/tmp/dan”);tmpDirContext.bind(“dan”, f );
NameAlreadyBoundException occurs if “dan” is already bound in the tmp contextCan use unbind() or rebind()
![Page 24: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/24.jpg)
v001025 JNDI 24
Object Lookup
Lookup object in a contextString name; Object o =
initContext.lookup(name);name can be compound
“/usr/tmp/dir/myfile”Filesystem provider returns
File for files
![Page 25: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/25.jpg)
v001025 JNDI 25
Attributes
DirContext contains an object with attributes for each contained bindingCommon LDAP Attributesc = Countryo = Organizationou = Organizational Unitcn = Common Name (typically first or full name)sn = User’s surname
![Page 26: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/26.jpg)
v001025 JNDI 26
Basic SearchSpecify a set of attributes that an entry must have. Specify required attribute valuesSpecify attributes to returnsearch(String name, Attributes match, String[]
retAttrIds )
Returns NamingEnumeration of SearchResults
![Page 27: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/27.jpg)
v001025 JNDI 27
Storing objects
Object Types to storeJava Serializable ObjectReferenceable Java ObjectRemote Objects like RMI and CORBA
objects
![Page 28: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/28.jpg)
v001025 JNDI 28
Federation
Composite names span multiple namespacesou=People/tmp/myFileFile f = (File)
ctx.lookup(“ou=People/tmp/myFile”);
lookup on this name will traverse through LDAP and the Filesystem provider to retrieve the file
![Page 29: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/29.jpg)
v001025 JNDI 29
JNDI Providers
LDAP com.novell.naming.service.nds.NdsInitialContextFactory
NIS com.sun.jndi.nis.NISCtxFactory
RMI com.sun.jndi.rmi.registry.RegistryContextFactory
COSNaming ( CORBA Naming System )Files com.sun.jndi.fscontext.[Ref]FSContextFactory
![Page 30: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/30.jpg)
v001025 JNDI 30
Setup Requirement
For 1.3, essentially nothing. JNDI should be included in the distribution along with the 3 providersFor Java 2 SDK version 1.2 jndi.jar in $JAVA_HOME/jre/lib/ext
For jdk 1.1Add jndi.jar to the CLASSPATH
![Page 31: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/31.jpg)
v001025 JNDI 31
Summary
Provide a consistent API to access different naming and directory services. Clients learn one API.
Different naming and directory systems can be combined into one logical systemNew naming service implementations can be added with no client modifications
![Page 32: Java Naming and Directory Interface](https://reader036.vdocuments.mx/reader036/viewer/2022062315/56815d4c550346895dcb54ea/html5/thumbnails/32.jpg)
v001025 JNDI 32
ResourcesJNDI API Tutorial and Reference, Rosanna Lee and Scott Seligman. Addison Wesley, 2000Main JNDI Page http://java.sun.com/products/jndi/
Java Lecture Source http://www.apl.jhu.edu/~jcstaff/ejava/ejava_whatsnew_f00.htm
Java Online Tutorial http://java.sun.com/docs/books/tutorial/