Tomcat 8 - context.xml uses an environment variable in a data source

I have a Tomcat 8 project that uses a data source (see below)

<Resource auth="Container" 
          name="jdbc/JtmDS"  
          driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
          type="javax.sql.DataSource" 
          username="xfer"
          password="xfer10" 
          url="jdbc:derby:/home/PUID/tm/control/JtmDB"                    
          initialSize="25"
          maxTotal="100" 
          maxIdle="30" 
          maxWaitMillis="10000"                                      
          removeAbandonedOnBorrow="true"
          removeAbandonedTimeout="20" />

This works great.

However, the url is hardcoded /home/PUID/tm/control/JtmDB

When this takes effect, the PUID part of the path will vary on many systems. I have an environment variable. export PUID=abcd The rest of the application can use things like System.getenv( )or ${env:PUID}how and when it is needed.

They all work fine.

My question is very simple: How can I make the PUID value in my context an .xml variable that can be read from an environment variable?

+4
source share
1

- , ... , .

java Tomcat , .

setenv.sh

export PUID=abcd

JAVA_OPTS=-Dpuid=${PUID} 

context.xml,

<Resource auth="Container" 
          name="jdbc/JtmDS"  
          driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
          type="javax.sql.DataSource" 
          username="xfer"
          password="xfer10" 
          url="jdbc:derby:/home/${puid}/tm/control/JtmDB"                    
          initialSize="25"
          maxTotal="100" 
          maxIdle="30" 
          maxWaitMillis="10000"                                      
          removeAbandonedOnBorrow="true"
          removeAbandonedTimeout="20" />

, Tomcat PUID.

+1

Source: https://habr.com/ru/post/1680122/


All Articles