tags:

views:

28

answers:

1

Today I was presented with this exception

Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext  [Root exception is java.lang.NullPointerException]
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
        ... 23 more

I was trying to remotely access an ejb, the offending code was

Context c = new InitialContext();

Ive seen this exception before and fixed it but couldn't remember exactly how I did it. I knew that I had to set some environment variables for the initial ,context url and service provider or some such stuff.

In fact I managed to find the code I used to fix this issue last time i had it, it is as follows.

Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
Context c = new InitialContext(env);

My question is this how can you find out what initial context factory to use? I've written an ejb module for our database that runs on glassfish v3, at no point did I get any hint that of course I should use the com.sun.enterprise.naming.SerialInitContextFactory, I mean like its so obvious. Who makes these context factories? Who decides which one I have to use and why? Why isn't there a list showing which one is required for different purposes? It seems like someone's gone out of their way to make the most impenetrable and cryptic method of accessing a resource that is humanly possible. Or I have completely misunderstood something here or am lacking a huge chunk of knowledge.

I would very much appreciate some enlightenment on this subject.

Thank you all.

A: 

Normally JNDI looks for its configuration in a jndi.properties file in the class path.

Meybe there is a rogue jndi.properties file misdirecting you.

For more details see https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

Peter Tillemans
Thanks for the tip, I have now got rid of the INITIAL_CONTEXT_FACTORY variable which is simplifying my code a bit, but as stated in the link you provided some vendors do require bootstrapping properties to InitialContext(args), so how would I go about finding out what properties to set in the instance that I was using a vendor other than glassfish, and while we're on the subject what is a vendor? Thanks Piers
PiersyP
A vendor is the organisation 'selling' the product. For glassfish that would be Sun/Oracle, for JBoss it is Redhat, etc... Finding these parameters is a nightmare. With JBoss it is in the manuals, but I dare you to find it. The best way is to look in the example directories and see what is used there.
Peter Tillemans
AAAAArrrrggghhhh!!!!!
PiersyP