Himanshu Yadav
Himanshu Yadav

Reputation: 13585

Access property file from outside of war/ear for multiple application servers

Currently my application runs on both Tomcat 6.0 and Websphere Application Server 8.0.
How can configure my property file outside of WAR/EAR file?
Right now property file is placed in webapps/WEB-INF/ directory and being accessed it with the help of commons configuration framework.

Upvotes: 2

Views: 5652

Answers (3)

Beryllium
Beryllium

Reputation: 12998

You could

  1. Use a property defined at the web application level to point to the file (this way the data center can change the location)
  2. Just read it directly (no need for additional libraries)

Example:

final Properties p = new Properties();
p.load(new FileInputStream(new File(System.getProperty("..."))));

You should only read it from the web layer (Servlet, JSP), but not from an EJB.


As for WebSphere, you can add a web application specific property in the console. It's hidden deeply, look for "Application server - xxx - Process definition - JVM - Custom properties".

As for Tomcat, I am not aware of a web application specific way, but you can define system properties in .../bin/setenv.sh (setenv.bat for Windows). I usually prepend a unique prefix (the application's name for example), so that these do not interfere.

Upvotes: 0

saratbeesa
saratbeesa

Reputation: 411

As you're already using commons configuration API, it already provides a hook like they've explained below in their documentation for optional configuration resources.

This way is much robust in a sense that your default out-of-the-box configuration still resides in the web application itself. But you can still override any property you want using external configuration XML file.

In our project, we've used this extensively by providing a facility to inject an external config.xml file into the system using a System level property. We did something like below.

<configuration>
<header>
    <result forceReloadCheck="true">
        <nodeCombiner
            config-class="org.apache.commons.configuration.tree.OverrideCombiner" />
    </result>

</header>
  <system/>
  <configuration fileName="${ext.config}" forceReloadCheck="true" config-optional="true">
        <reloadingStrategy refreshDelay="10000"
        config-class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy" />
        </configuration>
  <configuration fileName="com/company/config/config.xml"></configuration>      
</configuration>

This configuration XML file ensures that your webapp loads successfully with out-of-the-box configuration and at the same time, optionally checks for an external.config system property which can point to a totally alternative configuration overriding any arbitrary property defined in your web application.

Upvotes: 2

mthmulders
mthmulders

Reputation: 9705

You could easily achieve that by defining a system property, let's call it configLocation. Instead of reading a property file from the classpath, your application should read the location specified by the system property. Of course, you need to start both Tomcat and WebSphere with the system property defined: -DconfigLocation=/path/to/config.properties.

Note that you should also handle the situation where that file does not exist, or cannot be read. Possible options include failing to start the application, or falling back to a fail-safe configuration.

Upvotes: 0

Related Questions