Java Naming and Directory Interface - PowerPoint PPT Presentation

1 / 65
About This Presentation
Title:

Java Naming and Directory Interface

Description:

Has its own naming convention. Examples. Filesystem directory: '/' DNS domain: 'edu' ... only meaningful in a context. Ex. filename in a directory. Compound ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 66
Provided by: daniel85
Category:

less

Transcript and Presenter's Notes

Title: Java Naming and Directory Interface


1
Java Naming and Directory Interface
  • JNDI

2
Topics
  • Naming and Directory Services
  • JNDI Overview
  • Features and Code Samples
  • JNDI Providers
  • Resources
  • References

3
Naming Service
  • Associate names with objects (bind)
  • Retrieve objects by their name (resolve)
  • filename gt bits on disk
  • spreadsheet A3 gt Contents of cell
  • Examples
  • RMI Registry
  • CORBA Naming Service (COSNaming)
  • Domain Name Service (DNS)
  • Filesystem

4
Contexts
  • Contains a set of bindings and lookup operations
  • Has its own naming convention
  • Examples
  • Filesystem directory /
  • DNS domain edu
  • LDAP cus
  • Naming system is a set of contexts (same type)
  • names in a particular system namespace

5
Names
  • Logical identifier assigned to an object
  • localhost gt 127.0.0.1
  • Naming convention defined by Naming System
  • /dir/dir2/file for UNIX
  • drive\dir\string for DOS
  • cndan, oISP for LDAP
  • aplcenmp.apl.jhu.edu for DNS

6
Names (Cont)
  • Atomic Name
  • name used in a binding
  • only meaningful in a context
  • Ex. filename in a directory
  • Compound Name
  • sequence of atomic names
  • /usr/bin/ls
  • conforms to naming convention of name space

7
Names (Cont)
  • Composite Name
  • Spans multiple naming systems
  • http//www.apl.jhu.edu/weimer/
  • URL scheme id http
  • DNS www.apl.jhu.edu
  • UNIX and webserver /usr/weimer/public_html
  • Resolution
  • Object o ctx.lookup(usr/weimer/public_html)

8
Directory Service
  • Directory Objects represent an object
  • Objects may be given attributes
  • Can obtain an objects attributes and search for
    objects based on attributes
  • Examples
  • X.500 - ISO standard. Complex.
  • LDAP - Simplified X.500 over TCP/IP
  • NDS - Novell Directory Services.
  • NIS - Directory service for Solaris

9
Directory Schema
  • Specifies object types that can be placed in the
    directory
  • schema checking can usually be turned off
  • RFC 2252 defines attribute syntaxes

10
Directory Service (Cont)
  • For a user
  • telephone number(s), email address
  • For a machine
  • operating system, memory, location Search Filter
  • All machines with gt 32M memory
  • Directories can be arranged in a hierarchy and
    therefore function as a naming system
  • Directory Objects implement NamingContext

11
Naming and Directory Usage
  • Resources
  • printers
  • machines
  • Users
  • Java Objects !

12
Naming Vs. Directory Summary
  • Naming Services simply assign logical names to
    addresses or objects
  • localhost gt 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
  • schema
  • yellow pages

13
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 system
  • New naming service implementations can be added
    with no client modifications

14
JNDI Architecture Overview
Java Client Application
JNDI API
JNDI Naming Manager
JNDI Service Provider Interface
DNS
LDAP
CORBA
RMI
Anything
15
JNDI Architecture
  • Application Programming Interface (API)
  • API for client programmer
  • Unifying interface
  • Service Provider Interface (SPI)
  • For vendors enabling JNDI access to their
    naming/directory service

16
JNDI API
  • Included in Java 2 SDK v 1.3
  • Have to download for JDK 1.1 and SDK 1.2
  • Packages
  • javax.naming
  • javax.naming.directory
  • javax.naming.event
  • javax.naming.ldap
  • javax.naming.spi

17
Service Providers
  • Implementation for underlying products
  • Included in Java 2 SDK 1.3
  • LDAP
  • COSNaming
  • JNDI over RMI Registry
  • Have to download for previous versions
  • http//java.sun.com/products/jndi/

