views:

44

answers:

1

I have a servlet running in JBoss (4.2.2.GA and 4.3-eap) that needs to connect to an EJB to do work.

In general this code works fine to get the Context to connect and make RMI calls (all in the same server).

public class ContextFactory
{
  public static final int DEFAULT_JNDI_PORT = 1099;
  public static final String DEFAULT_CONTEXT_FACTORY_CLASS = "org.jnp.interfaces.NamingContextFactory";
  public static final String DEFAULT_URL_PREFIXES = "org.jboss.naming:org.jnp.interfaces";

  public Context createContext(String serverAddress)
  {
      //combine provider name and port 
      String providerUrl = serverAddress + ":" + DEFAULT_JNDI_PORT;

      //Set properties needed for Context: factory, provider, and package prefixes.
      Hashtable<String, String> env = new Hashtable<String, String>(3);
      env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_CONTEXT_FACTORY_CLASS);
      env.put(Context.PROVIDER_URL, providerUrl);
      env.put(Context.URL_PKG_PREFIXES, DEFAULT_URL_PREFIXES);


      return new InitialContext(env);
  }

Now, when I change the JNDI bind port from 1099 in server/conf/jboss-service.xml I can't figure out how to programatically find the correct port for the providerUrl above.

I've dumped System.getProperties() and System.getEnv() and it doesn't appear there.

I'm pretty sure I can set it in server/conf/jndi.properties as well, but I was hoping to avoid another magic config file.

I've tried the HttpNamingContextFactory but that fails "java.net.ProtocolException: Server redirected too many times (20)"

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
env.put(Context.PROVIDER_URL, "http://" + serverAddress + ":8080/invoker/JNDIFactory");

Any ideas?

A: 

The information about the port is stored on JBoss as an MBean property. The problem is that in order to read this property you need an access to MBeans, which requires the port number...

I think that the only way to get this port number is to read the configuration file itself and extract the port number. It is not very elegant, so you may prefere to create in web.xml file for your servlet and store the port number there.

You may also use JBoss HTTP invoker, which tunnels requests to 1099 port through port 8080 (default HTTP port), please note however that you need to secure this connector. In this case the port will be always the same as your HTTP port.

Piotr Kochański
Piotr, thanks for the response. I had not thought of going to the configuration file directly.Any idea what I'm doing wrong with the HTTP invoker at the bottom of my question? Is there a file where I hook it up to JAAS, or is that something I need to do in code? Thanks again.
Steve Jackson