Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different environment variables per war in tomcat

Tags:

java

tomcat

Is there a way to have different environment variables for different war files in tomcat? I am using a 3rd party war and need to have multiple deployments of the same war but with different environment variables (so it loads different configs).

like image 615
ekaqu Avatar asked Jun 05 '13 18:06

ekaqu


People also ask

Where are the environment variables stored in Tomcat?

Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can be specified in the "setenv" script. The script is placed either into CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named setenv.

What are the different environment variables?

There are two types of environment variables: user environment variables (set for each user) and system environment variables (set for everyone). By default, a child process inherits the environment variables of its parent process.

Are environment variables session specific?

Environment Variables are available to all processes and objects but a session variable is only available to that running process instance and values get updated on the go.


2 Answers

Its easy if you run two instances of Tomcat independently. I'm assuming here you are talking about the OS environment variables.

You can also set properties in Tomcat for each war/web app. That would let you run two wars in one Tomcat instance. But that's not what you asked.

like image 145
Lee Meador Avatar answered Sep 22 '22 18:09

Lee Meador


Ok, total crazy hack idea:

Implement a PropertyPlayHolderConfigurer (or use web.xml from Tomcat) for each app instance and load properties same name as you have for System.properties().

Then, create a delegate Properties class that contains both sets of properties. Then

Properties props = new DelegatingProperties(app1Props,app2Props)
System.setProperties(delegate);

public class DelegatingProperties extends Properties {

   private Properties app1Props;
   private Properties app2Props;

   public DelegatingProperties(Properties app1Props, Properties app2Props) {
        this.app1Props = app1Props;
        this.app2Props = app2Props;   
   }

   public String getProperty(String prop) {
       // begin crazy science
       String caller = new Throwable().getStackTrace()[0].getClassName();
       // this is where you get creative.
       // Do the System.setProperties() above so you can intercept calls to  
       //getProperty()
       // and find out the FQCN of the library class(es) that need these variable 
       // (use your debugger).
       // then implement the logic here to decide which of the 2 property sets you have
       // you will query to get the correct results     
   }
}

These are SYSTEM properties we are talking about and they are meant to apply system wide. Your library was probably developed when it was 1-app-1-jvm (or the developer is a tard which is also likely).

Can I atleast get props for creativity? :)

like image 33
Christian Bongiorno Avatar answered Sep 22 '22 18:09

Christian Bongiorno