• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

InitialContext and JNDI confusion

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little new to the whole EJB thing and I'm just trying to get a helloworld session bean to work and have hit a wall.
I'm using the 1.4 Beta J2EE SDK from Sun. I ran the asadmin server and the server starts up ok. I point my browser to localhost:4848 and I get the display page. Check. I also deployed their sample jsp page and that worked too so I'm assuming the server is running and working the way it should.
Now for the part that doesn't work. I made the helloworld session bean and then made my TestClient program. Now, in the documentation it says that when you use the deploytool on your .ear file (which I made with the delpoytool) you need to get it to return a client jar. There is a little check on the deploytool for this operation and I check the box when its deploying my .ear file. Now, shouldn't I get a .jar file in return? NO matter what I type in that stupid field I can't get it to produce a jar file. From what I read, I need to include this .jar file in the classpath when I run the client or it won't be able to connect to the sessionbean.
Now, after about 9 hours of playing with the stupid thing, I finally get it to produce a .jar file for me. But, I called it helloClient.jar and it gives me helloworldClient.jar? I'm totally confused. But anyway, I include the stupid .jar file in my classpath for my TestClient program anyway, just to see what happens.
I run my TestClient and it throws an InitialContext error. Says I need to pass it an initialcontxt. Alright. I create a hashtable and pass it 3 different values in 3 different tests to see what happens.
I pass the following,
com.sun.jndi.rmi.registry.RegistryConextFactory
com.sun.jndi.cosnaming.CNCtxFactory
com.sun.jndi.ldap.LdapCtxFactory
they all throw a different exception but what its saying is it can't connect on port 389 I believe it is. Firewall is disabled so this shouldn't be a problem.
Now, reading through some books and things, they tell me you have to supply your own InitialContext which is dependent on what server you're running.
Well, I can't find this anywhere in any documentation for Sun's J2EE asadmin server. When you run that asadmin server tool, what is the default InitialConext? Is it running an LDAP by default? I can't find it.
Or is there no naming service included at all and I have to download something else yet on top of it? You'd think they'd have a naming service included.

Here's the TestHello Program. NOthing earth shattering
***********************************************************
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
public class TestHello
{
public static void main(String[] args)
{
try
{
/** Creates a JNDI naming context for location objects */


Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

//com.sun.jndi.rmi.registry.RegistryContextFactory
//com.sun.jndi.cosnaming.CNCtxFactory
//com.sun.jndi.ldap.LdapCtxFactory


Context context = new InitialContext(env);



/** Asks the context to locate an object named "HelloWorld" and expects the
object to implement the HelloWorldSessionHome interface */

HelloWorldSessionHome home = (HelloWorldSessionHome)
PortableRemoteObject.narrow(
context.lookup("HelloWorld"),
HelloWorldSessionHome.class);

/** Asks the Home interface to create a new session bean */
HelloWorldSession session = (HelloWorldSession) home.create();
System.out.println("The default greeting is: "+
session.getGreeting());
session.setGreeting("Howdy!");
System.out.println("The greeting is now: "+session.getGreeting());
/** Destroy this session */
session.remove();
/** Now create a session with a different greeting */
session = (HelloWorldSession) home.create("Guten Tag!");
System.out.println("Created a new session with a greeting of: "+
session.getGreeting());
/** Destroy this session */
session.remove();
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}
***********************************************
Here is the stacktrace once the client is provided with an InitialContext.
I'm running this from Eclipse 2.0 and 1.4.1 JDK.

javax.naming.CommunicationException: localhost:389. Root exception is java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:434)
at java.net.Socket.connect(Socket.java:384)
at java.net.Socket.<init>(Socket.java:291)
at java.net.Socket.<init>(Socket.java:119)
at com.sun.jndi.ldap.Connection.createSocket(Connection.java:346)
at com.sun.jndi.ldap.Connection.<init>(Connection.java:181)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:119)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1668)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2556)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:275)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:53)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.init(InitialContext.java:219)
at javax.naming.InitialContext.<init>(InitialContext.java:195)
at TestHello.main(TestHello.java:23)

Help a noob please.
Thanks.
 
Mike Scherr
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would not use "com.sun.jndi.ldap.LdapCtxFactory" for this. Usually you have to specify a LDAP server address with that (unless it's local). I think you need to look into the server and see how it binds the interface, and what ctxfatcory to use... Doesn't sound like it uses LDAP. Sorry - I have no details. Please post a solution when found!!!
Maybe you could use JBoss instead. It's pretty straight forward. There is lots of support and documentation.
 
Mike Scherr
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I downloaded Jboss but using a new server seems like a roundabout way of solving the problem. In fact, when I've solved it, I'll probably just install and use Jboss anyway but I'd still like to know. I'm sure someone somewhere must know what the default InitialContext of Sun's J2EE server is. Yes I'm running this on localhost.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike, the J2EE SDK discussion forum at forum.java.sun.com might have what you're looking for.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using this instead
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
or try this
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
I think the latter should work for you.
wish u luck
Regards
Harjot
 
Author
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,
Yes this junk can be very frustrating
I think the to keep in mind here is that the InitialContext is expecting for you to tell it which class to instanciate as well as any other connection parameters that are needed to open an connection and work with the JNDI provider. Since it can be just about anything it is often very hard to get the client side config correct.
Something I often do when running a new sever is grab the jndi.properties file to discover what real properties are required to be set for the initial context to run. JBoss 3.2 has them in its sever conf directories (i.e. $JBOSS/server/default/conf/jndi.properties). Other app servers do other thing.
This document JNDI Tutorial describes the various ways you can get the JNDI env set up properly.
The J2EE RI (1.3.1 & 1.4) from sun has a jndi.properties file in the $RI_HOME/lib/j2ee.jar file.
The values are
java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
If you put this j2ee.jar file in your classpath you should be able to use simply

on the client side. If you can't or don't want j2ee.jar on your client classpath then you can specify the two properties like this

Hope this helps.
[ September 23, 2003: Message edited by: Bill Dudney ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic