It really depends on what you use these properties for.
Some (for example, a data source, for example) can be configured in the container itself ( Tomcat 5.5. JNDI resources , see the JDBC sources section as a).
The other (application-specific) may indeed be properties. In this case, your options are:
- Package properties in the WAR file and loading the corresponding subset based on some external switch (either an environment variable or a JVM property).
- Configure the deployment process on each of the servers on which the war is unpacked, and the properties file (located in a predefined location on this server and specific to this server) is copied to
WEB-INF/classes (or another suitable place).
As far as "this is the desired goal" goes - yes, I think so. Having one WAR for testing in a QA / statement and then deploying to production reduces the intermediate step and thus leaves less chance of errors.
Update (based on comment):
Point # 1 above refers to the actual environment variable (for example, something that you set via SET ENV_NAME=QA on Windows or ENV_NAME=QA; export ENV_NAME on Linux). You can read its value from your code using System.getenv() and load the appropriate properties file:
String targetEnvironment = System.getenv("TARGET_ENV"); String resourceFileName = "/WEB-INF/configuration-" + targetEnvironment + ".properties"; InputStream is = getServletContext().getResourceAsStream(resourceFileName); Properties configuration = new Properties(); configuration.load(is);
But yes, you can instead define a scalar value via JNDI ( see Environment Records in the Tomcat Document ):
<Context ...> <Environment name="TARGET_ENV" value="DEV" type="java.lang.String" override="false"/> </Context>
and read it in your application through
Context context = (Context) InitialContext().lookup("java:comp/env"); String targetEnvironment = (String) context.lookup("TARGET_ENV");
The fact is that if you still use JNDI, you can also abandon your properties files and configure everything through JNDI. Your data sources will be available to you, since the actual resources and basic properties will remain scalars (although they will be type safe).
Ultimately, you decide which path is best for your specific needs; both have pros and cons.
ChssPly76 Oct 28 '09 at 0:34 2009-10-28 00:34
source share