28
Topic : JNDI Kaster Nurmukan

Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

Embed Size (px)

Citation preview

Page 1: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

Topic : JNDIKaster Nurmukan

Page 2: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 2 (c)CDAC(Formerly NCST)

• What is JNDI?• Naming and Directory Services• Naming Concepts• Issues• JNDI Architecture• Programming with JNDI• Role of JNDI in J2EE

Page 3: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 3 (c)CDAC(Formerly NCST)

• Class.forName("com.microsoft.jdbc.

sqlserver.SQLServerDriver");

• cnn = DriverManager.getConnection

("jdbc:microsoft:sqlserver://siddh

ant:1433",“username",“password");

Page 4: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 4 (c)CDAC(Formerly NCST)

• Context ctx = new InitialContext();• DataSource ds =

(DataSource)ctx.lookup(“myname”);• Connection con = ds.getConnection(“abc”,”***”);

Page 5: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 5 (c)CDAC(Formerly NCST)

Java Naming and Directory Interface (JNDI) provides a standard interface for Java applications to access naming and directory services.

Page 6: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 6 (c)CDAC(Formerly NCST)

• Naming Service performs:

– Binding: Associating names with objects

– Lookup: Find an object based on a name.

• Examples: DNS and File systems• Examples:

– DNS

– Filesystems

Page 7: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 7 (c)CDAC(Formerly NCST)

• Directory is a naming service that stores

objects with attributes.

– Usernames and passwords etc stored in attrs.• Tree like structure

Page 8: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 8 (c)CDAC(Formerly NCST)

• Atomic name is a indivisible component of a

name

– in /etc/fstab, etc and fstab are atomic names.• Compound name is zero or more atomic

names put together.

– /etc/fstab is a compound name

Page 9: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 9 (c)CDAC(Formerly NCST)

• Binding is an association of a name with an object.

– Filename “autoexec.bat” has a binding to some file data on the disk.

– “c:\windows” foldername is bound to a folder on your drive.

• Compound name such as /usr/people/ed/.cshrc

consists of multiple bindings, one to usr, one to

people, one to ed, and one to .cshrc.

Page 10: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 10 (c)CDAC(Formerly NCST)

• Contains zero or more bindings.

– Each binding has a distinct atomic name.• /etc contains files named mtab and exports.• The /etc folder is a context containing

bindings with atomic names mtab and exports.

• mtab and exports are each bound to a file on

the disk.

Page 11: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 11 (c)CDAC(Formerly NCST)

• /usr – CONTEXT – /usr/people – SUB-CONTEXT – /usr/bin – SUB-CONTEXT – /usr/local – SUB-CONTEXT• Each atomic name is bound to a sub-context the

subfolder.• Subcontext are full-fledged contexts• Can contain more name-object bindings, such as

other files or other folders.

Page 12: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 12 (c)CDAC(Formerly NCST)

• Naming system: A connected set of

contexts.• Namespace: all the names contained within

that naming system.

Page 13: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 13 (c)CDAC(Formerly NCST)

• Starting point for exploring a namespace.• Starting point for performing all naming and

directory operations.

Page 14: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 14 (c)CDAC(Formerly NCST)

• Many naming and directory products. – Netscape Directory Server – MicrosoftActiveDirectory• Various naming and directory protocols: Each directory standard has a different protocol for accessing the directory. – Lightweight Directory Access Protocol (LDAP) – Network Information System (NIS) – Novell’s Network Directory System (NDS)• EveryDS has it’s ownAPI.

Page 15: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 15 (c)CDAC(Formerly NCST)

• Standard interface to interact with naming and

directory systems.• For Java programs.• Provides common interface to disparate

directories: Same API for LDAP and NIS NDS.• Used in EJB, RMI-IIOP, JDBC for operations like

locating entities i.e. Users, Machines (e.g. printer),

Objects, Services (e.g. datasource) etc.

Page 16: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 16 (c)CDAC(Formerly NCST)

• The client API

– Allow Java code to perform directory

operations.• The Service Provider: Driver• The Service Provider Interface (SPI)

– An interface to which naming and directory

service vendors can plug in.

Page 17: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 17 (c)CDAC(Formerly NCST)

Page 18: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 18 (c)CDAC(Formerly NCST)

The JNDI comprises of 5 packages• javax.naming – Contains classes and interfaces

for accessing naming services• javax.naming.directory – Extends javax.naming

and provides functionality to access directory services in addition to naming services

Page 19: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 19 (c)CDAC(Formerly NCST)

• javax.naming.event – Classes and interfaces for supporting event notification in naming and directory services

• javax.naming.ldap – Classes and interfaces for using features that are specific to LDAP v3 that are not already covered by the more generic javax.naming.directory

• javax.naming.spi – Vendors develop their naming/directory services conforming to SPI. Applications can then access these services through the JNDI API

Page 20: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 20 (c)CDAC(Formerly NCST)

• Used to acquire an initial context• Implementation of the JNDI driver: Knows the specific semantics of a particular directory structure.• bootstrapping.• Necessary information for JNDI to acquire that initial context. – The IP address of the J2EE server – The port number that the J2EE server accepts – Any username/password necessary to use the

J2EE server.

Page 21: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 21 (c)CDAC(Formerly NCST)

javax.naming.Context ctx = new javax.naming.InitialContext (System.getProperties()); java -Djava.naming.factory.initial= com.sun.jndi.fscontext.RefFSContextFactory -Djava.naming.provider.url= file:c:\examples.InitCtxclass of the JNDI driver• provider URL: URL that the service provider accepts for bootstrapping.

Page 22: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 22 (c)CDAC(Formerly NCST)

Methods invoked on a Context

• list() - list of contents available at the context.

– names of objects bound to the JNDI tree

– subcontexts.

• lookup() - look up objects bound to the JNDI tree

– Return type is driver specific

• RMI-IIOP java.rmi.Remote

• file system java.io.File

Page 23: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 23 (c)CDAC(Formerly NCST)

• rename() - give a new name to a context – c:\temp to c:\tmp• createSubcontext() - create a subcontext at the context – c:\foo\bar at the folder c:\foo.• destroySubcontext() - destroy a subcontext of the context – Destroy c:\foo\bar from the folder c:\foo.• bind() – associates a name to a content and stores it at the Context – JNDI drivers accept different parameters to bind()• rebind() - forces a bind even if some object is already bound to the name.

Page 24: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 24 (c)CDAC(Formerly NCST)

import javax.naming.*; public class Startup { public static void main(String args[]) throws

Exception { AccountImpl acct_impl = new AccountImpl(); Context ctx = new InitialContext

(System.getProperties()); ctx.rebind(“myname", acct_impl); } }

Page 25: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 25 (c)CDAC(Formerly NCST)

import javax.naming.*; import java.rmi.*; public class Client { public static void main (String[] args) throws

Exception { Context ctx = new InitialContext (System.getProperties());

Object remoteObject = ctx.lookup(“myname"); Account account = (Account) javax.rmi.PortableRemoteObject.narrow ( remoteObject, Account.class); int b = account.method1(5));}}

Page 26: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 26 (c)CDAC(Formerly NCST)

• J2EE servers have a JNDI implementation.• Used to Look up beans.• Connect to resource factories

– JDBC DataSource

– Java Message Service (JMS) drivers• Acquiring a reference to the Java

Transaction API’s (JTA) UserTransaction

interface.

Page 27: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming

JNDI 27 (c)CDAC(Formerly NCST)

• Mastering Enterprise JavaBeans by Ed

Roman et. al. (Wiley)• JNDI Tutorial on java.sun.com• Java Server Programming J2EE Edition –

Volume 1 – Wrox.• Java Enterprise in a nutshell – David

Flanagan et. Al. – O’reilly

Page 28: Topic : JNDI Kaster Nurmukan. JNDI2(c)CDAC(Formerly NCST) What is JNDI? Naming and Directory Services Naming Concepts Issues JNDI Architecture Programming