Add MBean registration and de-registration to LifecycleBase
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 2 May 2010 17:34:33 +0000 (17:34 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 2 May 2010 17:34:33 +0000 (17:34 +0000)
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

java/org/apache/catalina/LifecycleMBeanRegistration.java [new file with mode: 0644]
java/org/apache/catalina/util/LifecycleBase.java
java/org/apache/catalina/util/LocalStrings.properties

diff --git a/java/org/apache/catalina/LifecycleMBeanRegistration.java b/java/org/apache/catalina/LifecycleMBeanRegistration.java
new file mode 100644 (file)
index 0000000..a505d36
--- /dev/null
@@ -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();
+
+}
index a519713..daf4554 100644 (file)
 
 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);
     }
     
index b61f7cf..eabaac3 100644 (file)
@@ -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}]