Java Naming and Directory Interface - PowerPoint PPT Presentation

About This Presentation
Title:

Java Naming and Directory Interface

Description:

http://java.sun.com/products/jndi/ v001025. JNDI. 17. Common JNDI Tasks ... com.sun.jndi.rmi.registry.RegistryContextFactory. COSNaming ( CORBA Naming System ) Files ... – PowerPoint PPT presentation

Number of Views:311
Avg rating:3.0/5.0
Slides: 33
Provided by: daniel447
Learn more at: https://www.cise.ufl.edu
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
  • References

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

4
Contexts
  • Contains a set of bindings and lookup operations
  • Has its own naming convention
  • Examples
  • File system directory /
  • DNS domain edu
  • LDAP cus

5
Names
  • Logical identifier assigned to an object
  • Naming convention defined by Naming System
  • Examples
  • /dir/dir2/file for UNIX
  • drive\dir\string for DOS
  • cndan, oISP for LDAP
  • www.cise.ufl.edu for DNS

6
Names (Cont)
  • Atomic Name
  • name used in a binding
  • Ex. filename in a directory
  • Compound Name
  • sequence of atomic names
  • /usr/bin/ls

7
Names (Cont)
  • Composite Name
  • applies 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

8
Directory Service
  • Can obtain an object with attributes and search
    for objects based on attributes
  • Current Directory Systems
  • X.500 - ISO standard. Complex.
  • LDAP - Simplified X.500 over TCP/IP
  • NDS - Novell Directory Services.
  • NIS - Directory service for Solaris

9
Attributes
  • Common LDAP Attributes
  • c Country
  • o Organization
  • ou Organizational Unit
  • cn Common Name (typically first or full name)
  • sn Users surname

10
Naming and Directory Targets
  • Resources
  • printers
  • machines
  • Users
  • Java Objects !

11
Naming Vs. Directory
  • 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
  • yellow pages

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

13
JNDI Architecture Overview
Java Client Application
JNDI API
JNDI Naming Manager
JNDI Service Provider Interface
DNS
LDAP
CORBA
RMI
Anything
14
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

15
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

16
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/

17
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

18
JNDI Contexts
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.RefFSCont
    extFactory)
  • props.put(Context.PROVIDER_URL, file///)
  • Context initContext new InitialContext( props )

20
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 )

21
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 )

22
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

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

24
Object Lookup
  • Lookup object in a context
  • String name Object o initContext.lookup(name)
  • name can be compound
  • /usr/tmp/dir/myfile
  • Filesystem provider returns
  • File for files

25
Attributes
  • DirContext contains an object with attributes 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

26
Basic Search
  • Specify a set of attributes that an entry must
    have.
  • Specify required attribute values
  • Specify attributes to return
  • search(String name, Attributes match, String
    retAttrIds )
  • Returns NamingEnumeration of SearchResults

27
Storing objects
  • Object Types to store
  • Java Serializable Object
  • Referenceable Java Object
  • Remote Objects like RMI and CORBA objects

28
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

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

30
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

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

32
Resources
  • JNDI API Tutorial and Reference, Rosanna Lee and
    Scott Seligman. Addison Wesley, 2000
  • Main JNDI Page
  • http//java.sun.com/products/jndi/
  • Java Lecture Source
  • http//www.apl.jhu.edu/jcstaff/ejava/ejava_whatsn
    ew_f00.htm
  • Java Online Tutorial
  • http//java.sun.com/docs/books/tutorial/
Write a Comment
User Comments (0)
About PowerShow.com