GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes using xml config file, don't want to hard code path in config file reader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "using xml config file, don Watch "using xml config file, don New topic
Author

using xml config file, don't want to hard code path in config file reader

Sakti Singh
Greenhorn

Joined: Dec 13, 2000
Posts: 16
Hello Folks,

I am using an xml file as my initial config file. I don't want to hard code the aboslute path of the xml file in xml file reader class. Is there any way I can tell the xml path to my config xml file reader?

Eg: String xmlFilePath = "C:\test\test.xml"
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(xmlFilePath), this);

I don't want to hardcode xmlFilePath


My problem will be solved if I can

1. use relative path
2. some kind of class path

else I will have to write another ini file (which will have the xml file path) and place that at root directory

Note that my application supports : AIX, Linux and windows


Appreciate you help..

Namaste,

Sakti
[ June 26, 2004: Message edited by: Sakti Singh ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Is the user supposed to edit this configuration file at some point or is it a "static" configuration that the user doesn't even know about?

Is this a web application (a .war file) or a standalone application?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Sakti Singh
Greenhorn

Joined: Dec 13, 2000
Posts: 16
Hi Lasse, thanks for looking at this:

Lasse>Is this a web application (a .war file) or a standalone application?

Sakti> This is a web app, .war file

Lasse> Is the user supposed to edit this configuration file at some point or is it a "static" configuration that the user doesn't even know about?

Sakti> The xml file should be modified only at the time of deployment.

This xml file has ini variables like:
dbuser, dbpassword, schema, mailserver, adminid, approverid et al

Which may be modified after deployment as well. So I would say its not a "static" configuration.
[ June 27, 2004: Message edited by: Sakti Singh ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. Because you need to update the file at runtime, you can't really use the classpath for looking it up. Therefore, I'd suggest the following:
  • Add a "context-param" into your web.xml, which points to the absolute location of your custom configuration file.
  • Write a ServletContextListener which, upon startup, reads the context-param (the file path), and creates an object that 1) serves as the "configuration repository" from which other classes can ask the configuration properties, and 2) refreshes its data from the filesystem every now and then to keep in sync with the actual configuration file.

  • Sakti Singh
    Greenhorn

    Joined: Dec 13, 2000
    Posts: 16
    You are the Man, thanks Lasse ...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: using xml config file, don't want to hard code path in config file reader