Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

Re: Helping setting appropriate prefix when using ServletContext.getResourceAsStream()

James Dekker

2006-12-22

Replies:

Well, I actually got the properties file loading properly!

When I set undeployWars="false" in server.xml, I can still see my log
messages inside my Tomcat console!

Here's my corrected file (Log4jInitServlet):

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.PropertyConfigurator;

public class Log4jInitServlet extends HttpServlet {
 public void init() throws ServletException {
    Properties props = new Properties();
  //  String file = getServletContext().getInitParameter("log4j-init");
  //  File propFile = new File(file);
  //  if (!propFile.exists()) {
  //    System.out.println("log4j.properties file not found" +
propFile.getAbsolutePath());
  //  }
 
    try {
     props.load(this.getClass().getClassLoader().getResourceAsStream("/log4j.properties"));
   } catch (IOException e) {
     e.printStackTrace();
   }
 
   // PropertyConfigurator.configureAndWatch(propFile.getAbsolutePath(),10000);
  }
}

Now, my problem is that I can't use the PropertyConfigurator because I
placed log4j.properties in WEB-INF/classes and then just placed it
into my CLASSPATH using getResourceAsStream(). If you look at my code
whihc contains the invocation to PropertyConfigurator, I need a File
type to place as the first parameter inside the configureAndWatch()
method.

Is there a way to convert this stream as File?

Happy holidays!

-JD

On 12/22/06, Hassan Schroeder <hassan.schroeder@(protected):
> On 12/22/06, James Dekker <james.dekker@(protected):
>
> >     <Host name="localhost" appBase="webapps"
> >     unpackWARs="true" autoDeploy="true"
> >     xmlValidation="false" xmlNamespaceAware="false">
> >     </Host>
>
> > tomcat.home=C:/DevTools/tomcat/jakarta-tomcat-5.5.9
> > tomcat.deployment.dir=${tomcat.home}/webapps
>
> Most of that was irrelevant :-) but the above doesn't mesh with your
> earlier path:
> C:\DevTools\tomcat\jakarta-tomcat-5.5.9\mywebapp\WEB-INF\classes\log4j.properties.
> :: which doesn't include a 'webapps' directory.
>
> Is that just a typo or are you setting Context atttributes (like docBase)
> somewhere else?
>
> --
> Hassan Schroeder ------------------------ hassan.schroeder@(protected)
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@(protected)
> To unsubscribe, e-mail: users-unsubscribe@(protected)
> For additional commands, e-mail: users-help@(protected)
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@(protected)
To unsubscribe, e-mail: users-unsubscribe@(protected)
For additional commands, e-mail: users-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.