Read common external property file in Java Webapp and java normal app
Asked Answered
W

1

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?

Waggle answered 6/4, 2016 at 9:28 Comment(13)
better to use property-file's value persist into Database if you want to change at runtime without re-deployment then.Gratis
@vishalgajera as I mentioned the property file contains the db connection vlaues, so unless I read property values webapp cannot access db.Waggle
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.Gratis
@vishalgajera ok, just a small query can't we chnage db string while webapp is running?Waggle
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.Gratis
@vishalgajera understood, but is that correct way if we read updated property values by a method and then we do not need redeployWaggle
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.Metzler
@SchoolBoy sorry. can't you get updated value(properties file's value) without re-deoploy. let me know if it's so then.Gratis
@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.Metzler
@Metzler 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.Gratis
It's an outside resource, he will be able to read it without any problems.Metzler
@Metzler parden me, it's out of my focus. appreciate!!Gratis
It's ok, I'm glad you realized. :)Metzler
M
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.

Metzler answered 6/4, 2016 at 9:50 Comment(7)
can you share small example for Singleton?Waggle
Like this one: tutorialspoint.com/design_pattern/singleton_pattern.htmMetzler
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.Metzler
the <Context > method could you elaborate more and also I think in this case the property file's path need to be hard coded, correct?Waggle
@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.)Metzler
Thanks, I did not get exactly what you said. can you give example. also JFYI, I am using windows machine.Waggle
@SchoolBoy I've added the Windows example to the answer. You should be able to access the file then as I described.Metzler

© 2022 - 2024 — McMap. All rights reserved.