My application uses JNDI to access both local EJBs, and to access remote services via RMI. Currently this is handled in the following way:
public MyLocalEJB getMyLocalEJB(){
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"));
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"));
InitialContext ctx = new InitialContext(environment);
return (MyLocalEJB)ctx.lookup("MyEar/MyBean/local");
}
public RemoteServiceInterface getRemoveService(){
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.evermind.server.ApplicationClientInitialContextFactory"));
env.put(Context.PROVIDER_URL, "ormi://server:port/APPNAME");
env.put(Context.SECURITY_PRINCIPAL, "user");
env.put(Context.SECURITY_CREDENTIALS, "secret");
InitialContext ctx = new InitialContext(environment);
return (RemoteServiceInterface)ctx.lookup("ejb/service/RemoteService");
}
This seems to give some issues though since Context.PROVIDER_URL overrides java.naming.provider.url which causes timing problems since change the property back and forth. For example, Hibernate Sessionfactory throws warning that the provider url is wrong, when it gets the RMI provider url instead of the local, and JBoss throws multiple exceptions on shutdown about jndi bindings.
I read that you can specify multiple provider URLs, so I tried the following in both methods:
env.put(Context.PROVIDER_URL, "jnp://localhost:1099,ormi://server:port/APPNAME");
This removes the errors, but it still feels bad since some other parameters change constantly depending on what method was called last (the username/pass for example should not be used when accessing local ejbs.).
I assume there is something wrong at the core of this implementation - how is it supposed to be properly done?