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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with SUN JNDI tutorial example

Status
Not open for further replies.

sergps

Programmer
Jun 12, 2003
12
US
Hi guys there's an example of how to bound an object along with its codebase in the sun jndi tutorial

import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

/**
* Demonstrates how to bind a Serializable object to a directory
* with a codebase.
* (Use Unbind to remove binding.)
*
* usage: java SerObjWithCodebase <codebase URL>
*/

class SerObjWithCodebase {
public static void main(String[] args) {

if (args.length != 1) {
System.err.println("usage: java SerObjWithCodebase <codebase URL>");
System.exit(-1);
}

String codebase = args[0];

// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

try {
// Create the initial context
DirContext ctx = new InitialDirContext(env);

// Create object to be bound
Flower f = new Flower("rose", "pink");

// Perform bind and specify codebase
ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase));

// Check that it is bound
Flower f2 = (Flower)ctx.lookup("cn=Flower");
System.out.println(f2);

// Close the context when we're done
ctx.close();
} catch (NamingException e) {
System.out.println("Operation failed: " + e);
}
}
}

So in therory you should be able to retrieve an object in through doing a look up, and then cast it... but in this approach the client that performs the look up shouldn't need to include the class definition of the object that is being retrieved, because it's being included in the code base of the context, but then how do you avoid compilation errors such:

SerObjWithCodebase.java:73: cannot resolve symbol
symbol : class Flower
location: class SerObjWithCodebase
Flower f2 = (Flower)ctx.lookup("cn=Flower");

in the client code that is attepting to perform the look up?... is there a work-around for this problem? please let me know, thanks in advance.
 
If the object you're looking up implements a common interface, like DataSource, you can cast it to that interface and still access its functionality.

Does that resolve your problem?

haslo@haslo.ch - www.haslo.ch​
 
Hello, so you mean the client application needs to have an interface of what I'm trying to cast?, I thought about it before but just wanted to be sure that there were other options, I think there is no other way right?, what you think, thanks.
 
The client application has to have a clue about what the casted object looks like, otherwise compile-time error checking is not possible at all.

If you intend to circumvent that, you'll have to look into the Reflection API (you can call classes, functions and just about everything by means of its name as a string with that API), but that's expensive in computing time and not really efficient. But if you have no idea as to what the targeted object looks like at compile time, you can do it like that.

haslo@haslo.ch - www.haslo.ch​
 
Thanks a lot for your message, you're right, I'll do it in that way,

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top