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;
/**
while (current != null) {
if (current instanceof Lifecycle)
((Lifecycle) current).start();
- registerValve(current);
current = current.getNext();
}
while (current != null) {
if (current instanceof Lifecycle)
((Lifecycle) current).stop();
- unregisterValve(current);
current = current.getNext();
}
}
@Override
protected void destroyInternal() {
- // NOOP
+ Valve[] valves = getValves();
+ for (Valve valve : valves) {
+ removeValve(valve);
+ }
}
}
- 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
log.error("StandardPipeline.setBasic: start", e);
return;
}
- // Register the newly added valve
- registerValve(valve);
}
// Update the pipeline
log.error("StandardPipeline.addValve: start: ", e);
}
}
- // Register the newly added valve
- registerValve(valve);
}
// Add this Valve to the set associated with this Pipeline
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);
}
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;
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;
/**
* @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
* throwables will be caught and logged.
*/
public void backgroundProcess() {
+ // NOOP by default
}
}
- protected void initInternal() {
- // NOOP
- }
-
/**
* Start this component and implement the requirements
* of {@link LifecycleBase#startInternal()}.
}
- @Override
- protected void destroyInternal() {
- // NOOP
- }
-
-
/**
* Return a String rendering of this object.
*/
// -------------------- 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<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());
}
}