James,

James Dekker wrote:
> How do I use the getResourceAsStream() method to load this particular
> XML file which is not located under WEB-INF/classes? Its located under
> just WEB-INF.

You'll want to use the method ServletContext.getResourceAsStream(). From
the javadoc:

"
This method is different from java.lang.Class.getResourceAsStream, which
uses a class loader. This method allows servlet containers to make a
resource available to a servlet from any location, without using a class
loader.
"

The CWD should be your webapp's directory (in your case,
%TOMCAT_HOME/mywebapp%). You'll want to use the path "/WEB-INF/my.xml".

> Also is there a way to convert this InputStream into a File?

No. If you want more than just the bytes from the file, you'll need to
use ServletContext.getResource(), which returns a URL.

> Here's my original piece of code (which works when
> undeployWars="true", I am trying to get it to work when
> undeployWars="false"):
> 
>    public class XmlConfigInitServlet extends HttpServlet {
>       
>        public void init() throws ServletException {
>            // String prefix = getServletContext().getRealPath("/");
>            String file = getInitParameter("xml-config-file");
>            File xmlConfigFile = new File(prefix + file);
>            if (!xmlConfigFile.exists()) {
>                System.out.println("attributes-config.xml not found, "
>                        + xmlConfigFile.getAbsolutePath());
>            }

I'm sure that you don't have that one line commented-out, otherwise this
code does not compile. Assuming that "prefix" is defined as indicated
above, then this will certainly work when unpackWars (undeploy?) is set
to "true", as you are building a filesystem path and loading a file as
usual.

When your file is still inside a WAR, you're not going to be able to
find it using a File object. You either have to get clever and try to
load the file out of the WAR file by actually opening the WAR file and
searching for the file (bad, and a waste of time to boot), or just by
using the built-in helper method like this:

        public void init() throws ServletException {
            ServletContext application
                  = getServletConfix().getServletContext();
            String filename = getInitParameter("xml-config-file");
            URL xmlConfig = application.getResource(filename);

            if (null == xmlConfig) {
                System.out.println("attributes-config.xml not found, "
                        + xmlConfigFile.getAbsolutePath());
            }
            else
            {
                InputStream in = xmlConfig.openStream();
                // Hope a java.io.File argument is not necessary
                AttributeBeanXmlConfigHelper.parse(in);
                in.close();
            }

> What I thought I could do was something like this inside the try / catch:
> 
> InputStream is =
> getServletContext().getResourceAsStream("WEB-INF/attributes-config.xml");
> 
> // Configure Digester from XML ruleset
> AttributeBeanXmlConfigHelper.parse(xmlConfigFile);   
> 
> Now, the problem is... I think that there will be definitely be a
> pathing issue for WEB-INF/attributes-config.xml when using
> getResourceAsStream() (I am guessing because of my attempts with the
> Log4jInitServlet postings).

Did you actually try it? It's supposed to work this way.

> Also, how would one convert an InputStream to a File?

You can't: a java.io.File is a filesystem notion, and an InputStream is
just a source of bytes. If you had an InputStream that came from an HTTP
connection, what would it's java.io.File object look like?

The most portable way to do it would be to read the bytes from your
source file in the WAR and write them to a temp file in the TEMP
directory or something like that.

Why doesn't your class allow configuration through something other than
a java.io.File object? Can you change that?

-chris


Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to