Tomcat Valve mecanism is a way to have a kind of servlet filter but before the servlet chain. In two words it is commonly used for container and security usages/needs.
Main issue is the common way to add a valve is to configure it in server.xml or context.xml. Even if this last one can be packaged in a war, the valve itself can’t since this file is used before the webapp classloader is setup to build the webapp setup/context.
To solve it TomEE now provides a LazyValve implementation.
Idea is simply to provide in the container a valve able to instantiate lazily another valve which could be packaged in the war.
Configuration is then quite trivial. Just put in META-INF/context.xml of your war (take care it is not WEB-INF/classes/META-INF/context.xml but really just META-INF/context.xml):
<Context> <Valve className="org.apache.tomee.catalina.valve.LazyValve" delegateClassName="org.superbiz.MyValve" properties="attribute1=value1 attribute2=value2 attribute3=value3" /> </Context>
As you can see the main configuration is delegateClassName which is actually the real implementation name. If this valve supports configuration then it can be passed through properties attribute of LazyValve.
This really allows to go futher in the packaging of the application avoiding to split it between container setup and application setup – if you doubt of it just look how to setup Apache Fediz which relies on valves for good reasons but it also makes the setups not that trivial.
Note: LazyValve implementation doesn’t support CDI today (where LazyRealm can for instance). This is mainly cause CDI is very very rarely useful when implementing a valve. That said nothing really blocking to support it at technical level if you come with a good use case ;).
A very nice thing! Important to know is that it is available for 2.0.0-Milestone-1 and 1.7.2 but not for 1.7.1.
Reblogged this on Dinesh Ram Kali..