JNDI problems mail services tests
kabirkhan Apr 16, 2004 5:17 AMHi,
I am working on unit tests for the mail services. They run JBoss 3.2.4RC1 "embedded", by calling the following from their setup() method:
//Start JBoss ServerImpl impl = new ServerImpl(); System.out.println("got the server impl"); Properties prop = new Properties(); prop.setProperty(ServerConfig.HOME_DIR,(new File(".")).getAbsolutePath()); prop.setProperty(ServerConfig.ROOT_DEPLOYMENT_FILENAME,"src/META-INF/depends-service.xml"); prop.setProperty(ServerConfig.SERVER_CONFIG_URL,"file:"+(new File(".")).getAbsolutePath()); System.out.println("done setting up properties"); impl.init(prop); System.out.println("impl.init is finished"); impl.start(); System.out.println("impl.start done"); alreadyrun = true; System.out.println("done configureMBeanServerFactory"); //Start MBeans MBeanServer mbserver = JBMailJMXUtil.locateJBoss(); ObjectName oname = new ObjectName("jboss.mail:type=MailServices,name=UserRepository"); StaticUserRepository sur = new StaticUserRepository(); sur.addUser("acoliver","test"); sur.addUser("test","testpw"); sur.addUser("jboss","jbosspw"); mbserver.registerMBean(sur, oname); //Start other MBeans here
Until now we have been running JNDI with a mock context for JNDI, but since JBoss is actually being started, I am trying to use the existing NamingService, but have run into problems.
I have set up the jndi.properties file to be:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
I stepped through the org.jboss.naming.NamingService.startService() method. The first bit loads up the properties from the jndi.properties file and sets them in System.properties, as expected there is no java.naming.provider.url there. Also, when I do a System.getProperty("java.naming.provider.url") at any stage nothing is returned.
However, once the first InitialContext is created and it's environment got, java.naming.provider.url is there! (i.e. it is in env below, but System.getProperty("java.naming.provider.url") still returns nothing)
Code:
InitialContext iniCtx = new InitialContext(); Hashtable env = iniCtx.getEnvironment();
I followed this through, and new InitialContext()
calls javax.naming.InitialContext.init() //This initialises the environment for the InitialContext
which calls javax.naming.internal.ResourceManager.getInitialEnvironment() //"produces" the environment for the InitialContext
which calls javax.naming.internal.VersionHelper.getJndiProperties()
VersionHelper.getProperties() looks like this
String[] getJndiProperties() { if (getSystemPropsFailed) { return null; // after one failure, don't bother trying again } Properties sysProps = (Properties) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { try { return System.getProperties(); } catch (SecurityException e) { getSystemPropsFailed = true; return null; } } } ); if (sysProps == null) { return null; } String[] jProps = new String[PROPS.length]; for (int i = 0; i < PROPS.length; i++) { jProps = sysProps.getProperty(PROPS ); } return jProps; } String getJndiProperty(final int i) { return (String) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { try { return System.getProperty(PROPS ); } catch (SecurityException e) { return null; } } } ); }
In this funky looking code (in which to be honest I don't understand the special AccessController/PrivilegedAction bits) java.naming.provider.url and jnp://localhost:1099 are returned. I'm wondering
a) Why doesn't this appear in JBoss itself when starting that without the java.naming.provider.url in the jndi.properties
b) What the AccessController stuff does special to magically create the java.naming.provider.url=jnp://localhost:1099
c) If I change the test target in the ant build file to run only one of the tests, and make that test call ResourceManager.getInitialEnvironment(null).get(Context.PROVIDER_URL) BEFORE I have done anything to start the embbeddd JBoss it still returns a URL.
I'd like to not have java.naming.provider.url=jnp://localhost:1099, in order to run the naming service "in-jvm" since I am experiencing other problems if I set the Port and RmiPort > -1. If needed I can describe in more detail what they are.
Any ideas?
Cheers,
Kab