Martin K. Martin K. - 1 year ago 110
Java Question

How can I store Java EE configuration parameters outside of an EAR or WAR?

I want to store configuration for a web project outside of the web project (ear/war file).
The application shouldn't know in which container it's running (WebSphere/JBoss etc.).

What is the best way to handle this?

Is JNDI a clean way? If JNDI can solve my problems, how should I configure it? (Custom Objects?)

In my case are there only simple Key=>Value pairs (String,String) for SOAP/WS endpoints.

Answer Source

See this question for reading properties file outside of the WAR file.

See this question for reading variable values from JNDI. I believe that this is the best solution. You can read a String variable with this code:

Context initialContext = new InitialContext();
String myvar = (String) initialContext.lookup("java:comp/env/myvar");

The above code will work on all containers. In Tomcat you declare the following in conf/server.xml:

<GlobalNamingResources ...>
  <Environment name="myvar" value="..."
         type="java.lang.String" override="false"/>

The above will create a global resource. It is also possible to define a resource in the context of application. In most containers the JNDI resources are available through a MBeans Management Console. Some of them offer a graphical interface to edit them. At most an application restart is needed, when a change is made.

How JNDI resources are defined and edited is container specific. It is the job of the configurator/administrator to apply the appropriate settings.

These are the benefits offered by JNDI:

  • You can define default values of the parameters in the WAR/EAR file.
  • Parameters are easily configurable at the container.
  • You don't need to restart the container when you modify the value of a parameter.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download