3 Replies Latest reply on Jun 21, 2006 2:34 PM by jofree

    Adding a Tomcat Valve Via An MBean?

    jiwils

      I would like to add the Tomcat AccessLogValve programmatically to a running JBossAS instance. I would like to be able to leave the default server.xml configuration alone so it can be packaged "as is", and it would be neat to be able to enable/disable a valve such as this on an "as needed" basis.

      While it looks like I might could create a server configuration Element and pass that to the server through the JBossWeb/Tomcat MBeans, I would rather not completely reconfigure Tomcat in order to accomplish this (and I am guessing that this route would cause that behavior). Even if there is no "upfront" MBean way to do this, could I add an MBean that could do so programatically by using Tomcat-specific classes, and if so, could anyone help point me to the classes that I need to look at in order to do so?

      I would have no problem contributing such an MBean if others would find it useful to add any valve to Tomcat on the fly (not just the one I need).

        • 1. Re: Adding a Tomcat Valve Via An MBean?
          jiwils

          It looks like JBoss deploys valves in semi-dynamic fashion since the tomcat5-service.jar contains Tomcat MBean descriptor XML files. I am unsure how these are getting deployed however. I tried mocking up my own JAR with a descriptor for a valve and placed it in the jbossweb-tomcat55.sar directory, but I had no luck with that. I also tried using the Listener concept in server.xml as described by http://tomcat.apache.org/tomcat-5.0-doc/mbeans-descriptor-howto.html to refer to the same mock JAR's descriptor, but that did not seem to make a difference. Am I approaching this the correct way?

          • 2. Re: Adding a Tomcat Valve Via An MBean?
            starksm64

            Valves can be deployed via the tomcat sar context.xml as well as war level WEB-INF/context.xml descriptors.

            http://wiki.jboss.org/wiki/Wiki.jsp?page=CustomizingSecurityUsingValves

            • 3. Re: Adding a Tomcat Valve Via An MBean?
              jofree

              I've been able to achieve this by binding to the MBean that represents the Host and calling the Pipeline.addValve() method as shown here:

               ObjectName objectName = null;
               Object resource = null;
               Valve someValve = (Valve) new SomeValve();
               try {
               objectName = new ObjectName("jboss.web:type=Host,host=localhost");
               resource = (Object) server.getAttribute(objectName, "managedResource");
               if (resource != null && someValve != null) {
               StandardHost standardHost = (StandardHost) resource;
               standardHost.getPipeline().addValve(someValve);
               }
               else {
               //TODO Error Checking
               }
               }
               catch (Exception e) {
               //TODO Error Checking
               }
              
              


              My MBean instanceates the valve in the start() lifecycle method and keeps a reference to it so it can also call removeValve in the same fashion in the stop lifecycle method. This way the valve is installed when the SAR (or MBean) is deployed and uninstalled when the SAR is undeployed. Of course this can also be controlled via the JMX console as well.

              I did have to do a couple of other tricks... I had to move this code into a utility class to avoid class loader issues (namely Valve and ValveBase aren't available until Tomcat has started). I also had to set up a BarrierController on Tomcat so I can make my MBean depend on Tomcat starting.

              Hope this helps,

              Josh Freeman