Title: JNDI
1Chapter 4 5
2ltJSP useBean
- In Java Server Pages, you can use a server-side
java class within a JSP withltHTMLgt.ltJSP
useBean idmyBean classmypackage.mybean
/gtlt/HTMLgt - This name may be misleading. Its not technically
a bean, that is inherited from the javax.swing
for instance. It can be a simple java public
class, that your JSP will instantiate and use via
its public methods.
3JNDI
- JNDI (Java Naming Directory Interface) is an
API that provides a standard protocol for
accessing any network resource in an
inter/intranet. - JNDI allows a Java application to use servers
that provide network sharing, resource pooling
and access to their file structures.
4JNDI - Sun
- JNDI works in concert with other J2EE
technologies to organize and locate components in
a distributed computing environment -
http//java.sun.com/products/jndi/
5Network Services
- Supported by JNDI are
- Directory ServicesProvides access to directory
structures or other hierarchical trees of
objects on a services file system. - Naming ServicesEnables an application to access
a remote object by name (through mapping a unique
ID to each object and its name)
6Naming Service - Binding
- Mapping between an objects name and its unique
ID. - Server creates a unique ID, and user creates a
recognizable name for this object. - Examples
- Remote java class can be accessed in an Applet by
requesting its class name. - DNS (Domain Naming Service) translates domain
names to raw IP addresses.
7Naming Service - Namespace
- Defines the scope of an object.
- Objects must have unique names within their
namespace, but the same object/name can exist in
several of these spaces.
8Naming Service - Namespace
- Objects are identified by its naming convention
of the namespace. These may consist of Compound
names, where a (path)/(object) pair is given. - The other convention is a Composite Name, which
describe the protocol to use as well as the path
and resource requested.
9Service Providers
- These are part of the Name Service, residing on
the server, that map the JNDI API to the features
that are supported by this Name Service. - The API of the Name Service may be proprietary -
the Service Provider creates a common API (JNDI)
for these proprietary functions.
10Service Providers
- LDAP Lightweight Directory Access Protocol
provides common services for accessing
files/objects across many (sometimes
heterogeneous) servers. - NIS Network Information Service is a unix based
protocol providing access to those resources
across servers. - File Services Provides standard io from Java
classes to host file systems.
11Directory Services
- Provides a structured, hierarchical view to
objects in the name service. - Through the directory context (nodes on the
structure), users can query/search the various
different physical structures as one logical file
structure.
12LDAP
- The industry standard directory service for
enterprise servers, running on TCP/IP. - Items in the structure can be queried and
modified based on their context (where they are
in the structure). - http//www.ldapcentral.com/
13JNDI - Goals
- Consistency Uses a common API, with just a small
amount of classes that leverage an existing java
class library - Short learning curve Provides a simple interface
(may only be two lines of code) to access many
objects and services. - API is implementation independent, and open in
architecture. Providers can add to it if needed.
14Using JNDI
- Pg 80 - JNDI Architecture Review
- Example in Chapter 5
- Implementation of a File System provider
(FsContext), where - directories are context objects
- files are file objects represented by
java.io.File - Provides a look into a Naming Service
15javax.naming
- javax.naming is the package containing the
Context Interface. - Context is the starting point (root) of the
naming/directory service. - Pg 86 has a sample directory structure to use.
Context can be set at root (/) or /temp or other
...
16Defining a Context
-
- Properties p new Properties()
-
- p.put (Context.INITIAL_CONTEXT_FACTORY,
com.sun.jndi.fscontext.RefFSContextFactory) -
- p.put(Context.PROVIDER_URL,file/tmp/marketing)
-
- InitialContext ctx new InitialContext(p)
17Searching Objects In A Context
- Elements (File Objects) in a context can be
LISTED using an Enumeration object - NamingEnumeration rpts ctx.list(Reports)
- By using the rpts object, each report can be
retrieved using while ( rpts.hasMore() ) - Each element of rpts is a NameClassPair instance
(the name and the class to cast it to).
18Searching Objects
- To LOOKUP (retrieve) an object
- File rpt (File) cts.lookup(\reports\Report1.txt
) - rpt can now be used as a java.io.File object.
19LIST Example
- Hashtable env new Hashtable()
- env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.j
ndi.fscontext.RefFSContextFactory") - // Specify the filesystem to search in this
example - env.put(Context.PROVIDER_URL,"file/tmp/marketing"
) - Context initCtx new InitialContext(env)
- NamingEnumeration nl initCtx.list("reports")
- while (nl.hasMore())
-
- item nl.next()
- System.out.println("item's class is
"item.getClass().getName() ) - System.out.println(item)
20LOOKUP Example
Hashtable env new Hashtable() env.put(Contex
t.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fsco
ntext.RefFSContextFactory") env.put(Context.PROV
IDER_URL,"file/tmp/marketing") Context initCtx
new InitialContext(env) File f
(File)initCtx.lookup("reports/report1.txt")
21Binding Objects
- Binding brings together
- object name
- object class
- object instance
- Used for operations such as adding, deleting,
renaming or moving objects.
22Examples
- Deleting
- ctx.unbind(reports/Report1.txt)
- Renaming
- ctx.name(reports/Report1.txt,
reports/Report9.txt) - Add another Context
- ctx.createSubcontext(ArchiveReports)
23References To Java Objects
- JNDI provides another alternative to serializing
java objects. - References can be defined, which contain the
instructions for how to create an object that
isnt necessarily serializable. - Instead of the object being bound to a
naming/directory service, the address is stored
in an ObjectFactory.
24Object Factory
- An Object Factory holds references that JNDI uses
to retrieve objects that arent bound directly to
a service. - Between the Object Factory and the objects, are a
Reference object that holds - class name of referenced object
- RefAddr, which is the actual address
25Example
env.put(Context.PROVIDER_URL,
"file/tmp/marketing/presentations/") Context
initCtx new InitialContext(env)
initCtx.rebind("Susan", new Car("Toyota","Camry"))
initCtx.rebind("Cheryl",new Car("Saturn","Coupe
")) initCtx.rebind("Nicole", new
Car("Ford","Bronco")) Car c
(Car)initCtx.lookup("Cheryl")
26Example
public class Car implements Referenceable
public Reference getReference() throws
NamingException String cName
Car.class.getName() StringRefAddr cRef
new StringRefAddr("Car Des",
make "" model) String cfName
CarFactory.class.getName() Reference
ref new Reference(cName, cRef, cfName, null)
return ref
27Example
public class CarFactory implements ObjectFactory
public Object getObjectInstance(Object obj,
Name name, Context ctx, Hashtable env) throws
Exception if (obj instanceof
Reference) Reference ref
(Reference)obj if
(ref.getClassName().equals(Car.class.getName()))
RefAddr addr ref.get("Car
Des") if (addr ! null)
String s (String)addr.getContent(
) int n s.indexOf("")
String make
s.substring(0,n) String
model s.substring(n1)
return new Car(make,model)
return null
28javax.naming.directory
- Directory package provides an additional api for
directory specific services in addition to the
naming services. - Provides
- Directory searching using object attributes
- Search optimization
- Modification of an objects attributes
29Searching Objects
- env.put(Context.INITIAL_CONTEXT_FACTORY,
com.sun.jndi.ldap.LdapCtxFactory) - env.put(Context.PROVIDER_URL, ldap//localhost/o
JNDIExample) - DirContext dctx new InitialDirContext(env)
- Attribute Searching
- Attributes attrs new BasicAttributes(true)
- attrs.put(new BasicAttributes(email) )
- attrs.put(new BasicAttributes(website,www.cnn.c
om) ) - NamingEnumeration result dctx.search(ouPeople
, attrs)
30Advanced Searching
- SearchControls sc new SearchControls()
- Filters
- String filter ( (cnS) (account gt 1000) )
- NamingEnumeration result dctx.search(ouPeople
, filter) - Limiting Scope
- String attrIDs cn, email
- sc.setReturningAttributes(attrIDs)
- sc.setSearchScope(SearchControls.OBJECT_SCOPE)
- NamingEnumeration result dctx.search(ouPeople
, filter, sc)
31Advanced Searching
- Control Number Of Results
- sc.setCountLimit(5)
- Time Limit
- sc.setTimeLimit(2000)