4

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.

Thufir
  • 8,216
  • 28
  • 125
  • 273
PiersyP
  • 5,003
  • 2
  • 33
  • 35

3 Answers3

3

glassfish settings for jndi.properties for reference:

java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
Context.SECURITY_PRINCIPAL=user1
Context.SECURITY_CREDENTIALS=pass123
org.omg.CORBA.ORBInitialHost=localhost
org.omg.CORBA.ORBInitialPort=3700

1.) The jndi.properties file is loaded when the default InitialContext constructor is invoked. To my mind, this is preferable from hard-coding these values, creating a Properties object, etc.

2.) These connection parameters work for me with Glassfish running locally. I cobbled this together from various sources.

3.) I concur with the spirit the question: where in the fine manual are these specified? I've seen some mention of them in the manual, but they're not all in one location -- at least for glassfish. It doesn't help that the 4.x glassfish manual is only available via PDF.

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • The two CORBA properties are the defaults - you only need to specify them if the values will be different to the ones shown. Also, I believe the security properties should only be necessary if you're accessing Glassfish from a remote host? – Michael Oct 15 '15 at 11:05
1

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
  • 34,983
  • 11
  • 83
  • 114
  • 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 Aug 19 '10 at 11:47
  • 1
    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 Aug 19 '10 at 12:55
0

Chk if the jndiContext.lookup(???) is specified correctly.

Malar
  • 1