start the cluster JMX impl
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 7 Jan 2008 21:25:23 +0000 (21:25 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 7 Jan 2008 21:25:23 +0000 (21:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@609784 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java [new file with mode: 0644]
java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java

diff --git a/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java b/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java
new file mode 100644 (file)
index 0000000..25dc051
--- /dev/null
@@ -0,0 +1,134 @@
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements.  See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License.  You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.apache.catalina.ha.jmx;\r
+\r
+import javax.management.DynamicMBean;\r
+import javax.management.MBeanServer;\r
+import javax.management.MBeanServerFactory;\r
+import javax.management.ObjectName;\r
+\r
+import org.apache.catalina.core.StandardEngine;\r
+import org.apache.catalina.core.StandardHost;\r
+import org.apache.catalina.ha.authenticator.ClusterSingleSignOn;\r
+import org.apache.catalina.ha.deploy.FarmWarDeployer;\r
+import org.apache.catalina.ha.session.DeltaManager;\r
+import org.apache.catalina.ha.tcp.SimpleTcpCluster;\r
+import org.apache.juli.logging.Log;\r
+import org.apache.juli.logging.LogFactory;\r
+import org.apache.tomcat.util.modeler.ManagedBean;\r
+import org.apache.tomcat.util.modeler.Registry;\r
+/**\r
+ * \r
+ * @author Filip Hanik\r
+ */\r
+public class ClusterJmxHelper {\r
+    \r
+    protected static Registry registry = Registry.getRegistry(null,null);\r
+    \r
+    protected static Log log = LogFactory.getLog(ClusterJmxHelper.class);\r
+    \r
+    protected static boolean jmxEnabled = true;\r
+    \r
+    protected static MBeanServer mbeanServer = null;\r
+    \r
+    public static Registry getRegistry() {\r
+        return registry;\r
+    }\r
+\r
+    public static MBeanServer getMBeanServer() throws Exception {\r
+        if (mbeanServer == null) {\r
+            if (MBeanServerFactory.findMBeanServer(null).size() > 0) {\r
+                mbeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);\r
+            } else {\r
+                mbeanServer = MBeanServerFactory.createMBeanServer();\r
+            }\r
+        }\r
+        return mbeanServer;\r
+    }\r
+    \r
+    protected static boolean initMetaData(Class clazz) {\r
+        try {\r
+            if (clazz==null) return false;\r
+            getRegistry().loadMetadata(clazz.getResourceAsStream("mbeans-descriptors.xml"));\r
+        }catch (Exception x) {\r
+            log.warn("Unable to load meta data for class:"+clazz.getName());\r
+            return false;\r
+        }\r
+        return true;\r
+    }\r
+    \r
+    public static DynamicMBean getManagedBean(Object object) throws Exception {\r
+        DynamicMBean mbean = null;\r
+        if (getRegistry() != null) {\r
+            ManagedBean managedBean = registry.findManagedBean(object.getClass().getName());\r
+            mbean = managedBean.createMBean(object);\r
+        }\r
+        return mbean;\r
+    }\r
+\r
+    \r
+    protected static void initDefaultCluster() {\r
+        initMetaData(SimpleTcpCluster.class);\r
+        initMetaData(DeltaManager.class);\r
+        initMetaData(FarmWarDeployer.class); //not functional yet\r
+        initMetaData(ClusterSingleSignOn.class); //not functional yet\r
+    }\r
+    \r
+    public static boolean registerDefaultCluster(SimpleTcpCluster cluster)  {\r
+        try {\r
+            initDefaultCluster();\r
+            ObjectName clusterName = getDefaultClusterName(cluster);\r
+            if (!getMBeanServer().isRegistered(clusterName)) {\r
+                getMBeanServer().registerMBean(getManagedBean(cluster), clusterName);\r
+            }\r
+            return true;\r
+        }catch ( Exception x ) {\r
+            log.warn("Unable to register default cluster implementation with JMX",x);\r
+            return false;\r
+        }\r
+    }\r
+\r
+    public static boolean unregisterDefaultCluster(SimpleTcpCluster cluster) {\r
+        try {\r
+            ObjectName clusterName = getDefaultClusterName(cluster);\r
+            if (getMBeanServer().isRegistered(clusterName)) {\r
+                getMBeanServer().unregisterMBean(clusterName);\r
+            }\r
+            return true;\r
+        }catch ( Exception x ) {\r
+            log.warn("Unable to unregister default cluster implementation with JMX",x);\r
+            return false;\r
+        }\r
+    }\r
+\r
+    private static ObjectName getDefaultClusterName(SimpleTcpCluster cluster) throws Exception {\r
+        String domain = getMBeanServer().getDefaultDomain();\r
+        String type = ":type=";\r
+        String clusterType= type+"Cluster";\r
+        if (cluster.getContainer() instanceof StandardHost) {\r
+            domain = ((StandardHost) cluster.getContainer()).getDomain();\r
+            clusterType += ",host=" + cluster.getContainer().getName();\r
+        } else {\r
+            if (cluster.getContainer() instanceof StandardEngine) {\r
+                domain = ((StandardEngine) cluster.getContainer()).getDomain();\r
+            }\r
+        }\r
+        ObjectName clusterName = new ObjectName(domain + clusterType);\r
+        return clusterName;\r
+    }\r
+    \r
+}
\ No newline at end of file
index 95a9f0c..eca8863 100644 (file)
@@ -57,6 +57,7 @@ import org.apache.catalina.tribes.group.interceptors.MessageDispatch15Intercepto
 import org.apache.catalina.tribes.group.interceptors.TcpFailureDetector;
 import org.apache.catalina.ha.session.JvmRouteBinderValve;
 import org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener;
+import org.apache.catalina.ha.jmx.ClusterJmxHelper;
 
 /**
  * A <b>Cluster </b> implementation using simple multicast. Responsible for
@@ -417,7 +418,13 @@ public class SimpleTcpCluster
      * @param value
      */
     public boolean setProperty(String name, Object value) {
+        if (log.isTraceEnabled())
+            log.trace(sm.getString("SimpleTcpCluster.setProperty", name, value,properties.get(name)));
         properties.put(name, value);
+        //using a dynamic way of setting properties is nice, but a security risk
+        //if exposed through JMX. This way you can sit and try to guess property names,
+        //we will only allow explicit property names
+        log.warn("Dynamic setProperty("+name+",value) has been disabled, please use explicit properties for the element you are trying to identify");
         return false;
     }
 
@@ -669,6 +676,8 @@ public class SimpleTcpCluster
             channel.start(channel.DEFAULT);
             if (clusterDeployer != null) clusterDeployer.start();
             this.started = true;
+            //register JMX objects
+            ClusterJmxHelper.registerDefaultCluster(this);
             // Notify our interested LifecycleListeners
             lifecycle.fireLifecycleEvent(AFTER_START_EVENT, this);
         } catch (Exception x) {
@@ -764,6 +773,9 @@ public class SimpleTcpCluster
             channel.removeChannelListener(this);
             channel.removeMembershipListener(this);
             this.unregisterClusterValve();
+            //unregister JMX objects
+            ClusterJmxHelper.unregisterDefaultCluster(this);
+
         } catch (Exception x) {
             log.error("Unable to stop cluster valve.", x);
         }