Java Naming and Directory Interface JNDI - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Java Naming and Directory Interface JNDI

Description:

Telephone Directory. A to Z ... is stored inside the naming directory itself as with telephone directories ... Lightweight Directory Access Protocol. Based on ... – PowerPoint PPT presentation

Number of Views:1290
Avg rating:3.0/5.0
Slides: 25
Provided by: davide49
Category:

less

Transcript and Presenter's Notes

Title: Java Naming and Directory Interface JNDI


1
Java Naming and Directory Interface JNDI
  • Dave Elliman

2
What Is a Naming Service?
  • It allows you to look up a reference to an object
    using a user friendly name
  • This is familiar in everyday life
  • Telephone Directory
  • A to Z
  • Imagine a town in which all the streets move
    about while you are asleep
  • A to Z would be even more necessary!

3
Consider the Internet Domain Name System
  • A machine name maps to an IP address, eg
  • much.cs.nott.ac.uk maps to 128.243.21.21
  • The association of a name with an object is
    referred to as a binding
  • In these simple examples the object is stored
    inside the naming directory itself as with
    telephone directories
  • In more complex cases the object is big, or
    remote and a reference to the object I stored, as
    with A to Z street maps

4
A Context
  • A context is a set of name to object bindings
  • A context will have a naming convention
  • The mapping from DNS names to IP addresses
    follows a particular convention that can be
    described as a context
  • The mapping from filenames to file handles is
    another context

5
Naming Systems and Namespaces
  • A naming system is a set of contexts of the same
    type. That is they have the same naming
    convention
  • A namespace is the set of names in a naming
    system that is names that follow a common
    format
  • eg The DNS namespace is the set of all DNS
    domains and entries

6
Directory Concepts
  • Most (but not all) naming services are extended
    to support directories
  • A directory is an object that has attributes
  • eg name -gt Student Object with attributes
  • student_id 0787643
  • Surname Gates
  • Firstnames William Henry
  • Course G500
  • Year 3
  • Tutor dge

7
More on Directories
  • An attribute value could be another directory
    object
  • Qu. What kind of object can be represented by a
    naming and directory service then?
  • Ans. An attributed tree
  • Qu. What does this remind you of? What language
    represents exactly the same entity?
  • Ans xml

8
LDAP
  • The Internets standard naming and directory
    service is called LDAP
  • Lightweight Directory Access Protocol
  • Based on X500 data model
  • See http//www.ietf.org/rfc/rfc2251.txt
  • You can get an excellent open source
    implementation of an LDAP name server from
    http//www.openldap.org/
  • It is called slapd

9
Naming Service Examples
  • RMI registry - Java
  • Windows registry - MS
  • Windows active directory - MS
  • COS (Common Object Services) corba
  • NIS (Network Information System) Sun
  • DNS (Domain Naming Service) www
  • NDS (Novell directory service)

10
Where Does JNDI Fit in?
11
So It Simplifies Things Then?
  • Yes! See it as a layer or a wrapper
  • Learn JNDI and you can access ANY naming service
    using it
  • Of course you do need a Driver for the Naming
    Manager layer that supports you naming service.
    Very similar to JDBC isnt it?

12
A Closer Look at the JNDI API
  • The javax.naming package defines a Context
  • You need to
  • Import javax.naming.
  • Getting a Context is the basic pre-requisite for
    doing fairly obvious things like
  • lookup(), bind() etc

13
Getting an InitialContext for J2EE
  • import javax.naming.
  • Hashtable env new Hashtable()
  • env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.cosnaming.CNCtxFactory")
  • env.put(Context.PROVIDER_URL,"iiop//localhost370
    0")
  • env.put("org.omg.CORBA.ORBClass",
    "com.sun.corba.se.internal.POA.POAORB")
  • env.put("org.omg.CORBA.ORBSingletonClass",
    "com.sun.corba.se.internal.corba.ORBSingleton")
  • Context initial new InitialContext(env)

14
What Do You Need to Make This Work?
  • jndi.jar in your CLASSPATH it is for jdk1.3 and
    later
  • This has drivers for LDAP, COS and RMI naming
    services
  • If you need to access something else you will
    need a suitable driver, and put that in your
    CLASSPATH

15
Adding a Name
  • // Create the object to be bound
  • Fruit fruit new Fruit("orange")
  • // Perform the bind
  • ctx.bind("favourite", fruit)

16
You Can Change a Binding
  • // Create the object to be bound
  • Fruit fruit new Fruit("lemon")
  • // Perform the bind
  • ctx.rebind("favourite", fruit)

17
More Advanced Things
  • You can create a new Context
  • Or a new SubContext
  • You can destroy such things too
  • You can read attributes
  • You can search for values
  • You can return all bindings
  • You can do anything you might reasonably wish to
    do.. This is beyond scope of G53ELC

18
And You Can Remove a Binding
  • // Remove the binding
  • ctx.unbind("favourite")

19
Suppose You Are Using LDAP
env.put(Context.PROVIDER_URL, "ldap//ldap.wiz.com
389") env.put(Context.SECURITY_PRINCIPAL,
"joeuser") env.put(Context.SECURITY_CREDENTIALS,
"joepassword")
20
Looking Something up
Object obj ctx.lookup(name)
21
Ever Messed About With the Windows Registry?
(Regedt32.Exe)
22
JNDI can do this!
import java.util. import javax.naming. public
class WinReg public static void main(String
args) try Hashtable env new
Hashtable() env.put("java.naming.factory.initial
", "com.scand.jndi.registry.RegistryInitialConte
xtFactory") Context ctx new
InitialContext(env) String comp
(String)ctx.lookup("HKEY_CURRENT_USER\\Environme
nt\\path") System.out.println("\nPATH IS\n"
comp) catch
(NamingException e) e.printStackTrace()

23
What do you need?
  • You need a jar and dll from http//www.scand.com/
  • WinRegistry.dll needs to be in PATH or in
    \Winnt\System32
  • Registry.jar needs to be in CLASSPATH
  • Free but pay 100 for more facilities

24
Other Things You Might Want to Do
  • Find out what software packages are installed or
    what ODBC resources
  • Read or set configuration parameters
  • Windows software tends not to use .ini or .xml
    configuration files but uses the registry
  • Find out about users, printers, communication
    protocols supported etc
  • THIS IS SERIOUSLY USEFUL!!!!
Write a Comment
User Comments (0)
About PowerShow.com