0

I know this question has answered many a time with most useful answer as below,

Where to place and how to read configuration resource files in servlet based application?

However, We have some special requirement as below,

  1. Webapp will be deployed to tomcat.
  2. Normal java app in form of .jar will be placed under folder /myapp
  3. myappConfig.property file will be placed under /myapp

Directory Structure on client machine

/myapp
   /myapp.jar
   /assests/myappConfig.property
   /tomcat/webapps/myapp.war

Basically myapp.jar and myapp.war will access sql db connection values for MySql database connection and db operations.

Accessing myappConfig.property from myapp.jar --> Working fine

        File jarPath = new File(Myapp.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        String propertiesPath = jarPath.getParent();
        System.out.println(" propertiesPath-" + propertiesPath);
        mainProperties.load(new FileInputStream(propertiesPath + "\\assets\\myapp.property"));

Can anyone help/suggest how to implement,

Accessing myappConfig.property file from mywebapp,

provided run time change in myappConfig.property file does not required myapp.war to be redeployed

Thanks for your help in advance.

edit

Below is the steps in which we want to deliver the project to client.

  1. Below is my app directory

    /myapp
      /myapp.jar
      /assests/myappConfig.property
      /tomcat/webapps/myapp.war
    
  2. pack everything in one package with some packaging tool.

  3. Run this package in client machine at any location and it will have same directory structure as above

  4. Here, I do not want to hard code any location in webapp or tomcat for "/assests/myappConfig.property"

  5. Normal Java app I can read property file but for wepapp I am not getting clear idea for how to do that?

Community
  • 1
  • 1
School Boy
  • 1,211
  • 2
  • 18
  • 31
  • better to use property-file's value persist into Database if you want to change at runtime without re-deployment then. – Vishal Gajera Apr 06 '16 at 09:32
  • @vishalgajera as I mentioned the property file contains the db connection vlaues, so unless I read property values webapp cannot access db. – School Boy Apr 06 '16 at 09:36
  • your req. looks like unclear, one way you mentioned that you want to change prop. file at run-time without re-deployment and one way you said database-credentials are then. how it's possible. – Vishal Gajera Apr 06 '16 at 09:38
  • @vishalgajera ok, just a small query can't we chnage db string while webapp is running? – School Boy Apr 06 '16 at 09:50
  • you can change it, but you had change property file!! means now get update of that prop. file you has to re-deploy it for get changes into web-app. that's why i am recommending if you don't likewise then store it into databse. – Vishal Gajera Apr 06 '16 at 09:52
  • @vishalgajera understood, but is that correct way if we read updated property values by a method and then we do not need redeploy – School Boy Apr 06 '16 at 09:55
  • Why would he need to redeploy? It's just a resource like a picture or anything else. If he's using JDBC, he just connects elsewhere. – Peter Apr 06 '16 at 09:57
  • @SchoolBoy sorry. can't you get updated value(properties file's value) without re-deoploy. let me know if it's so then. – Vishal Gajera Apr 06 '16 at 09:59
  • @vishalgajera So you are telling me that if I change my profile picture on your website you have to re-deploy your webapp to display it? If you say no, why is a picture different from a text file? You can read it anytime. He's not talking about JPA config. – Peter Apr 06 '16 at 10:03
  • @Peter it's different scenario what i discuss. but if counting that profile picture as a part of web-app then sure you need to re-deploy too. – Vishal Gajera Apr 06 '16 at 10:06
  • It's an outside resource, he will be able to read it without any problems. – Peter Apr 06 '16 at 10:09
  • @Peter parden me, it's out of my focus. appreciate!! – Vishal Gajera Apr 06 '16 at 10:11
  • It's ok, I'm glad you realized. :) – Peter Apr 06 '16 at 10:17

1 Answers1

1

You can add a <context> to tomcat/conf/server.xml (in this example, linux path):

<Context docBase="/home/yourusername/tomcat/assests" path="/assets" />

If you are using Windows:

<Context docBase="C:\path\to\myapp\assets" path="/assets" />

And then you can access it like any other resource within your webapp (e.g.: /assets/myappConfig.property).

If you are using JDBC for example, you could store the connection properties in a Singleton and request it from there, and that class could take care of change checks on that file.

Peter
  • 137
  • 8
  • 36
  • can you share small example for Singleton? – School Boy Apr 06 '16 at 09:56
  • Like this one: http://www.tutorialspoint.com/design_pattern/singleton_pattern.htm – Peter Apr 06 '16 at 09:57
  • I only recommended using it not to confuse old and new configs. Just read the config file every X minute with this class and if it's changed set the correct connection details. – Peter Apr 06 '16 at 09:58
  • the method could you elaborate more and also I think in this case the property file's path need to be hard coded, correct? – School Boy Apr 06 '16 at 15:36
  • @SchoolBoy yes, you technically give an alias to the "hard coded" property file path. (Actually, setting a path in a property isn't hard coding.) – Peter Apr 07 '16 at 04:54
  • Thanks, I did not get exactly what you said. can you give example. also JFYI, I am using windows machine. – School Boy Apr 07 '16 at 06:10
  • @SchoolBoy I've added the Windows example to the answer. You should be able to access the file then as I described. – Peter Apr 07 '16 at 10:27