Can read Web / Meta-Inf / Context.xml in Tomcat from some properties file

I have context.xml in my web / meta-inf / folder containing information about connecting to a database (pool). Now I want the database data for my application to be provided by the end user in some properties and context.xml files, reading the db connection information from the properties file, rather than hard-collecting them directly in the file.

Is it possible for tomcat to replace placeholders from some properties file?

I read about the context manager, but not sure where to place it.

Please enter your details.

Thanks Abhishek

+6
java
source share
5 answers

You can do it better. In our case, we had different databases for different profiles, such as dev, UAT, pre-prod, support, etc.

So what I did, I put my context.xml in its default location <TOMCAT_HOME>/conf .

The following was specified in the configuration of the context.xml resource:

 <Resource name="jdbc/someDS" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="${appName.db.url}" username="${appName.db.user}" password="${appName.db.password}" maxActive="30" maxIdle="10" defaultAutoCommit="true" maxWait="-1"/> 

I created three tomcat servers - server-dev, server-uat, server support and passed the corresponding values ​​for each database in the vm tomcat arguments in the eclipse / intellij tomcat configuration (as shown below):

 -DappName.db.url=jdbc:oracle:thin:@<DB_SERVER>:1521:<SID> -DappName.db.user=DB_USER -appName.db.password=DB_PASSWORD 

The advantage is that developers then do not need to change the database every time, they just need to start a specific server. It was very convenient and saved a lot of time during our development.

+1
source share

Adding JNDI resources to Context.xml is not enough. You also need to determine their use in WEB-INF / web.xml. Check this step by step.

0
source share

As I understand it, your goal is to have the application configuration outside of your .war so that the system administrator can configure the system.

One way to achieve this is not to put context.xml in your .war file, but to distribute this file with your .war. Then this file should be placed in CATALINA_HOME / conf / Catalina / HOSTNAME / APPLICATIONPATH.xml (e.g. CATALINA_HOME / conf / Catalina / localhost / myapp.xml). Thus, the database connection information can be edited directly in the external context configuration file without changing the .war file, you do not need placeholders in this script. This is not the most convenient way for the user, since he / she should edit the xml file, but it should be possible for most system administrators ...

You can find more information on tomcat context settings at tomcat.apache.org/tomcat-5.5-doc/config/context.html

0
source share

I'm not sure that you can load data from the properties file, but you can get the information in the central server.xml file, and not in the .xml context. Once you achieve this, you could perhaps externalize the connection details using a standard XML entity reference.

Instead of placing the database connection data in the context.xml file, put it in server.xml in the section, and then add the ResourceLink element in context.xml, which creates the connection between the global data source and your context ..

Unfortunately, the documentation is pretty weak for what you are trying to achieve. Follow the instructions in the Resource Definitions section of this page (about halfway down) and pay particular attention to the (tiny) ResourceLink section below:

http://tomcat.apache.org/tomcat-5.5-doc/config/globalresources.html

0
source share

If I do not have information about connecting to the database during the build, I just set up a link to the resource and tell the administrator what name they need to give the connection pool. They can configure everything in the Tomcat admin console, and I should never know about it. It also makes it easier to collect a single WAR file and use it in multiple environments, as database configuration data is not part of the WAR.

See also: Apache Tomcat 6.0 JNDI Resources

If you know the details during build and want to bake them in WAR, try Ant filters .

0
source share

All Articles