
<document>
  <header>
    <product>resin</product>
    <title>Web Applications Scrapbook</title>
    <description>
      <p>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.</p>
    </description>
  </header>

  <body>
    <summary objsummary='localtoc'/>
      <faq title="How can I load configuration files for a class that might be used outside of a web-app?">
        <description>
          <p>
          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?
          </p>
        </description>

        <p>You can use <var/ClassLoader/> to load your configration file from
            anywhere in the classpath.  </p>

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

        <p>The basic call would be:</p>

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

        <p>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:</p>

        <ol>
          <li>$RESIN_HOME/lib/anyjar.jar!/config/myconfig.xml
          <li>WEB-INF/classes/config/myconfig.xml
          <li>WEB-INF/lib/anyjar.jar!/config/myconfig.xml
        </ol>
          
      </faq>

  </body>
</document>
