You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Vams <vm...@charter.net> on 2004/08/07 18:33:33 UTC

Finding config file.

Hi all,

I have config xml files for my app.  Does anyone know how I could find and 
load them using a relative path to the class file wanting it instead of the 
working directory of the entire app?

Basically, I want to do the same thing that Configuration() does in hibernate.  
It looks for either hibernate.properties or hibernate.cfg.xml in the base of 
the classpath.  It also loads mapping files relative to the cfg file.  How to 
do that would be nice too.

Currently, I use the relative path to the project in eclipse.  I will have to 
change that to something else when I deploy it to tomcat.  I would like it so 
that the paths work no matter where I move the config and class files 
(keeping their relative relations in tact).

Thank you,
  Vamsi

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Finding config file.

Posted by Eric Schneider <er...@centralparksoftware.com>.
Vams,

I usually put my xml/config files in the WEB-INF folder.   On app 
startup I read them in as input streams in my ApplicationServlet 
subclass.

Hope this helps,
eric

public class MyAppServlet extends ApplicationServlet {

     /**
      * @see javax.servlet.Servlet#init(ServletConfig)
      */
     public void init(ServletConfig config) throws ServletException {
         super.init(config);

         InputStream myXmlFile = getResourceAsStream("myXmlFile.xml");
     }

     protected ServletContext appContext;

     protected InputStream getResourceAsStream(String resource) {
		return getAppContext().getResourceAsStream("WEB-INF/" + resource);
	}

     protected URL getResourceURL(String resource) {
		URL url;
		try {
			url = getAppContext().getResource("/WEB-INF/" + resource);
		} catch (MalformedURLException mex) {
			logObj.warn("Error locating resource.", mex);
			return null;
		}
		return url;
	}
	
	/**
	 * @return
	 */
	public ServletContext getAppContext() {
		return appContext;
	}
}

On Aug 7, 2004, at 12:33 PM, Vams wrote:

> Hi all,
>
> I have config xml files for my app.  Does anyone know how I could find 
> and
> load them using a relative path to the class file wanting it instead 
> of the
> working directory of the entire app?
>
> Basically, I want to do the same thing that Configuration() does in 
> hibernate.
> It looks for either hibernate.properties or hibernate.cfg.xml in the 
> base of
> the classpath.  It also loads mapping files relative to the cfg file.  
> How to
> do that would be nice too.
>
> Currently, I use the relative path to the project in eclipse.  I will 
> have to
> change that to something else when I deploy it to tomcat.  I would 
> like it so
> that the paths work no matter where I move the config and class files
> (keeping their relative relations in tact).
>
> Thank you,
>   Vamsi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Finding config file.

Posted by Todd O'Bryan <to...@mac.com>.
Every class has access to the ClassLoader that was used to load it, so 
you can use that to get to your file.

If your class is MyClass,

MyClass.class.getResourceAsStream("config.xml")

should return an InputStream for a config.xml file located in exactly 
the same folder as MyClass.

HTH,
Todd

On Aug 7, 2004, at 12:33 PM, Vams wrote:

> Hi all,
>
> I have config xml files for my app.  Does anyone know how I could find 
> and
> load them using a relative path to the class file wanting it instead 
> of the
> working directory of the entire app?
>
> Basically, I want to do the same thing that Configuration() does in 
> hibernate.
> It looks for either hibernate.properties or hibernate.cfg.xml in the 
> base of
> the classpath.  It also loads mapping files relative to the cfg file.  
> How to
> do that would be nice too.
>
> Currently, I use the relative path to the project in eclipse.  I will 
> have to
> change that to something else when I deploy it to tomcat.  I would 
> like it so
> that the paths work no matter where I move the config and class files
> (keeping their relative relations in tact).
>
> Thank you,
>   Vamsi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org