Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JNDI problem

Status
Not open for further replies.

sergps

Programmer
Jun 12, 2003
12
US
Hi, I wanto to access an object from a remote app, the client app which would perform the lookup will have to have a interface declaration of the object so it could try to cast when performing the look up, but then how do you get the implementation of the interface?, I've been reading that you can achieve it by specifying the codebase in the DirContext, but I cannot create the DirContext I'm using the following code:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
ctx = new InitialDirContext(env);
DirContext next = null;
StringTokenizer st = new StringTokenizer("tms-svc");
BasicAttributes battr = new BasicAttributes("javaCodebase","ctx.bind("tms-svc", timesSvc, battr);

I guess that the problem is that there is no ldap service enable or something and I haven't found anything about this, can someone help?, thanks in advance.
 
First, you obviously need to have a LDAP server listening on the port you expect. I expect you don't have an Active Directory server on localhost.

When I was playing around with LDAP & Active Directory, here is the code I started with - it illustrates some techniques, and may be of some use :

Code:
import javax.naming.directory.*;
import javax.naming.*;
import java.util.*;

public class ActDir {

	public static void main(String args[]) throws Exception {



		Hashtable env = new Hashtable();
		DirContext ctx;

		String port = args[1];
		String host = args[0];
		String user = "corp\\someLoginName";
		String passwd = "somepassword";

		env.put(Context.SECURITY_AUTHENTICATION,"simple");
		env.put(Context.SECURITY_PRINCIPAL, user);
		env.put(Context.SECURITY_CREDENTIALS, passwd);

		//env.put("java.naming.ldap.version", "3");
		//env.put(Context.REFERRAL, "follow");

		String url = new String("ldap://"+host+":"+port);
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL,url);

		System.err.println("Connecting to " +url);
		ctx = new InitialDirContext(env);

		//ctx.bind(user, ctx, null);

		System.err.println("Connected to " +url);

		// Read supportedSASLMechanisms from root DSE
		Attributes attrs = ctx.getAttributes(url,new String[]{"supportedSASLMechanisms"});
		System.out.println(attrs);

		Attributes answer = ctx.getAttributes("OU=People,OU=Bristol,DC=corp,DC=foobar,DC=net");

		// Specify the attributes to match
		// Ask for objects that has a surname ("sn") attribute with
		// the value "Bloggs" and the "mail" attribute
		Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
		matchAttrs.put(new BasicAttribute("sn", "Bloggs"));
		matchAttrs.put(new BasicAttribute("mail"));

		// Search for objects that have those matching attributes
		NamingEnumeration ne = ctx.search("OU=People,OU=Bristol,DC=corp,DC=foobar,DC=net", matchAttrs);

		while (ne.hasMoreElements()) {
			System.out.println(ne.next());
		}


	ctx.close();
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for your response, do you know how I can set a server so it'd be listening to my ldap requests?, I'm currently working with Jboss and I can't find any info regarding of how to enable the ldap feature, thanks..
 
You need an LDAP server like Microsoft Active Directory or openldap - .

If you don't have a current LDAP server & data to connect to ... then why would you want to use LDAP ??

--------------------------------------------------
Free Database Connection Pooling Software
 
Well, I'm really new in LDAP and I'm on the need of binding in a JNDI name an object along with its class definition, so it can be used by any code that invokes the jdni name... do you know another work-around for that?
 
Hello, thanks for your response, well when I created the DirContext with DirContest ctx = new InitialDirContext(); and then I try to bind the object to a JNDI name I get 12:29:51,590 INFO [STDOUT] javax.naming.NotContextException: Not an instance of DirContext, and I'm using the DirContext because I'm binding the service to a jndi name, but I need to send the class implementation as well so when it's cast in the client application (which only has a interface of the object) it'd know the implementions of the methods declared in the interface, that's why I'm using the line of code you mentioned as second, I read that was the way to send the class definition along with the object.. but I cannot find anything in the tutorials, could you help?, thanks.


 
I don't really understand why you cannot just bind your object to the standard JNDI context - ie within JBoss :


Context c = new InitialContext();

--------------------------------------------------
Free Database Connection Pooling Software
 
It's because I'm using that object in another application which is in other server.. so the class implementation needs to be known and we're only supposed to have the interface declaration in the client app... when I cast the object in the client app will cast the object resulting from the lookup and then the methods of that object will be used... do you think this makes sense?
 
What exactly do you mean by "class implementation" ?

--------------------------------------------------
Free Database Connection Pooling Software
 
I meant the class which implements the interface and contains all the code & behaviors in the methods of the class.

 
Well if you bind that object, that is the object you can access. A class that implements an interface doesn't "lose" its methods etc just because you bind it under JNDI.

Remember that all that JNDI is, is basically a Hashtable. There is a name, and that name just points to another object. There is nothing special about JNDI at all - its very very simple to write a JNDI implentation, and I have done in the past.

I think you want to just bind under the standard context like I said before and forget this "class implementation" stuff - you are just confusing yourself.


--------------------------------------------------
Free Database Connection Pooling Software
 
For example, when you say :

Context ctx = new IntialContext();

Your Context object, though specified as an interface (javax.naming.Context) is not actually that -the actual class will be something like org.jboss.naming.StandardContext or something like that (under JBoss) ... do you see ?

--------------------------------------------------
Free Database Connection Pooling Software
 
It's just that I was thinking that I could make available this object through remote servers, but now I'm not sure
 
You can, as long as the object is serializable !!!

I suggest reading up on the JBoss tutorial on remote JNDI contexts (and forget about LDAP & DirContext - you don't need it).

Use these properties :

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:eek:rg.jnp.interfaces
java.naming.provider.url=serverName

--------------------------------------------------
Free Database Connection Pooling Software
 
Great, could you please post url of the Jboss & JDNI tutorial you're metioning?
 
oh ok, thanks a lot for all your help, I really appreciate it.

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top