How to get initial context from GlassFish server in Java SE?

◇◆丶佛笑我妖孽 提交于 2019-12-04 12:35:01

In order to use JNDI you need to specify the java.naming.factory.initial somehow, just like the error message says.

There are multiple ways of doing this:

You could specify it as a system property in Glassfish, through server (Admin server) -> Properties

Alternatively, you could specify it in a HashTable and pass it to the constructor of InitialContext:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,  
    "com.sun.enterprise.naming.SerialInitContextFactory");

Context ctx = new InitialContext(env);

If you use Spring you could also do this:

<bean id="myJndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">com.sun.enterprise.naming.SerialInitContextFactory</prop>
            <prop key="java.naming.factory.url.pkgs">com.sun.enterprise.naming</prop>
            <prop key="java.naming.factory.state">com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl</prop>
        </props>
    </property>
</bean>

See http://docs.oracle.com/javase/jndi/tutorial/beyond/env/context.html for more information.

As far as the actual values go, the Spring config above is what we actually use with Glassfish. We do not specify provider url or credentials..

I don't think this is really connected to creating an ldap-realm, Glassfish might use JNDI to lookup the realm though.

Edit:

I think I might understand what the problem is, you are trying to access remote classes from a client. With this assumption, you can use Spring to do this, with JndiTemplate. Assuming that the server makes available the correct EJB-classes, do this on the client side:

Create a bean for JndiTemplate:

  <bean id="myJndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
      <props>
        <prop key="java.naming.factory.initial">com.sun.enterprise.naming.SerialInitContextFactory</prop>
        <prop key="org.omg.CORBA.ORBInitialHost">${servername}</prop>
        <prop key="org.omg.CORBA.ORBInitialPort">${jndiport}</prop>
      </props>
    </property>
  </bean>

You can then use this bean to lookup stuff on the server. If you want to take it a step further, and call your own remote EJB-classes, you could also do this:

  <bean id="ejbProxy"
        class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean"
        abstract="true">
    <property name="refreshHomeOnConnectFailure" value="true"/>
    <property name="cacheHome" value="true"/>
    <property name="lookupHomeOnStartup" value="true"/>
    <property name="resourceRef" value="false"/>
    <property name="jndiTemplate" ref="mySpringTemplate"/>
  </bean>

And then define beans as:

  <bean id="someRemoteService" parent="ejbProxy">
    <property name="jndiName"
              value="com.company.service.MyRemoteService"/>
    <property name="businessInterface"
              value="com.company.service.MyRemoteService"/>
  </bean>

You can inject this like a regular bean, any calls to it will be made to the server.

In order to access glassfish (and too look up the EIB) running on localhost I had to use:

public class Main {

    public static void main(String[] args) throws NamingException {

        java.util.Hashtable<String, String> hashTable = new Hashtable<String, String>();
        hashTable.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.impl.SerialInitContextFactory");
        hashTable.put(Context.STATE_FACTORIES, "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        hashTable.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
        Context ctx = new InitialContext(hashTable);

    // Looks up the EJB with JNDI
        BookEJBRemote bookEJB = (BookEJBRemote) ctx.lookup("java:global/chapter08-service-1.0/BookEJB!org.agoncal.book.javaee7.chapter08.BookEJBRemote");
    }
}

When glassfish is running on localhost, Context can be initiated with default properties (without hashtable parameter)

Context ctx = new InitialContext();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!