"Cannot locate configuration source" when using commons-configuration XMLConfiguration with Tomcat

19,595

Solution 1

Use ServletContext.getResourceAsStream(..), and pass the stream. Or if the file is on the classpath, you can use getClass().getResourceAsStream(..)

Solution 2

Actually, org.apache.commons.configuration.XMLConfiguration does not have a constructor which accepts an InputStream, so getClass().getResourceAsStream() will not work. There is an XMLConfiguration constructor which takes a URL, however, so use getClass().getResource() instead.

See http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/XMLConfiguration.html

Share:
19,595

Related videos on Youtube

Bruno Shine
Author by

Bruno Shine

My name is Bruno and I'am a .Net senior consultant. When I'm not working, you can catch me playing with my daughter, taking photos and hanging out with my wife and friends. :)

Updated on May 13, 2022

Comments

  • Bruno Shine
    Bruno Shine almost 2 years

    I'm building two apps that uses commons-configuration XMLConfiguration. Since the apps are related, I've build another project, called commons, that has a custom configuration manager that initializes the XMLConfiguration like so:

    config = new XMLConfiguration("conf/config.xml");
    

    What happens is that the "command-line" app works fine, loading the configuration file. But when I try to use my custom configuration manager on a webapp (using Tomcat) I get a

    org.apache.commons.configuration.ConfigurationException: Cannot locate configuration source

    I've placed the conf directory on the WEB-INF folder, the root folder and the META-INF folder. I've also tried with "/conf/config.xml", "./conf/config.xml" and "../conf/config.xml".

    The only time I got this to work - on the web app - was using an absolute path.

    What am I missing?

    Thanks, Bruno

  • Bruno Shine
    Bruno Shine over 13 years
    Hi bozho, Thanks for you response,but how do I get the ServletContext from a webservice? (sorry but i'm a java newbie :P)
  • Bruno Shine
    Bruno Shine over 13 years
    I've tried: @Resource private WebServiceContext context; And on the webservice constructor: ServletContext servletContext = (ServletContext) context.getMessageContext().get( MessageContext.SERVLET_CONTEXT); But I get the context null.