18
Common JNDI Tasks
  • Obtain reference to initial context
  • Context Operations
  • List children of a context
  • Bind names to objects
  • Lookup objects via name
  • Create/destroy contexts
  • Note NamingException can be thrown from most
    Context operations

19
JNDI Contexts
20
Obtain Initial Context
  • import javax.naming.
  • java.util.Properties props new
    java.util.Properties()
  • props.put(Context.INITIAL_CONTEXT_FACTORY,
  • com.sun.jndi.fscontext.RefFSCont
    extFactory)
  • props.put(Context.PROVIDER_URL, file///)
  • Context initContext new InitialContext( props )

21
Initial Context
  • Starting point in the namespace
  • All operations performed are relative to the
    initial context
  • Specify service provider with property
  • props.put(Context.INITIAL_CONTEXT_FACTORY,
  • com.sun.jndi.fscontext.RefFSContextFactory )

22
Initial Context (Cont)
  • Specify provider-specific properties
  • LDAP
  • props.put(Context.PROVIDER_URL,
    ldap//hostport)
  • props.put(Context.SECURITY_PRINCIPAL, user )
  • props.put(Context.SECURITY_CREDENTIALS,
    password)
  • File System
  • props.put(Context.PROVIDER_URL, file//tmp/)
  • Create
  • InitialContext initContext new InitialContext(
    props )

23
List children
  • NamingEnumeration 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 objects
  • listBindings() returns a NamingEnumeration of
    Binding objects

24
NameClassPair and Binding
javax.naming.NameClassPair
Represents the name and
class of an object bound to
getClassName() String
a context
getName() String
Represents association
javax.naming.Binding
between a name and an
object
getObject() Object
25
NamingEnumeration
  • Extends java.util.Enumeration
  • Throws exception when no more entries in the
    enumeration
  • Allows partial results to be returned and
    exception throws upon access where the problem
    occurred
  • i.e. hasMore() will throw the exception
  • Limit of search with search controls

26
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 context
  • Can use unbind() or rebind()

27
Object Lookup
  • Lookup object in a context
  • String name Object o initContext.lookup(name)
  • name can be compound
  • /usr/tmp/dir/myfile
  • Separator is not standardized !
  • Class returned is up to provider !
  • Filesystem provider returns
  • File for files
  • RefFsContext for directories
  • Novell NDS can return OrganizationalDirContext
  • Containers should implement Context

28
Context Lifecycle Operations
  • ctx.createSubcontext(String name)
  • JNDI Provider chooses actual implementation of
    new context.
  • Ex. File System Provider will create a directory
  • No way to create a file
  • ctx.destroySubcontext(String name)
  • Can not destroy current object with name of
  • Ex. Remove directory in a filesystem

29
Name Parameter
  • Context operations can take a String or a Name as
    the name parameter
  • Name
  • Ordered sequence of components
  • Compound Names
  • Composite Names
  • String
  • Represents composite name

30
Composing Names
  • Name separator is not-standardized
  • Portability Options
  • Step through contexts one by one
  • not very efficient
  • Name objects and Name Parser
  • Name parser knows naming convention of provider
  • ctx.getNameParser(String name) NameParser
  • nameParser.parse( String name ) Name

31
Naming Exceptions
  • catch( NamingException e )
  • Important Methods
  • toString() String
  • getRootCause() Throwable
  • getRemainingName() Name
  • getExplanation() String

32
Directory Operations
javax.naming.directory.DirContext extends
javax.naming.Context examine/search attributes
associated with a directory object javax.naming.di
rectory.Attribute name and set of
values getAttributes( String name )
Attributes modifyAttributes( String name, . )
33
javax.naming.InitialContext
InitialContext()
ltltabstractgtgt
InitialContext(java.util.Hashtable env)
javax.naming.Context
bind(String name, Object obj)
createSubcontext(String name) Context
list(String name) NamingEnumeration
listBindings(String name) NamingEnumeration
javax.naming.directory.InitialDirContext
lookup(String name) Object
InitialDirContext()
InitialDirContext(Hashtable env)
ltltabstractgtgt
javax.naming.directory.DirContext
getAttributes() Attributes
search(String name, Attributes matchingAttributes)
NamingEnumeration
34
Attributes
  • DirContext contains an Attributes object for each
    contained binding
  • Common LDAP Attributes
  • c Country
  • o Organization
  • ou Organizational Unit
  • cn Common Name (typically first or full name)
  • sn Users surname

35
Attributes (Cont)
  • Attribute Name
  • referred to as attribute id
  • determines type of attribute (attribute type
    definition)
  • attribute syntax definition specifies the syntax
    for the attributes value and whether it can have
    multiple values
  • reverse lookupcontent-based searching

36
Directory Examples
  • Obtain reference to initial Directory Context
  • Extended NamingContext operations
  • Accessing Directory Object attributes
  • Search

37
Initial Directory Context
  • import javax.naming.
  • import javax.naming.directory.
  • java.util.Properties props new
    java.util.Properties()
  • props.put(Context.INITIAL_CONTEXT_FACTORY,
  • com.sun.jndi.ldap.LdapCtxFactory
    )
  • props.put(Context.PROVIDER_URL,
    ldap//hostport/rootDN)
  • DirContext dirContext new InitialDirContext(
    props )

38
Extended Naming Context Operations
  • Context creation can also specify attributes
  • createSubcontext( String name, Attributes attr )
  • Object can be bound with attributes
  • bind(String name, Object o, Attributes attr )

39
Retrieving Attributes
  • // Get all attributes
  • Attributes attrs
  • attrs dirContext.getAttributes(serviceBankJour
    nal)
  • listAll( attrs )
  • // Get a specific attribute value
  • String s (String) attrs.get(requiredMemory).ge
    t()

40
Listing Attributes
  • void listAll( Attributes attrs )
  • NamingEnumeration ne attrs.getAll()
  • while( ne.hasMore() )
  • Attribute a (Attribute) ne.next()
  • System.out.println(ID a.getID() )
  • NamingEnumeration vals a.getAll()
  • while( vals.hasMore() )
  • System.out.println( Val vals.next() )

41
Retrieving Specific Attributes
  • Can specify attributes to be returned
  • String desiredAttr platform,
    requiredMemory
  • Attributes attrs ctx.getAttributes(serviceBank
    Journal, desiredAttr)
  • listAll( desiredAttrs )

42
Modify Attributes
  • Can Add, Remove, and Replace attributes
  • BasicAttributes ba new BasicAttributes(color,
    red)
  • ba.put(ga, helium)
  • dirContext.modifyAttributes(Balloon,

  • DirContext.REPLACE_ATTRIBUTE, ba )
  • ModificationItem array

43
Search Example
  • Atributes attr new BasicAttributes( true ) //
    ignore attribute case
  • attrs.put( new BasicAttribute(hobby, guitar)
    )
  • NamingEnumeration result dirContext.search(ouP
    eople, attrs )
  • while( result.hasMore() )
  • System.out.println(result.next())

44
Basic Search
  • Specify set of attributes that an entry must
    have. Can specify required attribute values
  • Returns NamingEnumeration of SearchResults
    (extends binding)
  • getName(), getClassName(), getObject()
  • getAttributes()
  • Can specify attributes to return
  • search(String name, Attributes match, String
    retAttrIds )

45
Search Options
  • Filter string can be specified.
  • Logical operators
  • RFC 2254 specifies format
  • Search Controls can limit scope of search
  • time limits
  • max returned results
  • Can also set attributes to return here

46
Search Filter
  • prefix, boolean expressions
  • // For more control over the search results
  • SearchControls sc new SearchControls()
  • // Lets find people who play guitar and have a
    phone number
  • String filter ((hobbyguitar)(phone))
  • // Search for all people that match
  • NamingEnumeration res ctx.search(ouPeople,
    filter, sc )

47
SearchControls
  • Finer Control over the search
  • Attributes to return
  • sc.setReturningAttributes(String attrIds )
  • Search Scope
  • sc.setSearchScope( int )
  • OBJECT, ONELEVEL, SUBTREE

48
Storing objects
  • Not all service providers support serialization
    e.g. file system provider
  • May not be efficient, either
  • Store objects that implement the Referenceable
    interface in these cases
  • Reference getReference()
  • Enough info so object can be reconstructed with a
    factory class

49
Reference
  • Class name of object
  • Vector of RefAddr objects
  • getType(), getObject()
  • StringRefAddr and BinaryRefAddr
  • String name of factory for the object and
    location of the factory object

50
Federation
  • Composite names span multiple namespaces
  • ouPeople/tmp/myFile
  • File f (File) ctx.lookup(ouPeople/tmp/myFile)
  • lookup on this name will traverse through LDAP
    and the Filesystem provider to retrieve the file

51
JNDI and RMI
  • Can put standard interface on RMI registry
  • Properties props new Properties()
  • props.put(Context.INITIAL_CONTEXT_FACTORY,
    com.sun.jndi.rmi.registry.RegistryContextFactory
    )
  • props.put(Context.PROVIDER_URL,
    rmi//hostport)
  • Context ctx new InitialContext(props)
  • ctx.rebind(Teller, someRMIObject )
  • ..
  • Teller t (Teller) ctx.lookup(Teller)

52
JNDI and JDBC
  • JDBC Data Source
  • Evolution of the JDBC Driver Manager
  • Typically stored in a JNDI tree
  • connection pooling support
  • Methods
  • ds.getConnection(String name, String password )
  • conn.close()

53
JNDI and EJB
  • Home objects for beans are stored in JNDI
  • In order to create a bean
  • InitialContext ic new InitialContext( props )
  • TellerHome th (TellerHome) ic.lookup(TellerHome
    )
  • th.transfer( )
  • // Remote stub typically returned to client

54
JNDI and J2EE Applications
  • InitialContext
  • supplied by Container
  • accessed by Component using default ctor
  • InitialContext rootCtx new InitialContext()
  • Properties
  • supplied to Container in deployment descriptor
  • accessed by Component through special context
  • Object object rootCtx.lookup(javacomp/env/myOb
    ject)

55
JNDI 1.2 Features
  • Event Notification
  • LDAPv3 Extensions and Controls
  • Service Provider Support

56
Event Package
  • Capability to receive notifications when
    significant operations are performed with the
    naming/directory service
  • NamingEvent
  • type
  • object info before/after the change

57
Event Package (Cont)
  • NamingListener
  • Implement in order to receive NamingEvents
  • NamespaceChangeListener
  • objectAdded, objectRemoved, objectRenamed
  • ObjectChangeListener
  • objectChanged
  • EventDirContext
  • Register to receive NamingEvents

58
LDAP Package
  • For specific LDAP operations
  • search and modify
  • extended operations
  • unsolicited notifications
  • LDAPContext
  • Only use this package if the generic directory
    operations in javax.naming.directory are not
    sufficient

59
Service Provider Support
  • javax.naming.spi
  • For connecting naming/directory services to the
    JNDI API
  • Support
  • Federating among multiple naming systems
  • state to/from Java objects
  • object factories
  • state factories

60
JNDI Providers
  • LDAP
  • com.novell.naming.service.nds.NdsInitialContextFac
    tory
  • NIS
  • com.sun.jndi.nis.NISCtxFactory
  • NIS (future)
  • RMI
  • com.sun.jndi.rmi.registry.RegistryContextFactory
  • COSNaming
  • Files
  • com.sun.jndi.fscontext.RefFSContextFactory

61
Setup Requirement
  • For 1.3, essentially nothing. JNDI should be
    included in the distribution along with the 3
    providers
  • For Java 2 SDK version 1.2
  • jndi.jar in JAVA_HOME/jre/lib/ext
  • For jdk 1.1
  • Add jndi.jar to the CLASSPATH

62
LDAP Products
  • Publicly accessible
  • ldap//ldap.Bigfoot.com
  • ldap//ldap.four11.com
  • ldap//ldap.InfoSpace.com

63
Summary
  • JNDI client applications can access resources
    such as
  • printers
  • fax machines
  • databases (JDBC 2.0 extensions)
  • user credentials
  • object references
  • contained in multiple underlying naming service
    implementations using the same API

64
Resources
  • Main JNDI Page
  • http//java.sun.com/products/jndi/

65
References
  • Developing Java Enterprise Applications. Asbury,
    Weiner. Wiley. 1999
  • Java Enterprise in a Nutshell. Flanagan et al.
    OReilly 1999
  • Sun JNDI Tutorial
  • Mastering Enterprise Java Beans. Roman. Wiley 1999
Write a Comment
User Comments (0)
About PowerShow.com