Web Applications Scrapbook
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Overview
Deploy
Tutorials
FAQ
Scrapbook
FAQ
Web Applications
IOC/AOP

A repository of notes and comments that will eventually make their way into the documentation. Please treat the information here with caution, it has often not been verified.

How can I load configuration files for a class that might be used outside of a web-app?

I have a class needs to load a configuration file. This class is independent of the ServletApplication, it could be deployed in a web application or someplace else. What is the best way to allow my class to access the configuration file?

You can use ClassLoader to load your configration file from anywhere in the classpath.

A file from the classpath is always available to every class using the ClassLoader.getResourceAsStream method. So you don't need to pass around any servlet context information.

The classpath also gives you a good deal of options of how to package your files. You can put them in jars in WEB-INF/lib or in the WEB-INF/classes directory, or even in a jar file in $RESIN_HOME/lib.

The basic call would be:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream("config/myconfig.xml");
        

The classpath is used to find the file, and the first one found is used. An example of the search order for the above example is:

  1. $RESIN_HOME/lib/anyjar.jar!/config/myconfig.xml
  2. WEB-INF/classes/config/myconfig.xml
  3. WEB-INF/lib/anyjar.jar!/config/myconfig.xml

FAQ
Web Applications
IOC/AOP
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.