From: markt Date: Tue, 4 May 2010 10:59:52 +0000 (+0000) Subject: Re-factor Valves to use LifecycleMBeanBase X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=7bfc18a29a4a07f963bc302d6ac49596bdc567d7;p=tomcat7.0 Re-factor Valves to use LifecycleMBeanBase git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@940802 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/core/ContainerBase.java b/java/org/apache/catalina/core/ContainerBase.java index d6dff03e9..83d739195 100644 --- a/java/org/apache/catalina/core/ContainerBase.java +++ b/java/org/apache/catalina/core/ContainerBase.java @@ -1089,6 +1089,11 @@ public abstract class ContainerBase extends LifecycleBase @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(); diff --git a/java/org/apache/catalina/core/StandardPipeline.java b/java/org/apache/catalina/core/StandardPipeline.java index 7fdc3bf0a..d1af28bef 100644 --- a/java/org/apache/catalina/core/StandardPipeline.java +++ b/java/org/apache/catalina/core/StandardPipeline.java @@ -35,7 +35,6 @@ import org.apache.catalina.valves.ValveBase; 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 LifecycleBase while (current != null) { if (current instanceof Lifecycle) ((Lifecycle) current).start(); - registerValve(current); current = current.getNext(); } @@ -209,7 +207,6 @@ public class StandardPipeline extends LifecycleBase while (current != null) { if (current instanceof Lifecycle) ((Lifecycle) current).stop(); - unregisterValve(current); current = current.getNext(); } } @@ -217,7 +214,10 @@ public class StandardPipeline extends LifecycleBase @Override protected void destroyInternal() { - // NOOP + Valve[] valves = getValves(); + for (Valve valve : valves) { + removeValve(valve); + } } @@ -233,55 +233,7 @@ public class StandardPipeline extends LifecycleBase } - 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 LifecycleBase 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 LifecycleBase 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 LifecycleBase 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); } diff --git a/java/org/apache/catalina/core/StandardWrapperValve.java b/java/org/apache/catalina/core/StandardWrapperValve.java index 991e39ec5..e847dc884 100644 --- a/java/org/apache/catalina/core/StandardWrapperValve.java +++ b/java/org/apache/catalina/core/StandardWrapperValve.java @@ -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.HttpServletResponse; 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 } } diff --git a/java/org/apache/catalina/valves/ValveBase.java b/java/org/apache/catalina/valves/ValveBase.java index af86ccfaf..b8617016e 100644 --- a/java/org/apache/catalina/valves/ValveBase.java +++ b/java/org/apache/catalina/valves/ValveBase.java @@ -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.LogFactory; * @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 LifecycleBase * throwables will be caught and logged. */ public void backgroundProcess() { + // NOOP by default } @@ -227,10 +222,6 @@ public abstract class ValveBase extends LifecycleBase } - protected void initInternal() { - // NOOP - } - /** * Start this component and implement the requirements * of {@link LifecycleBase#startInternal()}. @@ -259,12 +250,6 @@ public abstract class ValveBase extends LifecycleBase } - @Override - protected void destroyInternal() { - // NOOP - } - - /** * Return a String rendering of this object. */ @@ -283,128 +268,74 @@ public abstract class ValveBase extends LifecycleBase // -------------------- 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; - } - - 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 = "/"; + @Override + public String getObjectNameKeyProperties() { + StringBuilder name = new StringBuilder("type=Valve"); + + Container container = getContainer(); + int unknown = 0; + + // 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 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()); } }