Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 7/8: Reuse of Environment variables in server.xml

for some projects with tomcat we externalize some configs (URLs, etc) in environment variables.

Example: /usr/share/tomcat7/conf/other-urls.xml

 <!-- LDAP -->
<Environment name="remote/com/mycompany/ldap" type="java.lang.String" value="ldaps://myldap.mycompany.net"/>

server.xml

 <?xml version='1.0' encoding='utf-8'?>
 <!DOCTYPE server-xml [
   <!ENTITY Other-URLs SYSTEM "/usr/share/tomcat7/conf/other-urls.xml">
 ]>
 <Server port="8005" shutdown="SHUTDOWN">
 ...
   <GlobalNamingResources>
      &Other-URLs;
   </GlobalNamingResources>
   ....
   <Service name="Catalina">
   ....
     <Engine name="Catalina" defaultHost="localhost"   jvmRoute="4996b9646dc">

  <Realm className="org.apache.catalina.realm.JNDIRealm"
       connectionURL="${remote/com/mycompany/ldap}"
       userPattern="(|(uid={0},ou=People,dc=mycompany,dc=net)(uid={0},ou=Other,dc=mycompany,dc=net))"     
    ....
    />   

...

This configuration does not work (javax.naming.NamingException: Cannot parse url: ${remote/com/mycompany/ldap}). When I write the correct LDAP-URL in connectionURL then it works. I try different ways: With ${..}, without, with java:/env/comp, without.

Is there a way to reuse the environment variable in the server.xml?

Thanks for any hint.

Ciao Peter Schütt

like image 656
user1195024 Avatar asked Apr 10 '15 10:04

user1195024


People also ask

Where is setenv sh?

The setenv script files are located in subdirectories of the component_dir /config directory (setenv.sh on Linux and UNIX or setenv.

Where is Setenv file 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. bat (on Windows) or setenv.sh (on *nix). The file has to be readable.

How do I set an environment variable in Tomcat?

Environment variables can be set, by creating a setenv. bat (windows) or setenv.sh (unix) file in the bin folder of your tomcat installation directory. However, environment variables will not be accessabile from within your code. System properties are set by -D arguments of the java process.


1 Answers

Instead of solving this via XML, you could try doing it via system properties (-Dname=value). There's an article which explains this in a more detailed fashion.

like image 121
mindas Avatar answered Sep 30 '22 05:09

mindas