You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2008/01/07 22:25:24 UTC

svn commit: r609784 - in /tomcat/trunk/java/org/apache/catalina/ha: jmx/ jmx/ClusterJmxHelper.java tcp/SimpleTcpCluster.java

Author: fhanik
Date: Mon Jan  7 13:25:23 2008
New Revision: 609784

URL: http://svn.apache.org/viewvc?rev=609784&view=rev
Log:
start the cluster JMX impl

Added:
    tomcat/trunk/java/org/apache/catalina/ha/jmx/
    tomcat/trunk/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java
Modified:
    tomcat/trunk/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java

Added: tomcat/trunk/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java?rev=609784&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java (added)
+++ tomcat/trunk/java/org/apache/catalina/ha/jmx/ClusterJmxHelper.java Mon Jan  7 13:25:23 2008
@@ -0,0 +1,134 @@
+/*
+ * 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.ha.jmx;
+
+import javax.management.DynamicMBean;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+
+import org.apache.catalina.core.StandardEngine;
+import org.apache.catalina.core.StandardHost;
+import org.apache.catalina.ha.authenticator.ClusterSingleSignOn;
+import org.apache.catalina.ha.deploy.FarmWarDeployer;
+import org.apache.catalina.ha.session.DeltaManager;
+import org.apache.catalina.ha.tcp.SimpleTcpCluster;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.modeler.ManagedBean;
+import org.apache.tomcat.util.modeler.Registry;
+/**
+ * 
+ * @author Filip Hanik
+ */
+public class ClusterJmxHelper {
+    
+    protected static Registry registry = Registry.getRegistry(null,null);
+    
+    protected static Log log = LogFactory.getLog(ClusterJmxHelper.class);
+    
+    protected static boolean jmxEnabled = true;
+    
+    protected static MBeanServer mbeanServer = null;
+    
+    public static Registry getRegistry() {
+        return registry;
+    }
+
+    public static MBeanServer getMBeanServer() throws Exception {
+        if (mbeanServer == null) {
+            if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
+                mbeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
+            } else {
+                mbeanServer = MBeanServerFactory.createMBeanServer();
+            }
+        }
+        return mbeanServer;
+    }
+    
+    protected static boolean initMetaData(Class clazz) {
+        try {
+            if (clazz==null) return false;
+            getRegistry().loadMetadata(clazz.getResourceAsStream("mbeans-descriptors.xml"));
+        }catch (Exception x) {
+            log.warn("Unable to load meta data for class:"+clazz.getName());
+            return false;
+        }
+        return true;
+    }
+    
+    public static DynamicMBean getManagedBean(Object object) throws Exception {
+        DynamicMBean mbean = null;
+        if (getRegistry() != null) {
+            ManagedBean managedBean = registry.findManagedBean(object.getClass().getName());
+            mbean = managedBean.createMBean(object);
+        }
+        return mbean;
+    }
+
+    
+    protected static void initDefaultCluster() {
+        initMetaData(SimpleTcpCluster.class);
+        initMetaData(DeltaManager.class);
+        initMetaData(FarmWarDeployer.class); //not functional yet
+        initMetaData(ClusterSingleSignOn.class); //not functional yet
+    }
+    
+    public static boolean registerDefaultCluster(SimpleTcpCluster cluster)  {
+        try {
+            initDefaultCluster();
+            ObjectName clusterName = getDefaultClusterName(cluster);
+            if (!getMBeanServer().isRegistered(clusterName)) {
+                getMBeanServer().registerMBean(getManagedBean(cluster), clusterName);
+            }
+            return true;
+        }catch ( Exception x ) {
+            log.warn("Unable to register default cluster implementation with JMX",x);
+            return false;
+        }
+    }
+
+    public static boolean unregisterDefaultCluster(SimpleTcpCluster cluster) {
+        try {
+            ObjectName clusterName = getDefaultClusterName(cluster);
+            if (getMBeanServer().isRegistered(clusterName)) {
+                getMBeanServer().unregisterMBean(clusterName);
+            }
+            return true;
+        }catch ( Exception x ) {
+            log.warn("Unable to unregister default cluster implementation with JMX",x);
+            return false;
+        }
+    }
+
+    private static ObjectName getDefaultClusterName(SimpleTcpCluster cluster) throws Exception {
+        String domain = getMBeanServer().getDefaultDomain();
+        String type = ":type=";
+        String clusterType= type+"Cluster";
+        if (cluster.getContainer() instanceof StandardHost) {
+            domain = ((StandardHost) cluster.getContainer()).getDomain();
+            clusterType += ",host=" + cluster.getContainer().getName();
+        } else {
+            if (cluster.getContainer() instanceof StandardEngine) {
+                domain = ((StandardEngine) cluster.getContainer()).getDomain();
+            }
+        }
+        ObjectName clusterName = new ObjectName(domain + clusterType);
+        return clusterName;
+    }
+    
+}
\ No newline at end of file

Modified: tomcat/trunk/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java?rev=609784&r1=609783&r2=609784&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java (original)
+++ tomcat/trunk/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java Mon Jan  7 13:25:23 2008
@@ -57,6 +57,7 @@
 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 @@
      * @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 @@
             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 @@
             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);
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org