From fd6ff5f27fc1a749e5983b25baada5141c17e23b Mon Sep 17 00:00:00 2001 From: markt Date: Sun, 2 May 2010 17:34:33 +0000 Subject: [PATCH] Add MBean registration and de-registration to LifecycleBase The new interface will need to be added to each component to get this to work. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@940271 13f79535-47bb-0310-9956-ffa450edef68 --- .../catalina/LifecycleMBeanRegistration.java | 46 ++++++++++++++++++++++ java/org/apache/catalina/util/LifecycleBase.java | 27 +++++++++++-- .../apache/catalina/util/LocalStrings.properties | 1 + 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 java/org/apache/catalina/LifecycleMBeanRegistration.java diff --git a/java/org/apache/catalina/LifecycleMBeanRegistration.java b/java/org/apache/catalina/LifecycleMBeanRegistration.java new file mode 100644 index 000000000..a505d36c1 --- /dev/null +++ b/java/org/apache/catalina/LifecycleMBeanRegistration.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.catalina; + +import javax.management.MBeanRegistration; +import javax.management.ObjectName; + +/** + * This interface extends the {@link MBeanRegistration} interface and adds + * methods for obtaining the domain and object name used to register this + * component. This interface is intended to be implemented by components that + * already implement {@link Lifecycle} to indicate that they require + * registration during {@link Lifecycle#init()} and de-registration during + * {@link Lifecycle#destroy()}. + */ +public interface LifecycleMBeanRegistration extends MBeanRegistration { + + /** + * Obtain the {@link ObjectName} under which this component will be / has + * been registered. + */ + public ObjectName getObjectName(); + + + /** + * Obtain the domain under which this component will be / has been + * registered. + */ + public String getDomain(); + +} diff --git a/java/org/apache/catalina/util/LifecycleBase.java b/java/org/apache/catalina/util/LifecycleBase.java index a519713b0..daf4554b8 100644 --- a/java/org/apache/catalina/util/LifecycleBase.java +++ b/java/org/apache/catalina/util/LifecycleBase.java @@ -17,12 +17,16 @@ package org.apache.catalina.util; +import javax.management.ObjectName; + import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleListener; +import org.apache.catalina.LifecycleMBeanRegistration; import org.apache.catalina.LifecycleState; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; +import org.apache.tomcat.util.modeler.Registry; import org.apache.tomcat.util.res.StringManager; @@ -95,7 +99,19 @@ public abstract class LifecycleBase implements Lifecycle { invalidTransition(Lifecycle.INIT_EVENT); } - // TODO - Check for JMX support and register if required + // Register MBean if required + if (this instanceof LifecycleMBeanRegistration) { + ObjectName oname = + ((LifecycleMBeanRegistration) this).getObjectName(); + + try { + Registry.getRegistry(null, null).registerComponent( + this, oname, null); + } catch (Exception e) { + log.warn(sm.getString("lifecycleBase.initMBeanFail", toString(), + oname), e); + } + } initInternal(); @@ -247,10 +263,15 @@ public abstract class LifecycleBase implements Lifecycle { invalidTransition(Lifecycle.DESTROY_EVENT); } - // TODO - Check for JMX support and de-register if required - destroyInternal(); + // De-register MBean if required + if (this instanceof LifecycleMBeanRegistration) { + ObjectName oname = + ((LifecycleMBeanRegistration) this).getObjectName(); + Registry.getRegistry(null, null).unregisterComponent(oname); + } + setState(LifecycleState.DESTROYED); } diff --git a/java/org/apache/catalina/util/LocalStrings.properties b/java/org/apache/catalina/util/LocalStrings.properties index b61f7cf1b..eabaac37e 100644 --- a/java/org/apache/catalina/util/LocalStrings.properties +++ b/java/org/apache/catalina/util/LocalStrings.properties @@ -22,6 +22,7 @@ extensionValidator.web-application-manifest=Web Application Manifest extensionValidator.extension-not-found-error=ExtensionValidator[{0}][{1}]: Required extension [{2}] not found. extensionValidator.extension-validation-error=ExtensionValidator[{0}]: Failure to find [{1}] required extension(s). extensionValidator.failload=Failure loading extension [{0}] +lifecycleBase.initMBeanFail=Failed to register component [{0}] with MBean name [{1}] lifecycleBase.alreadyStarted=The start() method was called on component [{0}] after start() had already been called. The second call will be ignored. lifecycleBase.alreadyStopped=The stop() method was called on component [{0}] after stop() had already been called. The second call will be ignored. lifecycleBase.invalidTransition=An invalid Lifecycle transition was attempted ([{0}]) for component [{1}] in state [{2}] -- 2.11.0