aspose file tools*
The moose likes Tomcat and the fly likes how can i put configurations files outside war file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "how can i put configurations files outside war file " Watch "how can i put configurations files outside war file " New topic
Author

how can i put configurations files outside war file

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all

my application is deployed to multiple machines as war file inside tomcat .

until now im keeping my conf.properties file ( my app config file ) inside the WEB-INF\classes dir

but this is not so good , we have in our over all application conf directory , and i like my application to

see and read the conf file from this conf directory outside the tomcat .

how can i configure tomcat to see directories outside of tomcat and read from there the conf file
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Is the config file common to all apps? If so, you could place it Tomcat's shared/lib folder or common/lib folder to make it available in the classpath of all apps.

This is what I do with my Log4J config file, for example.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
yeah i know about it , i need it to be outside the tomcat installetion directory .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Then you'll need to write some code to access and load the configuration in your web app.

Personally, if the configuration is app-specific, I keep it in the WEB-INF hierarchy. But then, I never deploy my apps as unexpanded war files.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
no problem to write code , i just dont know in whice direction to shot
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If your configurations are not that complex you could make them context init params which can be added to the server.xml file or the newer context fragment files.

With this approach, there is no need to write any code.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
can i add programmatically directories to the classpath ?
also server.xml is not good i like to place the configuration out side the
tomcat directory
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Check the tomcat documentation for classloader-howto.It explains the classloaders hierarchy.You cannot write your own classloader and plug to tomcat.
[ June 12, 2007: Message edited by: Ben Souther ]

Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rahul Bhattacharjee:
Check the tomcat documentation for classloader-howto.It explains the classloaders hierarchy.You cannot write your own classloader and plug to tomcat.



Writing a custom classloader just to read a config file is overkill, don't you think?
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
i do ..
but what options do i have to read conf files outside tomcat?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Ben Souther:


Writing a custom classloader just to read a config file is overkill, don't you think?


Offcourse Ben ;it overkills and not at all necessary.Nor I think that we can plug our own classloader to Tomcat.


[Meir Yan] but what options do i have to read conf files outside tomcat?

You can pass the directory of the configuration files as param to a servlet and then construct paths to configuration file.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
im using resource bundle and as i know it cant read from given path only what defined in the classpath

and im not using servlets , im using pure java as backend , i dont want to be bound on the servlet-api.jar

because this backend also function out side tomcat
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
One bad idea for this.
Put the conf directory as a system property.
No dependency on servlet api.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
how do i do that :
Put the conf directory as a system property


and how can i read from java code this system property?
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
how do i do that :
Put the conf directory as a system property


and how can i read from java code this system property?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Meir Yan:
how do i do that :

and how can i read from java code this system property?


System.getProperty("config.location");
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
where do i set config.location?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
As system variable.From program also you can set or the way you set JAVA_HOME.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
cant i just add it to the CLASSPATH variable and let TOMCAT to include this
path as the path to search ?
i realy dont want to do it inside the code
is there away ?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Meir Yan:
cant i just add it to the CLASSPATH variable and let TOMCAT to include this path as the path to search ?


Please have a look into this document.

CLASSPATH variable is ignored by Tomcat.

What is the problem with the approach suggested to you before.
[ June 25, 2007: Message edited by: Rahul Bhattacharjee ]
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
well i dont want to recompile the application
if i start to using code (System.getProperty("config.location");)
Omar Sharim
Greenhorn

Joined: Apr 28, 2011
Posts: 1
If you are using Spring 3, you can take advantage of the Spring Expression Language. Let's say you have two applications app1.war and app2.war and they require a properties file named config.properties. The applications will be deployed with context paths /app1 and /app2.

Create two directories app1 and app2 in a common directory, eg. C:\myConfig\app1 and C:\myConfig\app2.

Put config.properties inside app1 and another config.properties inside app2.

Then create a file ${CATALINA_HOME}/conf/[enginename]/[hostname]/context.xml.default with the contents:

The parameter myConfigDirectory will be available to all the applications on the host. It is better to create this parameter in context.xml.default rather than in server.xml, because the file can be changed later without restarting tomcat.

In the applicationContext.xml inside war you can access config.properties using the SpEL expression: "#{contextParameters.myConfigDirectory + servletContext.contextPath}/config.properties", so for example you can write:

The expression will get expanded to C:/myConfig/app1 for application with contextPath /app1, and C:/myConfig/app2 for application with contextPath /app2. This will make the applications access the config.properties file based on their contextPath.
Abhijeet Neware
Greenhorn

Joined: Mar 28, 2011
Posts: 16
dear freinds;

How to call the applet from jsp?


Regards
Abhijeet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i put configurations files outside war file