Sie sind auf Seite 1von 28

Java Naming and Directory Interface (JNDI) FAQ From

jGuru
Generated Sep 13, 2005 1:47:06 PM

Location: http://www.jguru.com/faq/JNDI
Ownership: http://www.jguru.com/misc/user-agree.jsp#ownership.

Does JMS require the Java Naming and Directory Interface (JNDI)?
Location: http://www.jguru.com/faq/view.jsp?EID=778
Created: Nov 10, 1999 Modified: 1999-11-10 16:23:38.421
Author: Jerry Smith (http://www.jguru.com/guru/viewbio.jsp?EID=9)

No, but... JNDI provides network-oriented look-up services for distributed objects
(among other things). It's possible to implement, as well as use, JMS software
without JNDI. In general, however, JNDI provides critical services for JMS-based
software because it's important to be able to locate (programmatically) JMS services
implemented in objects called factories.

For example, applications that communicate via JMS must set up communication
pathways that involve destinations (topic areas and queues), connections, sessions,
and so on. JNDI makes it easy for an application to locate, for example, a connection
factory for the appropriate vendor's JMS middleware, which can then provide a
connection, which can provide a session, and so on, all relative to the JMS server
that's associated with that connection factory.

What is JNDI?
Location: http://www.jguru.com/faq/view.jsp?EID=10852
Created: Feb 2, 2000 Modified: 2000-02-02 05:25:14.381
Author: Richard Monson-Haefel (http://www.jguru.com/guru/viewbio.jsp?EID=16)

The Java Naming and Directory Interface (JNDI) is an application programming


interface (API) for accessing different kinds of naming and directory services. JNDI is
not specific to a particular naming or directory service, it can be used to access many
different kinds of systems including file systems; distributed objects systems like
CORBA, Java RMI, and EJB; and directory services like LDAP, Novell NetWare, and
NIS+.

JNDI is similar to JDBC in that they are both Object-Oriented Java APIs that provide
a common abstraction for accessing services from different vendors. While JDBC can
be used to access a variety of relational databases, JNDI can be used to access a
variety of of naming and directory services. Using one API to access many different
brands of a service is possible because both JDBC and JNDI subscribe to the same
architectural tenet: Define a common abstraction that most vendors can implement.
The common abstraction is the API. It provides an objectified view of a service while
hiding the details specific to any brand of service. The implementation is provided by
the vendor, it plugs into the API and implements code specific to accessing that
vendor's product.

JNDI provides two APIs and one SPI. JNDI has a naming API that allows Java
applications to access naming systems like CORBA's Naming services and a directory
API that extends the naming service to provide access to directory services like
LDAP. JNDI also has a SPI (Service-Provider Interface) which is a programming
model that vendors use to write JNDI plug-ins or implementations for their specific
product. Each vendor's plug-in is called a service-provider. A service-provider
implements the JNDI APIs so that a Java application can access that vendor's
product. For the most part, JNDI hides the implementation details of the a service-
provider so that Java developer that uses JNDI can use the same objects and method
regardless of the brand of naming or directory service accessed. This is the real
power behind APIs like JDBC and JNDI: They provide one programming model for
accessing many different products; there is no need to learn a different programming
model every time a different product is used.

Comments and alternative answers

its good
Author: srinivas kumar (http://www.jguru.com/guru/viewbio.jsp?EID=1100681), Jul
10, 2003
this page is highly informative and can be easily understood

What mailing lists are available for JNDI discussion?


Location: http://www.jguru.com/faq/view.jsp?EID=11934
Created: Feb 6, 2000 Modified: 2000-03-16 23:40:38.213
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

Sun manages the JNDI-INTEREST mailing list. A searchable archive of this list can be
found at http://archives.java.sun.com/archives/jndi-interest.html. There is also a link
on that page which lets you join or leave the mailing list.

Are there any other FAQs on JNDI?


Location: http://www.jguru.com/faq/view.jsp?EID=11935
Created: Feb 6, 2000 Modified: 2000-02-07 06:25:06.375
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

Sun's JNDI FAQ is available from http://java.sun.com/products/jndi/faq.html.

Are there any books that provide decent JNDI coverage?


Location: http://www.jguru.com/faq/view.jsp?EID=12029
Created: Feb 7, 2000 Modified: 2000-05-02 23:54:34.883
Author: Richard Monson-Haefel (http://www.jguru.com/guru/viewbio.jsp?EID=16)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

There are no books at this time that cover JNDI exclusively. There are, however, two
books that include some coverage of JNDI as well as other Enterprise Java subjects
-- they are listed below.

• Java Enterprise in a Nutshell (O'Reilly)


• Professional Java Server Programming (Wrox Press)
• LDAP Programming with Java (Addison-Wesley)

Comments and alternative answers


See also Implementing LDAP by Mark Wilcox.
Author: Taruvai Subramaniam (http://www.jguru.com/guru/viewbio.jsp?EID=43495),
May 2, 2000
See also Implementing LDAP by Mark Wilcox.

Note that there is now a true JNDI book. Check out...


Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4), May 17,
2000
Note that there is now a true JNDI book. Check out What books cover JNDI?

Note that the book recommended above called "LDAP...


Author: Tony Sheehan (http://www.jguru.com/guru/viewbio.jsp?EID=37355), Jun 16,
2000
Note that the book recommended above called "LDAP Programming with Java
(Addison-Wesley)" does not make use of JNDI but rather Netscape's implementation
(Directory SDK).

What's a Naming System or Service?


Location: http://www.jguru.com/faq/view.jsp?EID=14444
Created: Feb 15, 2000 Modified: 2000-03-16 23:42:15.702
Author: Richard Monson-Haefel (http://www.jguru.com/guru/viewbio.jsp?EID=16)

A naming system associates names with addresses. A phone book is a perfect


example of a naming system that associate people's names with phone numbers and
addresses. Naming systems are used in computing to make it easier for user to
locate and utilize software that is addressable. A software system that exposes a
naming system to other software is called a naming service.

A prefect example of a naming service is the Domain Name System (DNS) used by
everyone on the World Wide Web. Internet Protocol (IP) addresses are made up of a
series of digits separated by "dots" ('.'). The address of jGuru.com, for example, is
216.217.9.172. IP address are great for routing messages among the millions of
computers on the Internet, but its not very user friendly. The DNS associates names
with these IP addresses so that it's easier for humans to remember addresses of
their favorite sites and access them on the Internet.

The rules that govern association of names with IP addresses are called the Domain
Naming System. The Berkley Internet Name Domain (BIND) server, which is a type
of server used to resolve names into IP addresses using DNS, is an example of a of a
naming service.

Naming services are common place in computing. The OS file system on a computer
is a type of naming service that associates the addresses of data blocks on a disk
drive with file and directory names. Network file systems are also naming services
and some, like NIS+ and Novell Netware are an advanced naming service called a
directory service.

What's the format of an LDAP URL?


Location: http://www.jguru.com/faq/view.jsp?EID=16059
Created: Feb 19, 2000 Modified: 2000-02-21 05:17:23.234
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

RFC 2255 defines the basic structure of the LDAP URL. Its basic definition is:
"ldap://" [host [":" port] ] ["/" [dn
["?" [attributeList] ["?" [scope]
["?" [filterString] ["?" extensions]]]]]

• host and port specify where the LDAP server is located


• dn is the LDAP Distinguished Name to search for
• attributeList is a comma-delimited list of what to retrieve
• scope is either base, one, or sub [base is the default if unspecified]
• filterSearch is the search filter
• extensions is a comma-delimited list of extensions

What is an LDAP Distinguished Name?


Location: http://www.jguru.com/faq/view.jsp?EID=17639
Created: Feb 24, 2000 Modified: 2000-02-25 05:06:20.193
Author: Madhusudan Chaganthi (http://www.jguru.com/guru/viewbio.jsp?EID=1416)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

Distinguished name is a unique name for an entry in the Directory Service.

A directory service has entries that are similar to the UNIX file structure. For e.g., in
Unix we have
/etc
/bin
/usr/bin
/usr/local

In a LDAP directory service, we can have the distinguished name:

cn=John Zukowski, ou=FAQ Guru, o=JGuru

Where:
o is Organization
ou is the Organizational Unit
cn is the Common Name

In the same LDAP service we can have the distinguished name:


cn=John Zukowski, ou=FAQ Member, o=JGuru

Both of these entries relate to same person (John Zukowski), but in different sub-
trees (ou). One entry might have different permissions, rights, etc. when compared
to the other. So the only way we can distinguish these two entries is by using their
Distinguished names.

What's the relationship between LDAP and JNDI?


Location: http://www.jguru.com/faq/view.jsp?EID=17763
Created: Feb 24, 2000 Modified: 2000-02-25 05:10:11.085
Author: neal ravindran (http://www.jguru.com/guru/viewbio.jsp?EID=17737)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

JNDI has classes provided by Sun that will help your application interact with an
LDAP server. JNDI applications work similarly to JDBC applications....Write an
application once and be free to use "drivers" from different vendors. Sun provides
the "driver" that will help interact with the LDAP server. Sun also provides "drivers"
for other naming services(like CORBA, Netscape)

What Sun has not written is a driver to interact with ADSI(win 2000)

Comments and alternative answers

Java/ADSI?
Author: David Pratt (http://www.jguru.com/guru/viewbio.jsp?EID=453513), Jul 11,
2001
Are there any solutions for connecting to Active Directory via Java?

I need to know this as well!


Author: keith gomoll (http://www.jguru.com/guru/viewbio.jsp?EID=588788), Dec 18,
2001
I would really like to know this i am having a hard time here

I need to know this as well!


Author: keith gomoll (http://www.jguru.com/guru/viewbio.jsp?EID=588788), Dec 18,
2001
I would really like to know this i am having a hard time here

Re: I need to know this as well!


Author: maximili malir (http://www.jguru.com/guru/viewbio.jsp?EID=795566),
Mar 13, 2002
dEFINITELY INTERESTED ALSO , SINCE I DO NOT WASNT TO WRITE IT
IN .NET.

Re: I need to know this as well!


Author: maximili malir (http://www.jguru.com/guru/viewbio.jsp?EID=795566),
Mar 13, 2002
dEFINITELY INTERESTED ALSO , SINCE I DO NOT WASNT TO WRITE IT
IN .NET.

Can I mix JNDI v1.1 with JNDI v1.2?


Location: http://www.jguru.com/faq/view.jsp?EID=25307
Created: Mar 16, 2000 Modified: 2000-03-16 23:59:54.397
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

It depends. Check out JNDI 1.2 Compatibility with JNDI 1.1 page.
Where is the official documentation for JNDI?
Location: http://www.jguru.com/faq/view.jsp?EID=25310
Created: Mar 16, 2000 Modified: 2000-03-17 00:00:20.539
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the JNDI Documentation page.

What's new in JNDI v1.2?


Location: http://www.jguru.com/faq/view.jsp?EID=25311
Created: Mar 16, 2000 Modified: 2000-03-17 00:00:50.011
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the new JNDI features summary.

Where can I get the JNDI reference implementation?


Location: http://www.jguru.com/faq/view.jsp?EID=25312
Created: Mar 16, 2000 Modified: 2000-03-17 00:01:14.433
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

From the JNDI download page.

Can I redistribute Sun's JNDI software?


Location: http://www.jguru.com/faq/view.jsp?EID=25313
Created: Mar 16, 2000 Modified: 2000-03-17 00:01:43.025
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

The redistribution terms are discused in the JNDI license.

How do you add an entry to an LDAP server using JNDI?


Location: http://www.jguru.com/faq/view.jsp?EID=38293
Created: Apr 20, 2000 Modified: 2000-04-21 00:20:54.886
Author: Jonathan Bruce (http://www.jguru.com/guru/viewbio.jsp?EID=38290)
Question originally posed by Tony Sheehan
(http://www.jguru.com/guru/viewbio.jsp?EID=37355

You should take a look here first ...


http://java.sun.com/products/jndi/tutorial/ldap/index.html

And then take a look in the 'examples' diretory of the JNDI download.

Comments and alternative answers

There is no example to add a new entry.


Author: Alex Surkov (http://www.jguru.com/guru/viewbio.jsp?EID=1036616), Dec
11, 2002
There is only example to add new attributes.

There is no example to add a new entry.


Author: Alex Surkov (http://www.jguru.com/guru/viewbio.jsp?EID=1036616), Dec
11, 2002
There is only example to add new attributes.

Re: There is no example to add a new entry.


Author: Jim Werwath (http://www.jguru.com/guru/viewbio.jsp?EID=1146147),
Feb 13, 2004
I found an example at http://www.openldap.org/lists/openldap-
software/200010/msg00342.html

Are there any JNDI tutorials?


Location: http://www.jguru.com/faq/view.jsp?EID=50797
Created: May 17, 2000 Modified: 2000-05-17 14:07:17.49
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Yes, check out The JNDI Tutorial online and/or buy a copy of it here.

What books cover JNDI (Java Naming and Directory Interface)?


Location: http://www.jguru.com/faq/view.jsp?EID=50819
Created: May 17, 2000 Modified: 2000-05-17 14:17:58.483
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out The JNDI Tutorial. It's also available on the web.

For other books that are relevant to JNDI, check out Are ther any books that provide
decent JNDI coverage?.

How do you use the JNDI ENC to access JavaMail?


Location: http://www.jguru.com/faq/view.jsp?EID=75066
Created: Jun 14, 2000 Modified: 2000-06-14 07:30:41.03
Author: Steven Lau (http://www.jguru.com/guru/viewbio.jsp?EID=72026) Question
originally posed by Se Hee Lee
(http://www.jguru.com/guru/viewbio.jsp?EID=21287

First of all, you have to create a javax.mail.Session like you would in accessing other
resources such as JDBC connections:

InitialContext ctx = new InitialContext();


Session session =
(Session) ctx.lookup("java:comp/env/TheMailSession");
After that, everything else is the same:

Message msg = new MimeMessage(session);


...
...
Depending on the application server you're using, you'll have different ways in
setting up the ENC.

What is LDAP (Lightweight Directory Access Protocol)?


Location: http://www.jguru.com/faq/view.jsp?EID=109929
Created: Jul 24, 2000 Modified: 2000-07-24 13:11:31.212
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4) Question
originally posed by Deepak Kalra
(http://www.jguru.com/guru/viewbio.jsp?EID=99188

LDAP is the "Lightweight Directory Access Protocol" and is defined by RFC 2251. Very
basically, LDAP is a simpler take on the X.500 directory access protocol.

How can I use a database connection pool within a servlet or JSP page?
Location: http://www.jguru.com/faq/view.jsp?EID=112097
Created: Jul 26, 2000 Modified: 2001-02-13 12:32:02.875
Author: Ken Young (http://www.jguru.com/guru/viewbio.jsp?EID=58585) Question
originally posed by arun suresh
(http://www.jguru.com/guru/viewbio.jsp?EID=110596

If you are using a J2EE Server, you should be able to access a connection pool via
JNDI and retreive the a connection. If you are not using a J2EE Server, you can
download (or write) a connection pool, and install it into your JSP/Servlet application,
and share it via the application space.
Comments and alternative answers

See also How do I access a database from my servle...


Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3), Aug 14, 2000
See also How do I access a database from my servlet?

One Connection Pooler


Author: Nate McMorris (http://www.jguru.com/guru/viewbio.jsp?EID=929030), Jun
26, 2002
A simple and functional and free Connection Pooler is the DbConnectionBroker from
javexchange (http://www.javaexchange.com). However... Almost no documentation.
So here's what I found. 1) Start really simple, just create the broker object and
NOTHING ELSE first. 2) When catching exceptions, use the method catching syntax
( public void method() throws Exception {...} ) rather than using a "try {} catch(){}"
clause. 3) You must create your log file first. The Broker won't create it if it doesn't
exist. 4) If you're running this inside a JSP/Servlet container, you can't rely on the
container's class loader to find the database driver or any other classes you need from
the DbConnectionBroker class; you need to make sure the DB Driver etc. is
somewhere in your $CLASSPATH. 5) I needed to get this to work inside TOMCAT,
and without using Servlets, so I create the broker between the <jsp:useBean...> and
</jsp:useBean> tags, then I load that instance into a bean with an "application" cope.
Then I can call that bean from any page where I need a connection. 6) Lastly, I
couldn't get some of the methods (like idOfConnection() ) to work, so try the methods
out one by one to see if they work. -

Re: One Connection Pooler


Author: Shane Word (http://www.jguru.com/guru/viewbio.jsp?EID=68418), Jul 1,
2002
Another Free DB Connection Pool that has pretty good Documentation is at:
http://sourceforge.net/projects/esw/ Its pretty easy to use with a convenience
method to load the driver and a single line to get the connection pool.

What special characters do I have to encode for an LDAP name?


Location: http://www.jguru.com/faq/view.jsp?EID=113588
Created: Jul 29, 2000 Modified: 2000-07-29 02:46:51.43
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

For LDAP providers, you must encode a space character at the beginning or end of
the string, a sharp ("#") character at the beginning of the string, and all of: """, ",",
"\", "+", "<", ">", and ";" by preceding the special character using a backslash ("\").
For a more complete scoop on this, check out Handling Special Characters from the
JNDI API tutorial.

How do I use JNDI to locate the home interface of an EJB running on a


different host? My client's InitialContext doesn't seem to know where the
server is.
Location: http://www.jguru.com/faq/view.jsp?EID=116655
Created: Aug 1, 2000 Modified: 2001-10-09 23:26:36.597
Author: Robert Castaneda (http://www.jguru.com/guru/viewbio.jsp?EID=4362)
Question originally posed by Puneet Sachdeva
(http://www.jguru.com/guru/viewbio.jsp?EID=28785

This depends on which EJB container / JNDI implementation that you are using.

For example, in WebLogic, you set the javax.naming.Context.PROVIDER_URL


property to be the host and port, for example "t3://hostname:7001"

Using the Inprise Application Server, it will do this automatically if you are within the
same subnet using the Smart Agent, or you can pass a command-line parameter into
the virtual machine.

You should refer to the documentation for the implementation that you are using for
the exact specifics.

Comments and alternative answers

How do I use JNDI to locate the home interface of an EJB running on a different
host?
Author: Sribha Jain (http://www.jguru.com/guru/viewbio.jsp?EID=484436), Aug 27,
2001
How do we do this using J2EE server and where is this property specified?

Re: How do I use JNDI to locate the home interface of an EJB running on a
different host?
Author: James Chiang (http://www.jguru.com/guru/viewbio.jsp?EID=341533),
Aug 29, 2001
Setting the following env. when instantiating InitialContext:
Properties env = new Properties();
env.put("java.naming.factory.initial",
"com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url",
"iiop://hostname:1050");
InitialContext jndiCtx = new InitialContext(env);

Re: Re: How do I use JNDI to locate the home interface of an EJB running
on a different host?
Author: Sribha Jain (http://www.jguru.com/guru/viewbio.jsp?EID=484436),
Aug 29, 2001
Thanks, James...but will this work for all application servers...i am using J2EE
server currently. And how do we know whether our iiop port is 1050(as u have
given in code).

Re: Re: How do I use JNDI to locate the home interface of an EJB running
on a different host?
Author: Sribha Jain (http://www.jguru.com/guru/viewbio.jsp?EID=484436), Aug
29, 2001
I tried it out but it gives following error on running the client.
javax.naming.NameNotFoundException. Root exception is
org.omg.CosNaming.NamingC ontextPackage.NotFound at
org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(Unknown
So urce) at
org.omg.CosNaming.NamingContextPackage.NotFoundHelper.extract(Unknown
Source) at org.omg.CosNaming._NamingContextStub.resolve(Unknown Source)
at com.sun.jndi.cosnaming.CNCtx.callResolve(Unknown Source) at
com.sun.jndi.cosnaming.CNCtx.lookup(Unknown Source) at
com.sun.jndi.cosnaming.CNCtx.lookup(Unknown Source) at
javax.naming.InitialContext.lookup(Unknown Source) at
ConverterClient.main(ConverterClient.java:27)

Re: Re: Re: How do I use JNDI to locate the home interface of an EJB
running on a different host?
Author: James Chiang
(http://www.jguru.com/guru/viewbio.jsp?EID=341533), Aug 29, 2001
It's indeed port 1050 for SUN's j2ee RI server. You can type "j2ee -verbose"
to find the naming service port.
As to solve NotFoundException, just copy j2ee.jar (located at
%J2EE_HOME%\lib) to the client end and set into classpath.

Re: Re: Re: Re: How do I use JNDI to locate the home interface of an
EJB running on a different host?
Author: Sribha Jain
(http://www.jguru.com/guru/viewbio.jsp?EID=484436), Aug 29, 2001
I have already done that, but the problem persists...
Re: Re: Re: Re: Re: How do I use JNDI to locate the home interface
of an EJB running on a different host?
Author: moiroux matthieu
(http://www.jguru.com/guru/viewbio.jsp?EID=540275), Nov 6, 2001
i've just try it and it work tx !

Re[2]: How do I use JNDI to locate the home interface of an EJB running
on a different host?
Author: Vassilis Mokas (http://www.jguru.com/guru/viewbio.jsp?EID=855498),
Apr 26, 2002
Can you please tell me the corresponding code for Borland Application Server
4.5 or 5 ? I just want to locate an EJB in BAS using a servlet in Tomcat.

Re[3]: How do I use JNDI to locate the home interface of an EJB running
on a different host?
Author: Laurent Mihalkovic
(http://www.jguru.com/guru/viewbio.jsp?EID=407112), Jul 17, 2002
Hi

I was just going through the same discovery process with the Borland
AppServer, and found myself at the same place you must have been a while
ago...

I found the following document in the Google search cache.

I found it very interesting as it explains some of the basic mechanisms of the


Borland Naming service. One thing for sure, Borland uses Corba/IIOP as its
underlying communication layer. And unlike LDAP for example, or ven the
SUN reference j2ee server, there does not seem to be syntax for specifying a
Context.PROVIDER_URL property. However, I found the following bit of code
that shows some of the other properties that can be used:

javax.naming.Context context;

// get a JNDI context using the Naming service


Hashtable env = new Hashtable();

env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
"com.inprise.j2ee.jndi.CtxFactory");
env.put(javax.naming.Context.URL_PKG_PREFIXES,
"com.inprise.j2ee");
context = new javax.naming.InitialContext(env);
The documentation states that URL_PKG_PREFIXES is necessary to support the
standard java:comp namespace.

Borland uses a runtime discovery mechanism to identify the servers, and in


all of the examples, a simple ctx = new InitialContext() does the job of
identifying the server dynamically at run-time. My understanding is that there
must be a command line parameter for specifying a specific server when
more than one is present on the network.

Cheers,
laurent

Re[2]: How do I use JNDI to locate the home interface of an EJB running
on a different host?
Author: test sherchan (http://www.jguru.com/guru/viewbio.jsp?EID=962675),
Jul 24, 2002
I am getting the same problem and i am using J2EE server. Can u tell me plz
how to set that env.
Re[3]: How do I use JNDI to locate the home interface of an EJB running on a
different host?
Author: frank xing (http://www.jguru.com/guru/viewbio.jsp?EID=918691), Oct 23, 2002
The way to use JNDI to locate a home interface of an
EJB on remote host differs among different application servers.
As I am using WebLogic6.1, the steps are as follows:
1. at the client side, set the weblogic.jar in your classpath.
2. You have to prepare a Properties object for initialContext, that is:
Properties p = new Properties();
//weblogic's JNDI driver(provider)
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory")

//URL which located the remote server


p.put(Context.PROVIDER_URL,"t3://192.168.153.101:7001");
whereas 192.168.153.101 is my remote server's IP address.
3.Then you construct the InitialContext object
Context ctx = new InitialContext(p);
4.lookup your EJBHome interface:
Object objref = ctx.lookup("com.***.ejb.session.User");
whereas "com.***.ejb.session.User" is your EJB Home interface's JNDI name
5. narrow to the EJBHome
return PortableRemoteObject.narrow(objref, UserHome.class);
that's all you need to do on WebLogic.

And for other application servers, you need to obtain the corresponding JNDI
provider(driver) as step1, set the INITIAL_CONTEXT_FACTORY and PROVIDER_URL as
step2 and then follow step 3, 4, 5.

Hope this will be help.

Re: Re[3]: How do I use JNDI to locate the home interface


of an EJB running on a different host?
Author: Srinivas Tati
(http://www.jguru.com/guru/viewbio.jsp?EID=1022849), Nov 7, 2002
how can I achieve the same result if i dont want to hard code the
hostname . Is there some way of passing the hostname as a parameter ?
Re: Re[3]: How do I use JNDI to locate the home interface
of an EJB running on a different host?
Author: leena sharma
(http://www.jguru.com/guru/viewbio.jsp?EID=1030690), Nov 26, 2002
But if my client application is not using weblogic then also I have to
include weblogic.jar. Is there some other way to lookup the bean
deployed under weblogic server.

Re: How do I use JNDI to locate the home interface of an EJB running on
a different host? [JBoss]
Author: Edison Wang
(http://www.jguru.com/guru/viewbio.jsp?EID=1149288), Feb 25, 2004
I was about to post a new thread on the forums when I see this one... I sort of
understood how the looking up to another host is now, but which namespace
should I put in for this line? I'm also not sure whether the IIOP for JBoss is on
port 1050.
env.put("java.naming.factory.initial",
"com.sun.jndi.cosnaming.CNCtxFactory");

Lookup for jndi resources on different app/ldap servers


Author: Java Developer (http://www.jguru.com/guru/viewbio.jsp?EID=1042536), Jan
2, 2003
Take this scenario: I am using iPlanet's app server. My jndi provider is using LDAP in
the background.
Now I have multiple ejbs each on a different appsever say EJB1 is deployed on
AppServer1 EJB2 is deployed on AppServer2 (and essentially different ldap server)
EJB1 uses EJB2 and so does a lookup of EJB2. In the implementation how do I
seamlessly lookup EJB2 on AppServer2 ?
By seamlessly I mean, is there a configuration in jndi, which does the lookup on a
primary server for a resource and not finding it there does a lookup on a secondary
ldap server ?

How do I create an authenticated session to an LDAP server?


Location: http://www.jguru.com/faq/view.jsp?EID=117303
Created: Aug 2, 2000 Modified: 2000-08-07 10:08:58.382
Author: Steve Czulewicz (http://www.jguru.com/guru/viewbio.jsp?EID=117296)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

The authentication is provided when you create the InitialDirContext. Take a look
at the snippet below...

private Hashtable _env = new Hashtable();


private InitialDirContext _ctx = null;
// Authentication is controlled by the following environment
// All values are Strings
// Refer to javax.naming.Context
// Authentication method. May also be 'none' or 'Strong'

_env.put(Context.SECURITY_AUTHENTICATION, "simple");

// Distiguished name of user to authenticate


_env.put(Context.SECURITY_PRINCIPAL,
"cn=username,ou=orgunit,o=company");

// Password or certificate signature


_env.put(Context.SECURITY_CREDENTIALS, "myPassword");

this._ctx = new InitialDirContext(this._env);


Comments and alternative answers

Can't cn=username,ou=orgunit,o=company go very deep?


Author: neal ravindran (http://www.jguru.com/guru/viewbio.jsp?EID=17737), Mar 5,
2003
Can't cn=username,ou=orgunit,o=company go very deep? Say for example it could go
cn=username,ou=orgunit,ou=anotherOrgUnit,ou=subAnotherOrgUnit, o=company

How would I use that in a corporate env were different users are in different levels?
Iow, how would I be able to write a login program capable of being used by any user?

Re: Can't cn=username,ou=orgunit,o=company go very deep?


Author: Michael Glasson (http://www.jguru.com/guru/viewbio.jsp?EID=1137313),
Jan 15, 2004
If you do not know how to form a DN from the user's login name, you first have to
perform a directory search using the login name. Then you can get the DN from the
search result. This implies either that the directory permits anonymous searches or
your search process has authenticated with its own account which is permitted to
perform the search.

When (manually) deploying an EJB where do you specify the JNDI name
that the bean is bound to in the JNDI tree?
Location: http://www.jguru.com/faq/view.jsp?EID=132923
Created: Aug 23, 2000 Modified: 2000-08-24 18:56:25.582
Author: satyanarayana sairam
(http://www.jguru.com/guru/viewbio.jsp?EID=132906) Question originally posed by
Henrik Buch (http://www.jguru.com/guru/viewbio.jsp?EID=121686

[Questioner continues: According to the EJB1.1 spec there is no relation between the
<ejb-name> tag in the deployment descriptor (or assembly descriptor) and the JNDI
name. I can see that a client can use a "logical" name that can be mapped via
vendor specific deployment info (e.g. orion-application-client.xml).]
When you are deploying the application in using Web Logic 4.5.1 server, in the
deployment wizard you can specify the JNDI name and using the same name you can
look up from the client.

[Is there any standard way to do it? If not, can someone give feedback with info for
other servers? -Alex]

Where can I find the API documentation of the Java Naming and Directory
Interface (JNDI)?
Location: http://www.jguru.com/faq/view.jsp?EID=134137
Created: Aug 24, 2000 Modified: 2000-08-28 19:11:08.331
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

They are available from Sun's J2EE API documentation page.


Comments and alternative answers

I've found the O'Reilly book Java Enterprise in a ...


Author: Michael Dalrymple (http://www.jguru.com/guru/viewbio.jsp?EID=201651),
Sep 9, 2000
I've found the O'Reilly book Java Enterprise in a Nutshell to be a great desktop
resource for JNDI. It has the standard Nutshell API docs as well as a good overview
of how the API is used.

How does one abandon a directory search before completion?


Location: http://www.jguru.com/faq/view.jsp?EID=201813
Created: Sep 10, 2000 Modified: 2000-09-11 15:59:21.056
Author: Robert Castaneda (http://www.jguru.com/guru/viewbio.jsp?EID=4362)
Question originally posed by John Zukowski PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=7

To cancel a search, you simply need to close the NamingEnumeration that is returned
from the context.search() method.

NamingEnumeration enum = ctx.search( "ou=People",


attributes );
if ( enum.hasMore() ) {
//cancel search by closing Naming Enumeration
enum.close();
}

Comments and alternative answers

This does not really 'abandon' a search. In LDAP t...


Author: Vikram Rajan (http://www.jguru.com/guru/viewbio.jsp?EID=5140), Nov 14,
2000
This does not really 'abandon' a search. In LDAP terminology, 'abandon' is a protocol
operation which stops the search on the LDAP directory. In this case, the search has
already been completed and only the search result enumeration is being closed.
Where can I get a JNDI provider for NetWare?
Location: http://www.jguru.com/faq/view.jsp?EID=217160
Created: Sep 27, 2000 Modified: 2000-10-10 15:49:02.867
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7)

You can pick this up with the rest of the Novell Class Libraries for Java from the
Novell Developer Portal.

Are there any complete JNDI service provider (SPI) examples that use
federation?
Location: http://www.jguru.com/faq/view.jsp?EID=223319
Created: Oct 5, 2000 Modified: 2000-10-05 13:32:13.127
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4) Question
originally posed by Shane Jarych
(http://www.jguru.com/guru/viewbio.jsp?EID=130672

Hmm... I don't know if this is "complete" enough for you or not but as part of the
Building a Service Provider section of Sun's JNDI API Tutorial and Reference there is
a lesson which covers Adding Federation Support.

Can I have my JSP container access JSP pages which are not present in a
local file system by doing a JNDI lookup?
Location: http://www.jguru.com/faq/view.jsp?EID=231498
Created: Oct 18, 2000 Modified: 2001-02-13 12:30:53.181
Author: Serge Knystautas (http://www.jguru.com/guru/viewbio.jsp?EID=100012)
Question originally posed by Dmitri Paltchoun
(http://www.jguru.com/guru/viewbio.jsp?EID=41856

There is nothing in the specification that would prevent you from doing this. The
specification defines how requests should get mapped to resources, the syntax and
API for JSPs and servlets, and the internal dispatching of requests within a container.
It does not address where these are stored, whether the servlets come from a
remote class loader or if the JSPs are stored in an LDAP server or other remote
location. In fact, the Tomcat project has toyed with the idea of supporting JSPs that
come from a database.

The problem you will face though is that there are no implementations available yet
that support this. Nonetheless, there are open source projects (such as Tomcat) that
I'm sure would be happy to accept code or a patch that had this functionality where
you to write it.

How can I configure JNDI to use SSL to connect to an LDAP server?


Location: http://www.jguru.com/faq/view.jsp?EID=235601
Created: Oct 24, 2000 Modified: 2000-10-24 16:03:22.526
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4) Question
originally posed by John Prout
(http://www.jguru.com/guru/viewbio.jsp?EID=49355

Check out Sun's JNDI Tutorial's Security chapter, particularly the SSL and Custom
Sockets section.
When using JNDI with LDAP, how can I get the DN (Distinguished Name)
from a DirContext?
Location: http://www.jguru.com/faq/view.jsp?EID=260006
Created: Nov 22, 2000 Modified: 2000-11-30 19:24:03.068
Author: Fdo J. Rendón Quiles (http://www.jguru.com/guru/viewbio.jsp?EID=142691)
Question originally posed by Michael Dalrymple
(http://www.jguru.com/guru/viewbio.jsp?EID=201651

If you get the DN for a DirContext you have to use the method,
getNameInNamespace(). This method retrieves the full name of this context within its
own namespace. For example:

DirContext ctx;
// ...
ctx.getNameInNamespace();
Comments and alternative answers

Did not work !


Author: Ayman Mahfouz (http://www.jguru.com/guru/viewbio.jsp?EID=286051),
Mar 21, 2002
i tried that and it printed out an empty String regards

Did not work


Author: Ayman Mahfouz (http://www.jguru.com/guru/viewbio.jsp?EID=286051),
Mar 21, 2002
i tried that and it printed out an empty String

Re: Did not work


Author: mos mos (http://www.jguru.com/guru/viewbio.jsp?EID=1111000), Aug
26, 2003
You have to use getName(). Like so ...: NamingEnumeration results =
ctx.search(LDAPConnectionValues.SEARCHBASE, cn, constraints); if(results !=
null && results.hasMore()) { SearchResult si = (SearchResult)results.next(); dn =
si.getName()+","+LDAPConnectionValues.SEARCHBASE; }

Re: Did not work


Author: mos mos (http://www.jguru.com/guru/viewbio.jsp?EID=1111000), Aug
26, 2003
You have to use getName(). Like so ...: NamingEnumeration results =
ctx.search(LDAPConnectionValues.SEARCHBASE, cn, constraints); if(results !=
null && results.hasMore()) { SearchResult si = (SearchResult)results.next(); dn =
si.getName()+","+LDAPConnectionValues.SEARCHBASE; }

Re[2]: Did not work


Author: Philip Stockwell
(http://www.jguru.com/guru/viewbio.jsp?EID=1181536), Jun 25, 2004
Thanks for that, been trying to get the DN for ages.
Re[2]: Did not work
Author: Philip Stockwell
(http://www.jguru.com/guru/viewbio.jsp?EID=1181536), Jun 25, 2004
Thanks for that, been trying to get the DN for ages.

Re[2]: Did not work


Author: Philip Stockwell
(http://www.jguru.com/guru/viewbio.jsp?EID=1181536), Jun 25, 2004
Thanks for that, been trying to get the DN for ages.

How can I obtain a JNDI Context from inside an applet?


Location: http://www.jguru.com/faq/view.jsp?EID=288164
Created: Dec 28, 2000 Modified: 2000-12-28 14:05:24.874
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) Question
originally posed by Remith Raghavan
(http://www.jguru.com/guru/viewbio.jsp?EID=222896

To get an initial context from an applet, you need to pass the InitialContext
constructor a Hashtable with the Context.APPLET property ("java.naming.applet")
set:
Hashtable hash = new Hashtable()
// this is a reference to the applet
hash.put(Context.APPLET, this);
Context ctx = new InitialContext(hash);
The following JNDI parameters would then be read from the APPLET parameters:

• java.naming.factory.initial
• java.naming.factory.object
• java.naming.factory.state
• java.naming.factory.control
• java.naming.factory.url.pkgs
• java.naming.provider.url
• java.naming.dns.url

as in
<param
name="java.naming.factory.initial"
value="com.sun.jndi.ldap.LdapCtxFactory"
>

How can I use LDAP with JSP?


Location: http://www.jguru.com/faq/view.jsp?EID=292931
Created: Jan 4, 2001 Modified: 2001-02-13 12:29:40.267
Author: Jorge Jordão (http://www.jguru.com/guru/viewbio.jsp?EID=275762)
Question originally posed by Maher Arif
(http://www.jguru.com/guru/viewbio.jsp?EID=289570

To use LDAP within Java (JSP or not), you must use JNDI (Java Naming and
Directory Interface).
A bit like JDBC, JNDI consists of

• A list of packages: javax.naming, javax.naming.directory,


javax.naming.event and javax.naming.ldap, all included with JDK 1.3 and
also available separately at java.sun.com.
• Service providers (instead of drivers) that actually implement a given naming
or directory service (LDAP, DNS, File system, etc).

Check out the JNDI Tutorial for more information.

Comments and alternative answers

Actually there 2 ways to access LDAP from JSP. You...


Author: mark wilcox (http://www.jguru.com/guru/viewbio.jsp?EID=344899), Mar 5,
2001
Actually there 2 ways to access LDAP from JSP. You can use JNDI or you can use the
Directory SDK for Java from Netscape (www.mozilla.org/directory). The Wrox book,
Professional JSP contains a section on LDAP and JSP (or you can download the code
from www.wrox.com).

Re: Actually there 2 ways to access LDAP from JSP. You...


Author: jsp learner (http://www.jguru.com/guru/viewbio.jsp?EID=890223), Jun
27, 2002
Hi, I am trying to access an ldap server for authentication from a jsp page. I am
able to make the commandline version work, but jsp is giving me the error
ClassDefinitionNotFound Error. So I am guessing it is a configuration issue. I am
using the netscape ldap sdk. What do I have to configure for a Tomcat webserver
for it to recognise this sdk?

Any help would be greatly appreciated.

Thanks.

Re[2]: Using LDAP to authenticate JSP in Tomcat


Author: carl ogden (http://www.jguru.com/guru/viewbio.jsp?EID=947580), Jul
12, 2002
Thats actualy a complicated question. First you need to set up a Realm in
$Tomcat_Home/conf/server.xml that looks something like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn= 2queryldap,cn= Users,dc= warn,dc= com"
connectionPassword="above_users_password"
connectionURL="ldap://server.dns.name:389"
userPattern="cn={0},ou=employees,dc=warn,dc=com"
roleBase="cn=users,dc=warn,dc=com"
roleName="cn"
roleSearch="(member={0})"
userPassword="givenName" <-- what field usersPassword is stored in
roleSubtree="false" />

This assumes the password you want to use is stored in plain text or in digest
format that java can work with. How Tomcat currently works is it grabs the
password from ldap either in plain text or in a digest password, then it
compares the user submited password witht the retrieved password if they
match then it goes and looks to see if what groups (roles) the user is in. This
only works if:
1) The LDAP server is using a common Digest like SHA or MD5
2) The userPassword field isn't write_only, which it is in Microsoft Active
Directory.
Tomcat's JNDIRealm doesn't use a traditional ldap approach of trying to bind to
the directory to validate a user. If you want or need to do it this way it gets
more complicated.

To make all this work you must have JNDI setup and on the classpath
somewhere and the ldap.jar that comes with jndi somewhere on the classpath, if
you really want to use netscape's ldapjdk it gets alot more complicated.

if the above does all work for you then you need to tell tomcat what to protect
per container. So in the: $Tomcat_Home/webapps/app_name/WEB-
INF/web.xml create something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web


Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>PUT_GROUP_NAME_HERE</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Secure Realm</realm-name>
</login-config>

</web-app>

any changes to theese file requires a restart. LDAP debug information can be
found in:
$Tomcat_home/logs/catalina_log.2002-07-12.txt for what ever day it is.

once the user is authenticated you can get information about the user from the
supplied request object:
request.getUserPrincipal();
request.isUserInRole("some group name");
request.getUserPrincipal().getName();

If you want/need to use the Netscape ldapjdk.jar or you need to bind to the ldap
directory because of a non standard digest format or userPassword is a
write_only then you need do the following.

1)Create a new realm there is no documentation to do this so you got to go to


the tomcat source, the easyiest way is to make a copy of
org.apache.catalina.realm.JNDIRealm.java leave it in the same package, if you
want to use Netscape's ldapjdk you need to change:
contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
with the
contextFactory="com.netscape.jndi.ldap.LdapContextFactory"
and put ldapsp.jar on the classpath

to make it use the more common bind technique for authentication you need to
rewrite/modify the
protected String getUserDN(DirContext dircontext, String s, String s1) funtion
s = username from browser
s1 = password user typed
Function needs to return users Full DN if authenticated or null if not.

compile your new class and put it in:


$Tomcat_home/server/classes/org/apache/catalina/realm
change your server.xml
className="org.apache.catalina.realm.JNDIRealm" to
match your new class name and restart.

its easyer than it sounds.

Carl

Re[3]: Using LDAP to authenticate JSP in Tomcat


Author: Michael Glasson
(http://www.jguru.com/guru/viewbio.jsp?EID=1137313), Jan 15, 2004
Carl said, "How Tomcat currently works is it grabs the password from ldap
either in plain text or in a digest password, then it compares the user
submited password witht the retrieved password..." This is not actually
true. The JNDI Realm happily uses the bind method, at least the one in
Tomcat 5 does.

Re[3]: Using LDAP to authenticate JSP in Tomcat


Author: José Miguel Loor
(http://www.jguru.com/guru/viewbio.jsp?EID=1143982), Feb 5, 2004
Hi: I tryed your realm configuration XML Object but everytime i stop / start
the service and opens an Browser window with http://localhost:8080/ the
Apache Service stops. It just hangs up What can i be doing wrong ? Thanks

Re[4]: Using LDAP to authenticate JSP in Tomcat


Author: Ken Young
(http://www.jguru.com/guru/viewbio.jsp?EID=901514), Apr 30, 2004
Did you check the error logs?

Re[3]: Using LDAP to authenticate JSP in Tomcat


Author: M. Saquib Kazi
(http://www.jguru.com/guru/viewbio.jsp?EID=1221778), Jan 18, 2005
Can u please provide some specifications for using the same code on JRun
Server

Re[3]: Using LDAP to authenticate JSP in Tomcat


Author: Egon Müller-Lüdenscheid
(http://www.jguru.com/guru/viewbio.jsp?EID=1238196), Apr 13, 2005
Hello Carl , You wrote : "LDAP debug information can be found in:
$Tomcat_home/logs/catalina_log.2002-07-12.txt" . I read that file , and it
contains one line for JNDIRealm: "JNDIRealm[Jira-engine]: Connecting to
URL ...." . I had expected to find more information about the many steps in
Tomcat evaluating & executing the many attributes of the <Realm> element
. I had expected to find lines like lookupUser... ...successfully authenticated
returning 1 role ... What exactly do I have to configure in order to get these
debug messages ? Regards , Egon

In using DirContext.search() and using the password field as a filter, I'm


getting a NamingException with "no such algorithm" as the message. What
does that mean?
Location: http://www.jguru.com/faq/view.jsp?EID=316475
Created: Jan 30, 2001 Modified: 2001-02-13 17:08:02.962
Author: Nicholas Whitehead (http://www.jguru.com/guru/viewbio.jsp?EID=1260)
Question originally posed by Tony Karam
(http://www.jguru.com/guru/viewbio.jsp?EID=305982

I suspect that what your JNDI provider is telling you is that it cannot decrypt the
password in order to compare it to the value that you have supplied. I would cease
using the password as a filter.

How can I get the cleartext version of a password through JNDI?


Location: http://www.jguru.com/faq/view.jsp?EID=344900
Created: Mar 5, 2001 Modified: 2001-03-15 15:51:05.691
Author: mark wilcox (http://www.jguru.com/guru/viewbio.jsp?EID=344899)
Question originally posed by Kunwar Asheesh Saxena
(http://www.jguru.com/guru/viewbio.jsp?EID=324415

You can't, unless the password is stored in cleartext in the directory (which would be
a bad idea). The passwords are encrypted using a strong 1-way hash function such
as MD5 or SHA1 (SHA1 being the most common and preferred function).

Are there any JNDI service providers which support Micrsoft's Windows
security/authentication services?
Location: http://www.jguru.com/faq/view.jsp?EID=344902
Created: Mar 5, 2001 Modified: 2001-03-15 15:50:20.522
Author: mark wilcox (http://www.jguru.com/guru/viewbio.jsp?EID=344899)
Question originally posed by John Mitchell PREMIUM
(http://www.jguru.com/guru/viewbio.jsp?EID=4

Microsoft's sytsems can be broken down into 5 discrete areas:

1. Messaging -- Exchange has a quite capable LDAP interface


2. NT Domains -- There's no way to get access to NT domains from JNDI
currently
3. ActiveDirectory -- Active Directory is an LDAP capable system
4. Windows 2000 Authentication -- Win2K authentication via ActiveDirectory is
through Kerberos. So JNDI isn't valid here, that's a JAAS issue.
5. Windows 2000 DNS -- You should be able to manage Windows 2000 DNS
system either from LDAP through AD or via the JNDI DNS provider.

Comments and alternative answers

Is there any source code to access MS Active Directory?


Author: kvu kle (http://www.jguru.com/guru/viewbio.jsp?EID=460337), Jul 30, 2001
Is there any source code to access MS Active Directory? or Exchange. I'm trying to
create email accounts through Exchange server. Can this be done through JNDI.

Re: Is there any source code to access MS Active Directory?


Author: Brandon Smith (http://www.jguru.com/guru/viewbio.jsp?EID=872401),
May 9, 2002

I too am looking for some code to access AD using jndi. If you have solved this it
would be helpful! Sorry I can't answer. Thanks

Re[2]: Is there any source code to access MS Active Directory?


Author: Balla Niang (http://www.jguru.com/guru/viewbio.jsp?EID=875338), May 12, 2002
hi here is a simple code example. Customize it to your system /* Retourne des attributs bien determ
d'un objet apres l'avoir chercher en fournissant certains de ces attributs. */ import javax.naming.*;
import javax.naming.directory.*; import java.util.Hashtable; public class Search { public static voi
printSearchEnumeration(NamingEnumeration enum) { try { while (enum.hasMore()) { SearchRes
= (SearchResult)enum.next(); System.out.println(">>>" + sr.getName());
GetattrsAll.printAttrs(sr.getAttributes()); } } catch (NamingException e) { e.printStackTrace(); } }
public static void main(String[] args) { // rechargement de l'environnement de l'initialisation du co
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://digactive:389/dc=intraucad,dc=ucad,dc=sn");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"cn=administrateur,cn=users,dc=intraucad,dc=ucad,dc=
// specify the username env.put(Context.SECURITY_CREDENTIALS,"digactive"); // specify the
password try { // Crer le context initial DirContext ctx = new InitialDirContext(env); // Specifie le
des attributs à retourner String[] attrIDs = {"sn", "telephonenumber", "mail"}; // Specifie les attrib
que doit avoir l'objet // cherché ici l'attribut ("sn") // avec la valeur "titi" // et un attribut "mail" .
Attributes matchAttrs = new BasicAttributes(true); // ignore case matchAttrs.put(new
BasicAttribute("sn", "titi")); matchAttrs.put(new BasicAttribute("mail")); // Cherche parmi les obj
"toto" ceux ayant de tels attribut NamingEnumeration answer = ctx.search( "ou=toto",matchAttrs,
attrIDs); // Imprimer la reponse printSearchEnumeration(answer); // Clore le context ctx.close(); }
(Exception e) { e.printStackTrace(); } } }

Where can I find a JNDI service provider which uses an SQL database as its
backing store?
Location: http://www.jguru.com/faq/view.jsp?EID=344908
Created: Mar 5, 2001 Modified: 2001-03-15 15:47:01.873
Author: mark wilcox (http://www.jguru.com/guru/viewbio.jsp?EID=344899)
Question originally posed by Fredrik Hultkrantz
(http://www.jguru.com/guru/viewbio.jsp?EID=265955

The answer to your question will depend upon what you're trying to do. If you're
looking for a LDAP server with a SQL server, then you should probably check out
Oracle's OID system which is Oracle's LDAP server. Most directory servers don't use
SQL because SQL is ineffecient for LDAP and unecessary. Most people believe you
need an SQL server for transactions to keep things stable. But there are several
flavors of embedded databases like Sleepycat's Berkeley Database (used by IPlanet
among others) which provides for large databases that are extremely quick and still
provide thing like transaction support (IPlanet uses this feature) so your directory
server can recover its databases from a crash.

Next, SQL is a relational model, while JNDI supports a heiararchical model. Perhaps
the better question is there an Object Database that supports JNDI? (And the answer
is not that I know of, but they *could* exist)

If you're looking for a better object support for your Java applications from your
databases, then you you should be looking into an object database or an API that
allows object wrappers around JDBC ops.

Comments and alternative answers

Regarding better object support for accessing pers...


Author: David Smiley (http://www.jguru.com/guru/viewbio.jsp?EID=347254), Mar
16, 2001
Regarding better object support for accessing persistent information... isn't JNDI the
closest J2EE standard that does this? For this reason I am considering making a JDBC
SPI. The only down side I see is that there would be another object wrapping layer. --
David Smiley MITRE

Re: Regarding better object support for accessing pers...


Author: David Sanders (http://www.jguru.com/guru/viewbio.jsp?EID=237928),
Aug 25, 2001
Will you share your JDBC SPI with me? I want to do simple lookups. I want
something simple. I could use an RMI SPI but there is no persistence if the RMI
registry or its host fails.

Re: Re: Regarding better object support for accessing pers...


Author: David Smiley (http://www.jguru.com/guru/viewbio.jsp?EID=347254),
Aug 25, 2001
I said that I considering building one; I didn't :-/

Is there a JNDI service provider for NIS+?


Location: http://www.jguru.com/faq/view.jsp?EID=351568
Created: Mar 15, 2001 Modified: 2001-03-15 14:45:15.031
Author: raghavendra ghorpade
(http://www.jguru.com/guru/viewbio.jsp?EID=344479) Question originally posed by
Anthony Ng (http://www.jguru.com/guru/viewbio.jsp?EID=294982

As for now there is no sevice provider for NIS+ but there is JNDI sevice provider for
NIS that is -- com.sun.jndi.nis.nisctyfactory.

How can I retrieve the results of a LDAP search in pages and also define
number of entries on each page?
Location: http://www.jguru.com/faq/view.jsp?EID=353054
Created: Mar 16, 2001 Modified: 2001-03-16 10:02:36.425
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4) Question
originally posed by venkat ravula
(http://www.jguru.com/guru/viewbio.jsp?EID=279259

Set the Context.BATCHSIZE property before you create the context:


// Set the batch size to 25...
env.put ("java.naming.batchsize", "25");

DirContext dc = new InitialDirContext (env);


//...

What's the difference between JNDI lookup(), list(), listBindings(), and


search()?
Location: http://www.jguru.com/faq/view.jsp?EID=353071
Created: Mar 16, 2001 Modified: 2001-03-16 10:07:33.094
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)
lookup() attempts to find the specified object in the given context. I.e., it looks for a
single, specific object and either finds it in the current context or it fails.

list() attempts to return an enumeration of all of the NameClassPair's of all of the


objects in the current context. I.e., it's a listing of all of the objects in the current
context but only returns the object's name and the name of the class to which the
object belongs.

listBindings() attempts to return an enumeration of the Binding's of all of the


objects in the current context. I.e., it's a listing of all of the objects in the current
context with the object's name, its class name, and a reference to the object itself.

search() attempts to return an enumeration of all of the objects matching a given


set of search criteria. It can search across multiple contexts (or not). It can return
whatever attributes of the objects that you desire. Etc. It's by far the most complex
and powerful of these options but is also the most expensive.

Comments and alternative answers

Help
Author: Stefano Balloni (http://www.jguru.com/guru/viewbio.jsp?EID=1112879), Sep
4, 2003
I need a method to extract every deployed ejb because if i try to lookup my ejb i
receive an error. It says that the name of the ejb that i serch for is wrong. I'm sure that
the name is rigth... how can i get all the names of my deployed ejb? List() method
requires a string but i don't know what he want!! Help please!

Where can I learn (more) about Application Servers?


Location: http://www.jguru.com/faq/view.jsp?EID=431183
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru AppServer FAQ.

Where can I learn (more) about Java's EJB (Enterprise JavaBeans)?


Location: http://www.jguru.com/faq/view.jsp?EID=431189
Created: May 30, 2001 Modified: 2001-06-15 23:12:08.671
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru EJB FAQ.

Where can I learn (more) about sending email in Java and the JavaMail
library?
Location: http://www.jguru.com/faq/view.jsp?EID=431202
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru JavaMail FAQ.


Where can I learn (more) about Sun's Jini network technology?
Location: http://www.jguru.com/faq/view.jsp?EID=431208
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru Jini FAQ.

Where can I learn (more) about Java's support asynchronous and


publish/subscribe messaging using JMS (Java Message Service)?
Location: http://www.jguru.com/faq/view.jsp?EID=431210
Created: May 30, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru JMS FAQ.

Where can I learn (more) about Java's support for transaction processing?
Location: http://www.jguru.com/faq/view.jsp?EID=431948
Created: May 31, 2001
Author: John Mitchell (http://www.jguru.com/guru/viewbio.jsp?EID=4)

Check out the jGuru Transactions FAQ.

How do I initiate a JNDI lookup from an applet?


Location: http://www.jguru.com/faq/view.jsp?EID=739382
Created: Jan 29, 2002
Author: Luigi Viggiano (http://www.jguru.com/guru/viewbio.jsp?EID=101985)
Question originally posed by Sanjit Singh
(http://www.jguru.com/guru/viewbio.jsp?EID=533804

First, in the jar file of your applet you will need followin classes included:

1. Classes from the jar with the client portion of your specific jndi provider
classes
2. Classes from the the jar containing javax.naming standard extensions.
3. Classes from the jar containing javax.rmi.PortableRemoteObject (only if the
retrieved object are CORBA objecs)
4. The classes you want to lookup.

Then, the host where the lookup has to be done must be the same from wich your
applet has been downloaded (otherwise the applet must be signed).
The client browser must be able to open a connection to the port of where the
naming service is listening (be sure about proxy settings etc.).
The client browser must have Java 2 (Java Plugin 1.2.x)
Then the code is always the same:

Properties props = new Properties();


props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory" );
props.setProperty("java.naming.provider.url", "codebaseurl:1099");
Context ctx = new InitialContext(props);
Object ref = ctx.lookup("jndi_name");
Note: it's generally avoided to use applet working on remote object, for such many
difficulties and for applet security limitations, so it's better to think about applet
asking services to server object thru servlets.

Das könnte Ihnen auch gefallen