On Sun, 25 Mar 2001, [iso-8859-1] Endre St�lsvik wrote:

> I have a problem reading a file that I've put in the WEB-INF/classes dir.
> It works on tc3.2 and tc3.3, but doesn't work in tc4.0. Could someone shed
> some light on why, and how I could manage to read it on all servlet
> servers, and whether I should put it somewhere else.
> 
> The file is called "logconfig.xml", and the Settings.getLOG_CONFIG_FILE()
> is just a String with that content.
> 
> This code:
> 
>       private static boolean logConfig() throws ConfigException {
>               URL dtdURL = DOMConfigurator.class.getResource("log4j.dtd");
>               if(dtdURL == null)
>                       throw new IllegalArgumentException("CT: Couldn't find 
>log4j.dtd. Bad stuff.");
> 
>               FileInputStream configFileIS;
>               try {
>                       URL configURL = 
>DOMConfigurator.class.getClassLoader().getResource(Settings.getLOG_CONFIG_FILE());
>                       System.out.println("This is the config URL: "+configURL);
>                       System.out.println("This is the config URL.getFile(): 
>"+configURL.getFile());
>                       File configFile = new File(configURL.getFile());
>                       System.out.println("This is the config file: "+configFile);
>                       configFileIS = new FileInputStream(configFile);
>               }
>               catch (FileNotFoundException e) {
>                       throw new ConfigException("CT: Configfile not found: "+e);
>               }
> 
>       [ -- cut -- ]
> 
>       }
> 
> produces different results of tc4 vs tc3.2 and tc3.3
> 
> On tc3.3m2:
> This is the config URL: 
>file:/devel/endre/active/SERVER/webapps/CAF/WEB-INF/classes/logconfig.xml
> This is the config URL.getFile(): 
>/devel/endre/active/SERVER/webapps/CAF/WEB-INF/classes/logconfig.xml
> This is the config file: 
>/devel/endre/active/SERVER/webapps/CAF/WEB-INF/classes/logconfig.xml
> - and it works fine.
> 
> Excat the same with tc3.2.2b2:
> This is the config URL: 
>file:/devel/endre/active/SERVER/webapps/CAF/WEB-INF/classes/logconfig.xml
> This is the config URL.getFile(): 
>/devel/endre/active/SERVER/webapps/CAF/WEB-INF/classes/logconfig.xml
> This is the config file: 
>/devel/endre/active/SERVER/webapps/CAF/WEB-INF/classes/logconfig.xml
> - still fine
> 
> But On tc4.0b1:
> This is the config URL: jndi:/WEB-INF/classes/logconfig.xml
> This is the config URL.getFile(): /WEB-INF/classes/logconfig.xml
> This is the config file: /WEB-INF/classes/logconfig.xml
> com.corelets.settings.ConfigException: CT: Configfile not found: 
>java.io.FileNotFoundException: /WEB-INF/classes/logconfig.xml (No such
> file or directory)
>         at com.corelets.server.Startup.logConfig(Startup.java:234)
>         at com.corelets.server.Startup.init(Startup.java:46)
>         at javax.servlet.GenericServlet.init(GenericServlet.java:258)
>         at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:779)
>  ...
> - and here it breaks, of course.
> 

Could you try this with a recent nightly build?  There have been lots of
bug fixes since 4.0b1 (we're about to roll a 4.0b2 if you want to wait a
couple of days).

However, your code is making an assumption that is not portable.  Servlet
containers are *not* required to unpack your application into a directory
structure, so there is no guarantee that there will be such a thing as a
File that you can read.  The portable way to open this resource would be:

  InputStream is =
    DOMConfigurator.class.getResourceAsStream("log4j.dtd");


> What's that "jndi:" stuff instead of "file:" ??
> 

The "jndi:" stuff is because Tomcat 4.0 provides its own URLStreamHandlers
to access resources from WEB-INF/classes and WEB-INF/lib.  This stream
handler works when the application runs from a directory structure, or
directly from a WAR file.  The servlet spec requires that a servlet
container implement the getResource() and getResourceAsStream() calls and
provide any custom stream handlers required to do so.



> -- 
> Mvh,
> Endre
> 
> 

Reply via email to