This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Tomcat and the fly likes problem writing tomcat valve Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "problem writing tomcat valve" Watch "problem writing tomcat valve" New topic
Author

problem writing tomcat valve

steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
I have written a custom monitoring valve for tomcat 5.5. and tomcat 6. I added the following line after the <Engine> tag in both 5.5 and 6.


The valve works fine in tomcat 6, but the server doesn't boot in tomcat 5.5 (it flashes too quick for me to see what is happening). Does anyone have any knowledge of valves that may help me solve this problem? both versions use the same jdk.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you looked through the logs in the TOMCAT_INSTALL/logs directory?


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

Joined: Jun 26, 2002
Posts: 860
I have a little more info after conducting a few tests.

I have a custom valve that monitors page accesses in tomcat. It works in tomcat 6, and under tomcat 5.5 that comes bundled with jboss. I can not get it to work in tomcat 5.5.25. I put the jar in server/lib and the war I install that reads the monitoring data gets calls def not found errors on the classes in my jar when it tries to display the data. Shouldn't my war be able to see the classes from a jar put into server/lib? I noticed that the management wars were put under server/webapps. Do I need to do something along these lines?
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
I have a little more info after conducting a few tests.

I have a custom valve that monitors page accesses in tomcat. It works in tomcat 6, and under tomcat 5.5 that comes bundled with jboss. I can not get it to work in tomcat 5.5.25. I put the jar in server/lib and the war I install that reads the monitoring data gets calls def not found errors on the classes in my jar when it tries to display the data. Shouldn't my war be able to see the classes from a jar put into server/lib? I noticed that the management wars were put under server/webapps. Do I need to do something along these lines?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by steve souza:
Shouldn't my war be able to see the classes from a jar put into server/lib?


This is not allowed by default.
To grant access to container level objects, set the 'privileged' attribute in your context to true.

Of course, doing so increases the size and scope of the sandbox in which your application runs so you might want to read up on some of the security implications of doing so.

Personally, I would only write a valve if I wanted to implement something container wide. If I wanted this behavior for one application, I would implement it as a filter. Is there any reason that you're writing it as a valve?
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Personally, I would only write a valve if I wanted to implement something container wide. If I wanted this behavior for one application, I would implement it as a filter. Is there any reason that you're writing it as a valve?


I am writing the valve to be a server wide resource. jamon.war simply displays the statistical information that is gathered at the server level. I simply dropped my jar and war in for tomcat 6, and jboss tomcat 5.5 and it worked with no problems. not sure why tomcat 5.5 from the apache site works differently.

To see what jamon war does click on the link below that shows sql statistics. The valve will display different http stats from with in this same war.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
It seems to be a classpath problem, although i don't know how to make BOTH tomcat and my war avoid ClassNotFoundExceptions.

If i put jamon.jar in common/lib the jamon viewer servlet can see the classes, however tomcat bootup code can not (and the server doesn't boot). if i put jamon.jar in shared/lib the tomcat boot process works however my viewer war can't see the classes.

Here is the stack trace i get when I put jamon.jar in common/lib

WARNING: Catalina.start using conf/server.xml:
java.lang.ClassNotFoundException: com.jamonapi.http.JAMonTomcatValve
at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2726)
at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2752)
at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1279)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement (AbstractSAXParser.java:533)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:220)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement (XMLDocumentFragmentScannerImpl.java:872)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1693)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument (XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse (XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1562)
at org.apache.catalina.startup.Catalina.load(Catalina.java:490)
at org.apache.catalina.startup.Catalina.start(Catalina.java:544)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Sep 10, 2007 1:11:45 PM org.apache.catalina.startup.Catalina start
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Steve,
It looks like you're getting some pretty good help from Charles Caldarale on the Tomcat user's list. Did he answer everything for you or is this a new one?

-Ben
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
I am still having the problem. He had told me to put the jar in common/lib, and I had already tried putting the jar in all of the possible locations when he said that. None seem to work. I either get an exception in my war, or on server startup. Being as the Valve works in both tomcat 6, and jboss tomcat 5.5 I am not sure how to proceed.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you also set priviledged to true?
If your application needs to use Tomcat components, you will need to do this.
I'm not sure why it's working without this in version 6.
Admittedly, I haven't done much work with version 6 yet.

If Tomcat needs to access your classes then you would want to put your jars (or class/package directory structure) under tomcat/common/lib.
A good example of this is a JDBC driver that Tomcat's connection pool needs access to when the container starts up.

I'm guessing, since a valve is a container wide component, any dependents it has would need to be placed in tomcat/common/lib.

If within your application, you need access to Tomcat's components,
I believe you will need to set the privileged attribute to true.

From:
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html


privileged
Set to true to allow this context to use container servlets, like the manager servlet.


These seem to be two different issues you're dealing with (fighting).
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Yes, i had set priviledge to true and that didn't seem to help. Based on help from the tomcat mailing list I had to separate my valve from the rest of my jamon.jar. That is less than ideal, but works. Here is that thread below.


****

That worked. I created my valve as a single class and put it in server/classes. I kept my jar in common/lib. I tried inheriting from the valve in my jar and that didn't work, so i copied that code into a new package and it worked fine. It is still a bit mysterious and not ideal as installation requires a class put in server/classes as well as the jar in common/lib, but it will do. If there is a better way to do this let me know. Thanks for your help charles.

On 9/10/07, Caldarale, Charles R <Chuck.Caldarale@unisys.com> wrote:

> From: Steve Souza [mailto:jamonapi@gmail.com]
> Subject: Re: classpath problems with a custom valve
>
> I get the following exception which seems odd as I saw the missing
class in
> catalina.jar and I compiled against it.

Java classloading hierarchy is not bidirectional - classes can see only
up the tree (unless you're using reflection). For Tomcat 5.5, I think
you'll need to refactor your code, separating the valve and the part
needed by both the valve and the webapp. The jar containing valve
proper should be placed in server/lib, and the jar holding the common
bits into common/lib.

If you moved up to Tomcat 6, the classloading hierarchy is much simpler;
your existing jar would just go into the single lib directory.

- Chuck
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by steve souza:
Yes, i had set priviledge to true and that didn't seem to help.


Again, I think you were dealing with two different problems (Tomcat's valve having access to your classes and your webapp having access Tomcat's internals).

It looks like you've got the two separated now.
Thanks for posting back with your solution.
 
Consider Paul's rocket mass heater.
 
subject: problem writing tomcat valve
 
Similar Threads
filter for j_security_check
JDBC Realm inside the COntext element
web application performance monitoring
Reg Tom cat valve
Security Contraints setup..