GlassFish v3 adds support for Tomcat-style valves

GlassFish v3 adds support for Tomcat-style valves

GlassFish v3 adds support for Tomcat-style valves

GlassFish v3 is removing a major stumbling block that has made migration from Tomcat to GlassFish difficult, by adding "out-of-the-box" support for Tomcat-style custom valves.

Historical background on Valve incompatibility

Tomcat's org.apache.catalina.Valve interface defines a single invocation point, with this signature:

  public void invoke(Request request, Response response)
      throws IOException, ServletException;

A Tomcat valve would implement this invoke method by performing its preprocessing logic on the request/response objects, invoking the next valve in the pipeline, and performing its postprocessing logic after the invocation of the next valve in the pipeline had returned:

  public void invoke(Request request, Response response) {

      // Pre-processing logic goes here

      // Invoke next valve in the pipeline
      getNext().invoke(request, response);

      // Post-processing logic goes here

As a result of this invocation style, every valve invocation would add one frame to the call stack.

In an attempt to flatten the call stack for improved performance, Sun Application Server 8.0 changed the org.apache.catalina.Valve interface by defining two invocation points: an invoke method for the valve's preprocessing logic, and a postInvoke method for the valve's postprocessing logic. Instead of having a valve be responsible for invoking the next valve in the pipeline, a valve would return control back to the web container after completion of its invoke and postInvoke methods, respectively. In the case of invoke, the valve would also indicate to the web container whether the next valve in the pipeline should be invoked (INVOKE_NEXT), or whether the processing of the request should be aborted (END_PIPELINE), and an error response generated immediately. For example, an authentication valve would return END_PIPELINE if it failed to authenticate the client.

In summary, the Sun-modified Valve interface would be implemented as follows:

  public int invoke(Request request, Response response) {
      // Pre-processing logic goes here
      return INVOKE_NEXT;
      return END_PIPELINE;

  public void postInvoke(Request request, Response response) {
      // Post-processing logic goes here

Valve migration issues from Tomcat to GlassFish

GlassFish v1 inherited the modified Valve interface from Sun Application Server 8.0. At the time, there was no compatibility issue with Tomcat, though, because valves were an implementation detail of the web container.

The incompatible changes to the Valve interface started to become an issue only when GlassFish v2 added support for custom valves. Custom valves could be configured at the virtual server and web application levels, and would be specified in form of a <virtual-server> property in domain.xml, or a web application property in sun-web.xml, respectively, by using the fully-qualified class name of the valve as the property value.

Once GlassFish v2 started supporting custom valves, Tomcat users who were trying to migrate their custom valves from Tomcat to GlassFish hit a roadblock, because their valves simply would not work in GlassFish, unless they were adapted to the Sun-modified Valve interface and recompiled (which, understandably, was not an option for most developers).

Valve alignment benefits in GlassFish v3

In an attempt to smoothen the migration from Tomcat to GlassFish, GlassFish v3 supports Tomcat-style valves out-of-the-box. This has been accomplished by a number of changes. First, the Sun-modified version of the Valve interface has been renamed and moved to a separate package. It now lives in org.glassfish.web.valve.GlassFishValve, and has "made room" for the org.apache.catalina.Valve interface from Tomcat in its original form.

By default, GlassFish v3 retains the performance benefits gained from the flattened valve invocation. Developers who would like to take advantage of the more efficient invocation style for their custom valves may choose to implement the org.glassfish.web.valve.GlassFishValve interface.

On the other hand, developers who would like to migrate their valves from Tomcat to GlassFish v3 can now do so, without having to change and recompile them: Their valves will work out-of-the-box!

Not only does GlassFish v3 support Tomcat-style valves, but it also preserves binary compatibility with valves compiled against the Sun-modified org.apache.catalina.Valve interface.

As an added benefit, GlassFish- and Tomcat-style valves may be mixed-and-matched, and added to a pipeline in any order!

Valve alignment implementation details

Following are some implementation details on how this all works:

All valves that are part of the web container implementation are implemented as hybrid valves, meaning they support both the Tomcat- and GlassFish invocation styles. By default, they are used in GlassFish mode, for improved performance.

Once a Tomcat-style valve has been added to a pipeline, any valves subsequently added to the same pipeline must also be of Tomcat style (this is because a Tomcat-style valve does not return control to the web container): If they are not already of Tomcat style, they will be wrapped inside a TomcatValveAdapter. Likewise, any valve that implements the modified Sun-modified org.apache.catalina.Valve interface is turned into an instance of org.glassfish.web.valve.GlassFishValve, by wrapping it with a GlassFishValveAdapter.

Please try out your Tomcat valves with GlassFish v3 and report back!


Hi Jan,

is there a way to pass initialization parameters to Valves? Which would be the syntax?

I had a lengthy discussion on the users mailing list with Jeanfrancois some months ago about this. Back then it was not possible.

See issue 4006:

Wolfram Rittmeyer

Posted by Wolfram Rittmeyer on September 10, 2008 at 05:31 AM PDT #

Hi Wolfram, passing init params to valves is still not supported. I hope we will be able to address issue 4006 in time for the final v3 release.



Posted by guest on September 22, 2008 at 03:36 AM PDT #

[Trackback] GlassFish v3 Prelude is out today, and as I do for every GlassFish release, here is a list of cool features the official marketing/documentation might not talk about.

Posted by Jean-Francois Arcand's Blog on November 05, 2008 at 10:55 PM PST #

I think initialization params can be passed if you Implement LifeCycle class...I am not sure about that

Posted by sumit on June 30, 2009 at 01:28 AM PDT #

The program in this article will be a big help for those students who started up dealing with their programs with regard to this topic.

Posted by speedfit on March 10, 2011 at 03:08 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed



« December 2016