aspose file tools*
The moose likes Tomcat and the fly likes Logging Problem with unpackWARs= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Logging Problem with unpackWARs="false"" Watch "Logging Problem with unpackWARs="false"" New topic
Author

Logging Problem with unpackWARs="false"

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Hello there,

Am using JDK 1.5 and Tomcat 5.5.9...

I have two init servlets for the following purposes:

1. One is for logging

2. The other is for loading (and parsing) XML based configuration files using Commons Digester.

When I go to %CATALINA_HOME%/conf/server.xml and inside the <HOST> tag, when I set the unpackWars attribute to false, I see a bunch of null values inside where my logging is supposed to be set!

If I set it to true, everything works well!

e.g. when I set unpackWars = false, redeploy my app, and then start Tomcat this is what I get:



My %CATALINA_HOME% is c:\DevTool\tomcat\jakarta-tomcat-5.5.9\

and when I deploy my application, its supposed to auto load these particular files inside %CATALINA_HOME%\myapp\WEB-INF, but since I didn't want to unpack my war files, it puts that weird nullWEB-INF inside the particular path!?

Does this mean that I can't have my servlets load init params unless my war files is always unpacked?

Here's some code for your review:







Has anyone experienced the same kind of thing?

Thanks again, JavaRanch rocks!

Happy holidays,

JD
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Here's your problem:


The getRealPath method only works if your application is being run from an actual file system hierarchy.
If you don't unpack your war file getRealPath is supposed to return null becuase there is no real path.




From: java.lang.String&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)


getRealPath

public java.lang.String getRealPath(java.lang.String path)

Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed





[ December 21, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Dear Mr. Souther,

That was such a good find (on your part)!

Thank you so much!

After looking at this article

It seems like the only three ways to do is are:

1. In %CATALINA_HOME%/conf/context.xml

set the following attribute to true by doing this:

antiJARLocking="true" inside the <Context> tag

2. Do step1 and then move a copy of your context.xml file to %CATALINA_HOME%/yourapp/META-INF/

3. Use the ClassLoader to load the XML config file and log4j.properties file instead of using getServletContext().getRealPath(). Can anyone explain how to use the ClassLoader to do this?

Would it be something like this:



Can anyone suggest an alternative to my getServletContext().getRealPath()?

Does anyone know of a better way than the ones listed above?

Sincerely,

JD
[ December 21, 2006: Message edited by: James Dekker ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes,

If all you need to do is read files, look at
ServletContext.getResource
and ServletContext.getResourceAsStream
java.lang.String&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResource(java.lang.String)

Also look at:
http://wiki.apache.org/tomcat/HowTo#head-45c3314139cb900ddd43dde2ff671532e6e844bc

Lastly, Log4j will look for log4j.properties in the app's classpath.
I keep mine in WEB-INF/classes and it finds it.
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Dear Mr. Souther:

Thank you very much for your helpful responses!

I tried my first two suggestions (of modifying the context.xml file and moving it under mywebapp/META-INF) and when I set the unpackWars attribute to false, again, and redeployed my app, this is what I got:



I'll try the ServletContext.getResource() and ServletContext().getResourceAsStream() methods next...

Happy holidays!

-JD
[ December 21, 2006: Message edited by: James Dekker ]
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Dear Mr. Souther & others,

I tried using the ServletContext.getResourceAsStream() and made the following changes to my Log4jInitServlet:



I decided to test by having the war file undepacked, first.

Now, when I deploy the app (with the unpackWars = true), this is what I get in my console:



The propFile is equal the following path:

%CATALINA_HOME%/webapps/mywebapp/WEB-INF/

Because my build script places my log4j.properties in the following place:

%CATALINA_HOME%/webapps/mywebapp/WEB-INF/log4j.properties

What could I be possibly be doing wrong?

Sincerely yours,

JD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logging Problem with unpackWARs="false"