Dear Christopher,

Le 24/10/2019 à 00:36, Christopher Schultz a écrit :
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Mathieu,

On 10/23/19 17:23, Mathieu Dubois wrote:
I noticed that the application also need to access to a directory
to store the result of some computation usually outside the
location of tomcat (the results can be rather large). As for the DB
this depends on each instance of the application. Is there a
similar mechanism for such a case ?
It's not exactly clear what you are asking, but it sounds like you are
looking for a configuration similar to the JNDI binding that can be
split between conf/server.xml and META-INF/context.xml for connecting
to a database.

You have some choices, here, and the "right one" probably will require
you to make a decision based upon your requirements.

In WEB-INF/web.xml, there are some optional configuration data called
"context parameters". They look something like this:

<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee";
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";
          version="3.0"
          metadata-complete="true">
   <context-param>
     <description>
       You can put whatever you want in here. It's just documentation
       for human readers.
     </description>
     <param-name>my-configuration-property-name</param-name>
     <param-value>my value</param-value>
   </context-param>
   ...
</web-app>

If you want to put, for example, a directory path in here for storing
temporary files, you could do it like this:

<web-app>
   <context-param>
     <description>Path to the temporary file directory where we write
filed.</description>
     <param-name>fr.cns.genoscope.appname.tmpfiledir</param-name>
     <param-value>/tmp/app/temp-files</param-value>
   </context-param>
   ...
</web-app>

In order to use these configuration values, your code needs to read
them explicitly, so you'll need to make some code changes in order to
put your configuration into WEB-INF/web.xml. Something like this in
your servlet:

     String tmpDir =
getServletContext().getInitParam("fr.cns.genoscope.appname.tmpfiledir");
     // ... use the tmpDir for all your file-writing needs

Now, WEB-INF/web.xml is bundled inside your WAR file and, as you've
mentioned, it's not very flexible with your builds. So, here's what
you can do:

The file META-INF/context.xml (also bundled within your application's
WAR file -- hold that thought for a minute) can be used to override
the values of your context-param values, like this:

<?xml version="1.0"?>
<Context>
   <Parameter name="fr.cns.genoscope.appname.tmpfiledir"
value="/usr/local/other/location/for/client/X"
          override="true"/>
  ...
</Context>

More info can be found at:
http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Context_Para
meters

So, you could put the right value into WEB-INF/web.xml (which is
inconvenient) or into META-INF/context.xml (which is also
inconvenient) or -- and here's where things get a little interesting
- -- you can copy the file META-INF/context.xml from the WAR file and
put it into Tomcat's configuration directory structure like this:

conf/[enginename]/[hostname]/[appname].xml

...and it will *override* the file supplied by the WAR file in
META-INF/context.xml. So you get to use the same WAR file everywhere
and customize those XML files on a per-client basis.

Above, the [enginename] is almost always "Catalina" and matches the
"name" attribute of the <Engine> in your conf/server.xml file. By
default, it's name="Catalina" and pretty much nobody ever changes it.
Your [hostname] comes from the "name" attribute of the <Host> in which
your context/webapp is defined, and is often just "localhost" although
it would be anything depending upon your environment. The [appname] is
whatever you want it to be: the [appname] sets the context-path of the
application. But the [appname].xml must match your [appname].war file
name.

So if you don't mind modifying your code a little, this can get your a
lot of flexibility.

This feature goes back to Tomcat 5.5, so you should be able to use it.
I'd of course encourage you to look at upgrading to at least Tomcat
8.5 in the near-term. You may find that you can just drop-in the
latest Tomcat 8.5.x in place of your Tomcat 5.5 and everything still
works. (You will have to re-write your conf/server.xml file from
scratch, as those files are not compatible between major releases.)

Thanks for your in-depth explanation, it really helps. If I summarize, I can use conf/[enginename]/[hostname]/[appname].xml to configure bith the DB connection (with a Resource) and the directory where to write files (with a param). That sounds exactly like what we need.

Thanks again,
Mathieu

--
Mathieu Dubois - IR - UMR 8030 équipe LABGeM
CEA - Genoscope. 2 rue Gaston Crémieux. 91057 Evry Cedex France.
Bureau B07
+33 1 60 87 53 35


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to