Author: markt
Date: Tue May  4 10:59:52 2010
New Revision: 940802

URL: http://svn.apache.org/viewvc?rev=940802&view=rev
Log:
Re-factor Valves to use LifecycleMBeanBase

Modified:
    tomcat/trunk/java/org/apache/catalina/core/ContainerBase.java
    tomcat/trunk/java/org/apache/catalina/core/StandardPipeline.java
    tomcat/trunk/java/org/apache/catalina/core/StandardWrapperValve.java
    tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java

Modified: tomcat/trunk/java/org/apache/catalina/core/ContainerBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ContainerBase.java?rev=940802&r1=940801&r2=940802&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ContainerBase.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/ContainerBase.java Tue May  4 
10:59:52 2010
@@ -1089,6 +1089,11 @@ public abstract class ContainerBase exte
     @Override
     protected void destroyInternal() throws LifecycleException {
 
+        // Stop the Valves in our pipeline (including the basic), if any
+        if (pipeline instanceof Lifecycle) {
+            ((Lifecycle) pipeline).destroy();
+        }
+
         // Remove children now this container is being destroyed
         for (Container child : findChildren()) {
             child.destroy();

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardPipeline.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardPipeline.java?rev=940802&r1=940801&r2=940802&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardPipeline.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardPipeline.java Tue May  4 
10:59:52 2010
@@ -35,7 +35,6 @@ import org.apache.catalina.valves.ValveB
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
-import org.apache.tomcat.util.modeler.Registry;
 
 
 /**
@@ -181,7 +180,6 @@ public class StandardPipeline extends Li
         while (current != null) {
             if (current instanceof Lifecycle)
                 ((Lifecycle) current).start();
-            registerValve(current);
                current = current.getNext();
         }
 
@@ -209,7 +207,6 @@ public class StandardPipeline extends Li
         while (current != null) {
             if (current instanceof Lifecycle)
                 ((Lifecycle) current).stop();
-            unregisterValve(current);
                current = current.getNext();
         }
     }
@@ -217,7 +214,10 @@ public class StandardPipeline extends Li
     
     @Override
     protected void destroyInternal() {
-        // NOOP
+        Valve[] valves = getValves();
+        for (Valve valve : valves) {
+            removeValve(valve);
+        }
     }
 
     
@@ -233,55 +233,7 @@ public class StandardPipeline extends Li
     }
     
     
-    private void registerValve(Valve valve) {
-
-        if( valve instanceof ValveBase &&
-                ((ValveBase)valve).getObjectName()==null ) {
-            try {
-                
-                String domain=((ContainerBase)container).getDomain();
-                if( container instanceof StandardContext ) {
-                    domain=((StandardContext)container).getEngineName();
-                }
-                if( container instanceof StandardWrapper) {
-                    Container ctx=((StandardWrapper)container).getParent();
-                    domain=((StandardContext)ctx).getEngineName();
-                }
-                ObjectName vname=((ValveBase)valve).createObjectName(
-                        domain,
-                        ((ContainerBase)container).getJmxName());
-                if( vname != null ) {
-                    ((ValveBase)valve).setObjectName(vname);
-                    Registry.getRegistry(null, null).registerComponent
-                        (valve, vname, valve.getClass().getName());
-                    ((ValveBase)valve).setController
-                        (((ContainerBase)container).getJmxName());
-                }
-            } catch( Throwable t ) {
-                log.info( "Can't register valve " + valve , t );
-            }
-        }
-    }
     
-    private void unregisterValve(Valve valve) {
-        if( valve instanceof ValveBase ) {
-            try {
-                ValveBase vb=(ValveBase)valve;
-                if( vb.getController()!=null &&
-                        vb.getController() == 
-                        ((ContainerBase)container).getJmxName() ) {
-                    
-                    ObjectName vname=vb.getObjectName();
-                    Registry.getRegistry(null, null).getMBeanServer()
-                        .unregisterMBean(vname);
-                    ((ValveBase)valve).setObjectName(null);
-                }
-            } catch( Throwable t ) {
-                log.info( "Can't unregister valve " + valve , t );
-            }
-        }
-    }    
-
     // ------------------------------------------------------- Pipeline Methods
 
 
@@ -346,8 +298,6 @@ public class StandardPipeline extends Li
                 log.error("StandardPipeline.setBasic: start", e);
                 return;
             }
-            // Register the newly added valve
-            registerValve(valve);
         }
 
         // Update the pipeline
@@ -399,8 +349,6 @@ public class StandardPipeline extends Li
                     log.error("StandardPipeline.addValve: start: ", e);
                 }
             }
-            // Register the newly added valve
-            registerValve(valve);
         }
 
         // Add this Valve to the set associated with this Pipeline
@@ -501,10 +449,13 @@ public class StandardPipeline extends Li
                     log.error("StandardPipeline.removeValve: stop: ", e);
                 }
             }
-            // Unregister the removed valve
-            unregisterValve(valve);
         }
-    
+        try {
+            ((Lifecycle) valve).destroy();
+        } catch (LifecycleException e) {
+            log.error("StandardPipeline.removeValve: destroy: ", e);
+        }
+        
         container.fireContainerEvent(Container.REMOVE_VALVE_EVENT, valve);
     }
 

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardWrapperValve.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardWrapperValve.java?rev=940802&r1=940801&r2=940802&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardWrapperValve.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardWrapperValve.java Tue 
May  4 10:59:52 2010
@@ -21,8 +21,6 @@ package org.apache.catalina.core;
 
 import java.io.IOException;
 
-import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
 import javax.servlet.DispatcherType;
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
@@ -31,6 +29,7 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.catalina.Context;
 import org.apache.catalina.Globals;
+import org.apache.catalina.LifecycleException;
 import org.apache.catalina.comet.CometEvent;
 import org.apache.catalina.comet.CometProcessor;
 import org.apache.catalina.connector.ClientAbortException;
@@ -570,13 +569,9 @@ final class StandardWrapperValve
     public void setErrorCount(int errorCount) {
         this.errorCount = errorCount;
     }
-
-    // Don't register in JMX
-
+    
     @Override
-    public ObjectName createObjectName(String domain, ObjectName parent)
-            throws MalformedObjectNameException
-    {
-        return null;
+    protected void initInternal() throws LifecycleException {
+        // NOOP - Don't register this Valve in JMX
     }
 }

Modified: tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java?rev=940802&r1=940801&r2=940802&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java Tue May  4 
10:59:52 2010
@@ -21,10 +21,6 @@ package org.apache.catalina.valves;
 
 import java.io.IOException;
 
-import javax.management.MBeanRegistration;
-import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
 import javax.servlet.ServletException;
 
 import org.apache.catalina.Contained;
@@ -34,17 +30,16 @@ import org.apache.catalina.Engine;
 import org.apache.catalina.Host;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.LifecycleState;
-import org.apache.catalina.Pipeline;
 import org.apache.catalina.Valve;
 import org.apache.catalina.Wrapper;
 import org.apache.catalina.comet.CometEvent;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
-import org.apache.catalina.core.ContainerBase;
+import org.apache.catalina.mbeans.MBeanUtils;
 import org.apache.catalina.util.LifecycleBase;
+import org.apache.catalina.util.LifecycleMBeanBase;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.juli.logging.Log;
-import org.apache.juli.logging.LogFactory;
 
 
 /**
@@ -58,9 +53,8 @@ import org.apache.juli.logging.LogFactor
  * @version $Id$
  */
 
-public abstract class ValveBase extends LifecycleBase
-    implements Contained, Valve, MBeanRegistration {
-    private static final Log log = LogFactory.getLog(ValveBase.class);
+public abstract class ValveBase extends LifecycleMBeanBase
+    implements Contained, Valve {
 
     //------------------------------------------------------ Constructor
     
@@ -188,6 +182,7 @@ public abstract class ValveBase extends 
      * throwables will be caught and logged.
      */
     public void backgroundProcess() {
+        // NOOP by default
     }
 
 
@@ -227,10 +222,6 @@ public abstract class ValveBase extends 
     }
 
 
-    protected void initInternal() {
-        // NOOP
-    }
-
     /**
      * Start this component and implement the requirements
      * of {...@link LifecycleBase#startInternal()}.
@@ -259,12 +250,6 @@ public abstract class ValveBase extends 
     }
     
     
-    @Override
-    protected void destroyInternal() {
-        // NOOP
-    }
-
-
     /**
      * Return a String rendering of this object.
      */
@@ -283,128 +268,74 @@ public abstract class ValveBase extends 
 
 
     // -------------------- JMX and Registration  --------------------
-    protected String domain;
-    protected ObjectName oname;
-    protected MBeanServer mserver;
-    protected ObjectName controller;
-
-    public ObjectName getObjectName() {
-        return oname;
-    }
-
-    public void setObjectName(ObjectName oname) {
-        this.oname = oname;
-    }
-
-    public String getDomain() {
-        return domain;
-    }
-
-    public ObjectName preRegister(MBeanServer server,
-                                  ObjectName name) throws Exception {
-        oname=name;
-        mserver=server;
-        domain=name.getDomain();
-
-
-        return name;
-    }
-
-    public void postRegister(Boolean registrationDone) {
-    }
-
-    public void preDeregister() throws Exception {
-    }
-
-    public void postDeregister() {
-    }
-
-    public ObjectName getController() {
-        return controller;
-    }
-
-    public void setController(ObjectName controller) {
-        this.controller = controller;
-    }
-
-    /** From the name, extract the parent object name
-     *
-     * @param valveName The valve name
-     * @return ObjectName The parent name
-     */
-    public ObjectName getParentName( ObjectName valveName ) {
-
-        return null;
-    }
+    @Override
+    public String getObjectNameKeyProperties() {
+        StringBuilder name = new StringBuilder("type=Valve");
+        
+        Container container = getContainer();
+        int unknown = 0;
 
-    public ObjectName createObjectName(String domain, ObjectName parent)
-            throws MalformedObjectNameException
-    {
-        Container container=this.getContainer();
-        if( container == null || ! (container instanceof ContainerBase) )
-            return null;
-        this.containerLog = container.getLogger();
-        ContainerBase containerBase=(ContainerBase)container;
-        Pipeline pipe=containerBase.getPipeline();
-        Valve valves[]=pipe.getValves();
-
-        /* Compute the "parent name" part */
-        String parentName="";
-        if (container instanceof Engine) {
-        } else if (container instanceof Host) {
-            parentName=",host=" +container.getName();
-        } else if (container instanceof Context) {
-                    String path = ((Context)container).getPath();
-                    if (path.length() < 1) {
-                        path = "/";
-                    }
-                    Host host = (Host) container.getParent();
-                    parentName=",path=" + path + ",host=" +
-                            host.getName();
-        } else if (container instanceof Wrapper) {
-            Context ctx = (Context) container.getParent();
-            String path = ctx.getPath();
-            if (path.length() < 1) {
-                path = "/";
+        // Work up container hierarchy, add a component to the name for
+        // each container
+        while (!(container instanceof Engine)) {
+            if (container instanceof Wrapper) {
+                name.append(",servlet=");
+                name.append(container.getName());
+            } else if (container instanceof Context) {
+                String path = ((Context)container).getPath();
+                if (path.length() < 1) {
+                    path = "/";
+                }
+                name.append(",path=");
+                name.append(path);
+            } else if (container instanceof Host) {
+                name.append(",host=");
+                name.append(container.getName());
+            } else {
+                // Should never happen...
+                name.append(",unknown");
+                name.append(unknown++);
+                name.append('=');
+                name.append(container.getName());
             }
-            Host host = (Host) ctx.getParent();
-            parentName=",servlet=" + container.getName() +
-                    ",path=" + path + ",host=" + host.getName();
+            container = container.getParent();
         }
-        log.debug("valve parent=" + parentName + " " + parent);
 
-        String className=this.getClass().getName();
-        int period = className.lastIndexOf('.');
-        if (period >= 0)
-            className = className.substring(period + 1);
-
-        int seq=0;
-        for( int i=0; i<valves.length; i++ ) {
-            // Find other valves with the same name
-            if (valves[i] == this) {
+        int seq = 0;
+        for (Valve valve : container.getPipeline().getValves()) {
+            // Skip null valves
+            if (valve == null) {
+                continue;
+            }
+            // Only compare valves in pipeline until we find this valve
+            if (valve == this) {
                 break;
             }
-            if( valves[i]!=null &&
-                    valves[i].getClass() == this.getClass() ) {
-                log.debug("Duplicate " + valves[i] + " " + this + " " + 
container);
-                seq++;
+            if (valve.getClass() == this.getClass()) {
+                // Duplicate valve earlier in pipeline
+                // increment sequence number
+                seq ++;
             }
         }
-        String ext="";
-        if( seq > 0 ) {
-            ext=",seq=" + seq;
+        
+        if (seq > 0) {
+            name.append(",seq=");
+            name.append(seq);
         }
 
-        ObjectName objectName = 
-            new ObjectName( domain + ":type=Valve,name=" + className + ext + 
parentName);
-        log.debug("valve objectname = "+objectName);
-        return objectName;
+        String className = this.getClass().getName();
+        int period = className.lastIndexOf('.');
+        if (period >= 0) {
+            className = className.substring(period + 1);
+        }
+        name.append(",name=");
+        name.append(className);
+        
+        return name.toString();
     }
 
-    // -------------------- JMX data  --------------------
-
-    public ObjectName getContainerName() {
-        if( container== null) return null;
-        return ((ContainerBase)container).getJmxName();
+    @Override
+    public String getDomainInternal() {
+        return MBeanUtils.getDomain(getContainer());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to