cross server environment variable
I would like to set to be able to set开发者_Go百科 a string variable such as "DEVEL" or "PRODUCTION", in Glassfishv3
and Apache tomcat 6
servers, and want it to be accessible from java code so I can change behaviour of my app according to the variable. What is the easiest way of doing that?
I do not think there is a method of setting an environment variable that works for both of these servers. There are methods to set an environment variable for each of these servers though.
To set a system property that can be detected by your web application at run-time:
Tomcat : Set the value of the environment variable CATALINA_OPTS and start the server.
export CATALINA_OPTS=-DmyPropertyName=myPropertyValue
GlassFish 3 : There are a couple methods that you can use.
Direct use of a GlassFish system property. Start the server. Use the asadmin command 'create-system-properties' to define a System property. Restart the server.
asadmin create-system-property myPropertyName=myPropertyValue.
Direct use of a JVM Option: Start the server. Use the asadmin command 'create-jvm-option' to create a new JVM option that defines the System property that will be used when the server is started. Restart the server so that it uses the property.
asadmin create-jvm-options -DmyPropertyName=myPropertyValue
To change the value of a system property that can be detected by your web application at run-time:
Tomcat : Change the value of the CATALINA_OPTS environment variable and restart your server.
export CATALINA_OPTS=-DmyPropertyName=myNEWPropertyValue
GlassFish 3 : The method to change the property value depends on the method you used to set the property value.
Direct use of a GlassFish system property. Recreate the GlassFish system property with the 'create-system-properties' command and restart the server.
asadmin create-system-property myPropertyName=myNEWPropertyValue
Direct use of a JVM option: Delete the old jvm option and create a new one in its place. Restart the server.
asadmin delete-jvm-options -DmyPropertyName=myPropertyValue
asadmin create-jvm-options -DmyPropertyName=myNEWPropertyValue
See http://java.net/jira/browse/GLASSFISH-11253
To unset the value of a system property that can be detected by your web application at run-time:
Tomcat : Reset the value of the environment variable CATALINA_OPTS, without including the JVM Option definition. Restart the server.
export CATALINA_OPTS=
GlassFish 3 :
Using a GlassFish system property. Use the 'delete-system-properties' command and restart the server.
asadmin delete-system-property myPropertyName
Using a JVM Option to define the property. Use the delete-jvm-options command and restart the server.
asadmin delete-jvm-options -DmyPropertyName=myPropertyValue
To access a system property from inside your web application
Use System.getProperty(String) or [System.getProperty(String,String)][2]
After saying all this, I want to discourage you from using this info to pursue your stated development strategy.
There are other methods to differentiate a development and production environment that do not involve code changes.
[2]: http://download.oracle.com/javase/6/docs/api/java/lang/System.html#getProperty(java.lang.String, java.lang.String)
For tomcat you can use CATALINA_OPTS
environment variable in order to set system properties:
CATALINA_OPTS (Optional) Java runtime options used when the "start",
or "run" command is executed.
So you can setup this in IDE or just in command line:
CATALINA_OPTS=-DdevelopmentMode=true
And then in your app:
System.getProperty("developmentMode")
Use an env-entry in the web.xml :
<env-entry>
<description>development or production</description>
<env-entry-name>devMode</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>PRODUCTION</env-entry-value>
</env-entry>
And in the Java code, you can do :
Context ctx = new InitialContext();
String devMode = (String) ctx.lookup("java:/comp/env/devMode");
It depends whether it should be constant or dynamic variable. Whether you want to change it during compilation time or in the runtime on the fly. With 1st approach you may do that in web.xml setting init parameter or env-entries or even use simple property file packed in the jar archive. With the 2nd approach you could use special kind of configuration table in your database which would be pooled by the application piropdically or maybe you could harness JMX management mechanism and modify configuration on the fly using jconsole
精彩评论