Okay, Hi, I was having very similar problems for ages and solved it through brute force.
I don't know the book your talking about, so the initial context code could be wrong AFAIK. The code I use is thus:
private static Properties props = null;
props = new Properties();
props.setProperty( "java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty( "java.naming.provider.url","localhost:1099");
props.setProperty( "java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
InitialContext jndiContext = new InitialContext( props );
And the code I use to retrieve an EJB from a remote client is thus:
Object ref = jndiContext.lookup( "java:"+jndiName );
System.out.println("EXTERNAL LOOKUP");
// Get a reference from this to the Bean's Home interface
return (EJBHome)PortableRemoteObject.narrow( ref, homeRefClass );
And the following is for retrieving an EJB from within another EJB hosted on the same EAP (JBoss in this case, of course):
Object ref = jndiContext.lookup( "java:comp/env/"+jndiName );
System.out.println("INTERNAL LOOKUP");
// Get a reference from this to the Bean's Home interface
return (EJBHome)PortableRemoteObject.narrow( ref, homeRefClass );
Please note that the jndiName variable above will be whatever was entered into the <jndi-name></jndi-name> tag for the first code block and whatever was entered into the <ejb-ref-name></ejb-ref-name> inside the ejb-jar.xml of the session bean's <ejb-ref></ejb-ref> block for the second code block above.
In brief, binding takes place with no amendment if you are a remote client, but the value 'comp/env/' is added for retrieval within beans.
And lastly, if you place a simple
jndi.properties
file, in the root directory within your output jar of the remote client, which contains the three lines from the Properties or hash object you are passing into the constructor of the InitialContext, you will no longer have to create that Properties or hash object.
If you are writing a
servlet you will still have to pass in the Properties object because I haven't figured out where the servlet is supposed to read the jndi.properties file from. If anyone finds out I would love to know.
Good luck, hope that wasn't too congealed for you,
Best regards everyone,
Matt.