Iqbal
Iqbal

Reputation: 590

Creating A File Resource Using Tomcat JNDI

I need to read an application.properties file from a servlet application using Tomcat container. The file can not be included in my war so it can't be under webapps or Tomcat root folder in any ways. The file has to be somewhere in the folder. I also can not use FileInputStream to read the properties file. Only option I have is to define a JNDI name for a folder / directory and look that JNDI properties during runtime to find the folder location to read the file. Is thee any working example out there?

Upvotes: 3

Views: 7259

Answers (2)

Allan D
Allan D

Reputation: 51

I have a slightly different approach to a similar problem. I also toyed with the idea of JNDI but I found this a bit of overkill.

I found another article by BalusC. It allows you to specify folder relative to your tomcat installation and deploy all your properties files there. So you keep configuration external to your application but you don't require seperate configuration for every properties file for every app.

Here it the link: Where to place and how to read configuration resource files in servlet based application?

and the preferred choice is as follows:

Put it in the classpath, so that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));

Here filename.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. webapp/WEB-INF/lib, webapp/WEB-INF/classes, appserver/lib or JRE/lib.

If the properties file is webapp-specific, best is to place it in WEB-INF/classes. If you're developing a project in an IDE, you can also drop it in src folder (the project's source folder).

You can alternatively also put it somewhere outside the default classpath and add its path to the classpath of the appserver. In for example Tomcat you can configure it as shared.loader property of tomcat/conf/catalina.properties.

I hope this helps.

Allan

Upvotes: 1

Iqbal
Iqbal

Reputation: 590

I have chalked out a solution for myself reading the following similar posts and articles.

The Context Container

Reading a global variable from tomcat with JNDI. Example not working

java:comp/env is not bound

I have defined a Environment inside my Context for my web application under \conf\Catalina\localhost\mywebapp.xml as follows....

    <Environment name="propertiesfilelocation" value="E:\\tmp\\application.properties"
     type="java.lang.String" override="false"/>

Then accessed my properties file using a JNDI lookup to get the file name.

   Context ctx  = new InitialContext();
   Context envCtx = (Context)ctx.lookup("java:comp/env");
   String propertiesFileLocation = (String) envCtx.lookup("propertiesfilelocation");
   LOGGER.info("String property === " + propertiesFileLocation);

   properties.load(new FileInputStream(propertiesFileLocation));

@home : Yes you are right it involved FileInputStream. However, I am happy with the solution because I am no longer hard coding my folder location inside my Java code which makes my app more portable.

Upvotes: 3

Related Questions