cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From giac...@apache.org
Subject svn commit: r367505 - in /cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon: components/flow/ core/container/handler/ util/
Date Tue, 10 Jan 2006 06:27:10 GMT
Author: giacomo
Date: Mon Jan  9 22:27:02 2006
New Revision: 367505

URL: http://svn.apache.org/viewcvs?rev=367505&view=rev
Log:
added missing classes

Added:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImplMBean.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/PoolableComponentHandlerMBean.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/SingleThreadedComponentHandlerMBean.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/util/JMXUtils.java

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImplMBean.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImplMBean.java?rev=367505&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImplMBean.java
(added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/flow/ContinuationsManagerImplMBean.java
Mon Jan  9 22:27:02 2006
@@ -0,0 +1,97 @@
+/* 
+ * Copyright 2002-2005 The Apache Software Foundation
+ * Licensed  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.cocoon.components.flow;
+
+
+import org.apache.cocoon.util.jmx.ModelMBeanImpl;
+
+import java.util.Date;
+import java.util.Iterator;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+
+/**
+ * The ContinuationsManagerImplMBean adds JMX managability for ContinuationsManagerImpl.
+ *
+ * @version $Id: ThreadSafeComponentHandler.java 312637 2005-10-10 13:00:42Z cziegeler $
+ * @since 2.2
+ */
+public class ContinuationsManagerImplMBean
+extends ModelMBeanImpl {
+    
+    private final ContinuationsManagerImpl manager;
+    
+    protected void defineManagedResource() {
+        super.defineManagedResource();
+        defineAttribute("defaultTimeToLive", false, true);
+        defineAttribute("bindContinuationsToSession", false, true);
+        defineAttribute("expirationCheckInterval", false, true);
+        defineAttribute("expirationSet", false, true);
+    }
+    /**
+     * Construction of PoolableComponentHandlerMBean
+     *
+     * @param manager The managed ContinuationsManager instance
+     */
+    public ContinuationsManagerImplMBean(final ContinuationsManagerImpl manager)
+        throws MBeanException, InstanceNotFoundException {
+        super( manager );
+        this.manager = manager;
+    }
+    
+    public int getDefaultTimeToLive() {
+        return manager.defaultTimeToLive;
+    }
+    
+    public void setDefaultTimeToLive(final int ttl) {
+        manager.defaultTimeToLive = ttl;
+    }
+    
+    public boolean getBindContinuationsToSession() {
+        return manager.bindContinuationsToSession;
+    }
+    
+    public long getExpirationCheckInterval() {
+        return manager.expirationCheckInterval;
+    }
+    
+    public String[] getExpirationSet() {
+        final String [] lines = new String[ manager.expirations.size()];
+        int idx = 0;
+        for(final Iterator i = manager.expirations.iterator(); i.hasNext(); ) {
+            final StringBuffer wkSet = new StringBuffer();
+            final WebContinuation wk = (WebContinuation) i.next();
+            final long lat = wk.getLastAccessTime() + wk.getTimeToLive();
+            wkSet.append("WK: ")
+                    .append(wk.getId())
+                    .append(" ExpireTime [");
+
+            if (lat < System.currentTimeMillis()) {
+                wkSet.append("Expired");
+            } else {
+                final Date date = new Date();
+                date.setTime(lat);
+                wkSet.append(date);
+            }
+            wkSet.append("]");
+            lines[idx++] = wkSet.toString();
+        }
+        return lines;
+    }
+    
+}

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/PoolableComponentHandlerMBean.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/PoolableComponentHandlerMBean.java?rev=367505&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/PoolableComponentHandlerMBean.java
(added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/PoolableComponentHandlerMBean.java
Mon Jan  9 22:27:02 2006
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation
+ * Licensed  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.cocoon.core.container.handler;
+
+import org.apache.cocoon.components.ComponentInfo;
+
+
+import org.apache.cocoon.util.jmx.ModelMBeanImpl;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+
+/**
+ * The PoolableComponentHandlerMBean adds JMX managability for PoolableComponentHandler.
+ *
+ * @since 2.2
+ */
+public class PoolableComponentHandlerMBean
+    extends ModelMBeanImpl {
+    //~ Static fields/initializers ----------------------------------------------------------------------
+
+    /** The JMX ObjectName prefix used */
+    public static final String JMX_OBJECTNAME_PREFIX = "subsys=ECM++,handler=poolable";
+
+    /** JMX Attribute names */
+    public static final String JMX_ATTR_INTERFACES = "interfaces";
+    public static final String JMX_ATTR_DEFAULT_MAX_POOL_SIZE = "defaultMaxPoolSize";
+    public static final String JMX_ATTR_MAX_POOL_SIZE = "maxPoolSize";
+    public static final String JMX_ATTR_READY_POOL_SIZE = "readyPoolSize";
+    public static final String JMX_ATTR_TOTAL_POOL_SIZE = "totalPoolSize";
+    public static final String JMX_ATTR_HIGH_WATER_MARK = "highWaterMark";
+
+    /** The bean observing this MBeans highWaterMark attribute */
+    private static PoolableComponentHandlerObserver s_poolMonitor;
+
+    //~ Instance fields ---------------------------------------------------------------------------------
+
+    /** The ComponentInfo the component the handler manages */
+    private final ComponentInfo info;
+
+    /** The handler to manage */
+    private final PoolableComponentHandler handler;
+
+    //~ Constructors ------------------------------------------------------------------------------------
+
+    /**
+     * Construction of PoolableComponentHandlerMBean
+     *
+     * @param handler The managed PoolableComponentHandler instance
+     * @param info The ComponentInfo
+     */
+    public PoolableComponentHandlerMBean(final PoolableComponentHandler handler,
+                                         final ComponentInfo info) {
+        super(handler);
+        this.handler = handler;
+        this.info = info;
+    }
+
+    //~ Methods -----------------------------------------------------------------------------------------
+
+    /**
+     * A JMX Attribute
+     *
+     * @return the default max pool size
+     */
+    public int getDefaultMaxPoolSize() {
+        return NonThreadSafePoolableComponentHandler.DEFAULT_MAX_POOL_SIZE;
+    }
+
+    /**
+     * A JMX Attriute
+     *
+     * @return the pool size high water mark
+     */
+    public int getHighWaterMark() {
+        return handler.getHighWaterMark();
+    }
+
+    /**
+     * A JMX Attriute
+     *
+     * @return the array of implemented interfaces of the component managed by the handler
+     */
+    public String[] getInterfaces() {
+        final String[] ifaces = new String[this.handler.getInterfaces().length];
+
+        for(int i = 0; i < ifaces.length; i++) {
+            ifaces[i] = this.handler.getInterfaces()[i].getName();
+        }
+
+        return ifaces;
+    }
+
+    /**
+     * Give this MBean a JMX name
+     *
+     * @return the JMX name to use for this MBean
+     */
+    public String getJmxName() {
+        //return JMXUtils.genDefaultJmxName(handler.getInfo().getServiceClassName());
+        return JMX_OBJECTNAME_PREFIX + ((info.getRole() != null) ? (",role=" + info.getRole())
: "");
+    }
+
+    /**
+     * A JMX Attriute
+     *
+     * @return the maximum pool size
+     */
+    public int getMaxPoolSize() {
+        return handler.getMax();
+    }
+
+    /**
+     * A JMX Attriute
+     *
+     * @return how many components are ready in the pool
+     */
+    public int getReadyPoolSize() {
+        return handler.getReadySize();
+    }
+
+    /**
+     * A JMX Attriute
+     *
+     * @return current size of the pool
+     */
+    public int getTotalPoolSize() {
+        return handler.getSize();
+    }
+
+    /**
+     * Checks whether the PoolableComponentHandlerPoolObserver MBean is registered and passes
it the
+     * handler currently  set under JMX for observing the high water mark attribute.
+     *
+     * @see org.apache.cocoon.util.jmx.ModelMBeanImpl#preRegister(javax.management.MBeanServer,
+     *      javax.management.ObjectName)
+     */
+    public synchronized ObjectName preRegister(final MBeanServer server,
+                                               final ObjectName oName) {
+        final ObjectName objectName = super.preRegister(server, oName);
+
+        try {
+            final ObjectName monitorName =
+                new ObjectName(objectName.getDomain() + ":" + JMX_OBJECTNAME_PREFIX + ","
+
+                               PoolableComponentHandlerObserver.JMX_OBJECTNAME_SUFFIX);
+
+            if(! server.isRegistered(monitorName)) {
+                // Now let's register the PoolMonitor
+                // We would like to know if we have excessive object creation not served
from the pool
+                s_poolMonitor = new PoolableComponentHandlerObserver();
+                server.registerMBean(s_poolMonitor, monitorName);
+            }
+
+            // Setup the monitor: link to the service MBean
+            s_poolMonitor.addPoolHandler(handler, objectName);
+        } catch(final Exception e) {
+            System.out.println("Ignored Exception");
+            e.printStackTrace();
+        }
+
+        return objectName;
+    }
+
+    /**
+     * Define the manages resources
+     */
+    protected void defineManagedResource() {
+        super.defineManagedResource();
+        defineAttribute(JMX_ATTR_DEFAULT_MAX_POOL_SIZE, false, true);
+        defineAttribute(JMX_ATTR_HIGH_WATER_MARK, false, true);
+        defineAttribute(JMX_ATTR_INTERFACES, false, true);
+        defineAttribute(JMX_ATTR_MAX_POOL_SIZE, false, true);
+        defineAttribute(JMX_ATTR_READY_POOL_SIZE, false, true);
+        defineAttribute(JMX_ATTR_TOTAL_POOL_SIZE, false, true);
+    }
+}

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/SingleThreadedComponentHandlerMBean.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/SingleThreadedComponentHandlerMBean.java?rev=367505&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/SingleThreadedComponentHandlerMBean.java
(added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/core/container/handler/SingleThreadedComponentHandlerMBean.java
Mon Jan  9 22:27:02 2006
@@ -0,0 +1,75 @@
+/* 
+ * Copyright 2002-2005 The Apache Software Foundation
+ * Licensed  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.cocoon.core.container.handler;
+
+
+import org.apache.cocoon.components.ComponentInfo;
+import org.apache.cocoon.util.jmx.ModelMBeanImpl;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+
+/**
+ * The SingleThreadedComponentHandlerMBean adds JMX managability for SingleThreadedComponentHandler.
+ *
+ * @version $Id: ThreadSafeComponentHandler.java 312637 2005-10-10 13:00:42Z cziegeler $
+ * @since 2.2
+ */
+public class SingleThreadedComponentHandlerMBean
+extends ModelMBeanImpl {
+    
+    private final SingleThreadedComponentHandler handler;
+    private final ComponentInfo info;
+    
+    protected void defineManagedResource() {
+        super.defineManagedResource();
+        defineAttribute("maxCreated", false, true);
+        defineAttribute("maxDecommissioned", false, true);
+        defineAttribute("outstanding", false, true);
+    }
+    /**
+     * Construction of PoolableComponentHandlerMBean
+     *
+     * @param handler The managed PoolableComponentHandler instance
+     */
+    public SingleThreadedComponentHandlerMBean(final SingleThreadedComponentHandler handler,
final ComponentInfo info)
+        throws MBeanException, InstanceNotFoundException {
+        super( handler );
+        this.handler = handler;
+        this.info = info;
+    }
+
+    public long getMaxCreated()
+    {
+        return handler.getMaxCreated();
+    }
+
+    public long getMaxDecommissioned()
+    {
+        return handler.getMaxDecommissioned();
+    }
+
+    public long getOutstanding()
+    {
+        return handler.getMaxCreated() - handler.getMaxDecommissioned();
+    }
+    
+    public String getJmxName() 
+    {
+        return "subsys=ECM++,handler=single-threaded" + (info.getRole() != null ? ",role="
+ info.getRole() : "");
+    }
+}

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/util/JMXUtils.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/util/JMXUtils.java?rev=367505&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/util/JMXUtils.java (added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/util/JMXUtils.java Mon Jan  9
22:27:02 2006
@@ -0,0 +1,387 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation
+ * Licensed  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.cocoon.util;
+
+import org.apache.avalon.framework.logger.ConsoleLogger;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.components.ComponentInfo;
+
+import org.apache.cocoon.core.container.CoreServiceManager;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+
+
+/**
+ * Utility methods for JMX
+ *
+ * @version $Id$
+ * 
+ * @since 2.2
+ */
+public class JMXUtils {
+    //~ Static fields/initializers ----------------------------------------------------------------------
+
+    /** The {@link MBeanServer} first found */
+    private static MBeanServer mbeanServer = getInitialMBeanServer();
+
+    //~ Constructors ------------------------------------------------------------------------------------
+
+    /**
+     * Private c'tor: its a Utility class
+     */
+    private JMXUtils() {
+        super();
+    }
+
+    //~ Methods -----------------------------------------------------------------------------------------
+
+    /**
+     * Find a JMX domain name at various places
+     *
+     * @param pJmxDomain Default JMX domain name
+     * @param serviceManager The ServiceManager used
+     *
+     * @return The JMX domain name
+     */
+    public static String findJmxDomain(final String pJmxDomain,
+                                       final ServiceManager serviceManager) {
+        // try to find a JMX domain name first from this component configuration give as
parameter
+        String jmxDomain = pJmxDomain;
+
+        if(jmxDomain == null) {
+            // next from the CoreServiceManager managing this component
+            if((serviceManager != null) && serviceManager instanceof CoreServiceManager)
{
+                // next from the CoreServiceManager managing this component
+                jmxDomain = ((CoreServiceManager)serviceManager).getJmxDefaultDomain();
+            } else {
+                // otherwise use default one
+                jmxDomain = CoreServiceManager.JMX_DEFAULT_DOMAIN_NAME;
+            }
+        }
+
+        return jmxDomain;
+    }
+
+    /**
+     * Find or construct a JMX object name at various places (excluding the JMX domain)
+     *
+     * @param pJmxName Default JMX object name
+     * @param pClassName Class name to use if JMX object name has to be constructed
+     *
+     * @return The JMX obejct name (excluding the JMX domain name
+     */
+    public static String findJmxName(final String pJmxName,
+                                     final String pClassName) {
+        String jmxName = pJmxName;
+        final String className = ((pClassName == null) ? "unknown" : pClassName);
+
+        if(jmxName == null) {
+            // otherwise construct one from the service class name
+            final StringBuffer sb = new StringBuffer();
+            final List groups = new ArrayList();
+            int i = className.indexOf('.');
+            int j = 0;
+
+            while(i > 0) {
+                groups.add(className.substring(j, i));
+                j = i + 1;
+                i = className.indexOf('.', i + 1);
+            }
+
+            groups.add(className.substring(j));
+
+            for(i = 0; i < (groups.size() - 1); i++) {
+                sb.append("group");
+
+                if(i > 0) {
+                    sb.append(i);
+                }
+
+                sb.append('=');
+                sb.append(groups.get(i));
+                sb.append(',');
+            }
+
+            sb.append("item=").append(groups.get(groups.size() - 1));
+            jmxName = sb.toString();
+        }
+
+        return jmxName;
+    }
+
+    /**
+     * generate a default JMX object name (excluding JMX domain name) from a FQCN
+     *
+     * @param clazz The class name
+     *
+     * @return The generated JMX object name (excluding JMX domain name)
+     */
+    public static String genDefaultJmxName(final Class clazz) {
+        return genDefaultJmxName(clazz.getName());
+    }
+
+    /**
+     * generate a default JMX object name (excluding JMX domain name) from a FQCN
+     *
+     * @param className The class name
+     *
+     * @return The generated JMX object name (excluding JMX domain name)
+     */
+    public static String genDefaultJmxName(final String className) {
+        final StringBuffer nameBuf = new StringBuffer();
+        final List groups = new ArrayList();
+        int i = className.indexOf('.');
+        int j = 0;
+
+        while(i > 0) {
+            groups.add(className.substring(j, i));
+            j = i + 1;
+            i = className.indexOf('.', i + 1);
+        }
+
+        groups.add(className.substring(j));
+
+        for(i = 0; i < (groups.size() - 1); i++) {
+            nameBuf.append("group");
+
+            if(i > 0) {
+                nameBuf.append(i);
+            }
+
+            nameBuf.append('=');
+            nameBuf.append(groups.get(i));
+            nameBuf.append(',');
+        }
+
+        nameBuf.append("item=").append(groups.get(groups.size() - 1));
+
+        return nameBuf.toString();
+    }
+
+    /**
+     * Get the ev. found {@link MBeanServer}
+     *
+     * @return DOCUMENT ME!
+     */
+    public static MBeanServer getMBeanServer() {
+        return JMXUtils.mbeanServer;
+    }
+
+    /**
+     * Setup a component for possible JMX managability
+     *
+     * @param bean The bean to estabilsh JMX for
+     * @param info The component info
+     *
+     * @return DOCUMENT ME!
+     */
+    public static ObjectInstance setupJmxFor(final Object bean,
+                                             final ComponentInfo info) {
+        return setupJmxFor(bean, info, new ConsoleLogger(ConsoleLogger.LEVEL_INFO));
+    }
+
+    /**
+     * Setup a component for possible JMX managability
+     *
+     * @param bean The bean to estabilsh JMX for
+     * @param info The component info
+     * @param logger The Logger
+     */
+    public static ObjectInstance setupJmxFor(final Object bean,
+                                             final ComponentInfo info,
+                                             final Logger logger) {
+        if(getMBeanServer() != null) {
+            final Class beanClass = bean.getClass();            
+            final String packageName = beanClass.getPackage().getName();
+            String cName = beanClass.getName().substring(packageName.length()+1);
+            String mName=packageName+"."+cName+"MBean";
+            final ObjectInstance oi = setupJmxFor(mName, bean, info, logger);
+            if(oi == null) {            
+                mName = packageName+".jmx."+cName+"MBean";
+                return setupJmxFor(mName, bean, info, logger);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Setup a component for possible JMX managability
+     *
+     * @param mbeanClassName The class name to look after as MBean
+     * @param bean The bean to estabilsh JMX for
+     * @param info The component info
+     * @param logger The Logger
+     */
+    private static ObjectInstance setupJmxFor(final String mbeanClassName,
+                                             final Object bean,
+                                             final ComponentInfo info,
+                                             final Logger logger) {
+        final Class beanClass = bean.getClass();
+        ObjectName on = null;
+
+        try {
+            // try to find a MBean for bean
+            final Class mbeanClass = beanClass.getClassLoader().loadClass(mbeanClassName);
+            Constructor ctor = null;
+            Object mbean = null;
+
+            try {
+                ctor = mbeanClass.getConstructor(new Class[] {beanClass, ComponentInfo.class});
+                mbean = ctor.newInstance(new Object[] {bean, info});
+            } catch(final Exception e) {
+                // ignore this
+            }
+
+            if(ctor == null) {
+                ctor = mbeanClass.getConstructor(new Class[] {beanClass});
+                mbean = ctor.newInstance(new Object[] {bean});
+            }
+
+            // see if MBean supplies some JMX ObjectName parts
+            final String mBeanSuppliedJmxDomain = callGetter(mbean, "getJmxDomain", logger);
+            final String mBeanSuppliedJmxName = callGetter(mbean, "getJmxName", logger);
+            final String mBeanSuppliedJmxNameAdditions =
+                callGetter(mbean, "getJmxNameAddition", logger);
+
+            // construct a JMX ObjectName instance
+            final StringBuffer objectNameBuf = new StringBuffer();
+
+            if(mBeanSuppliedJmxDomain != null) {
+                objectNameBuf.append(mBeanSuppliedJmxDomain);
+            } else {
+                objectNameBuf.append(info.getJmxDomain());
+            }
+
+            objectNameBuf.append(':');
+
+            if(mBeanSuppliedJmxName != null) {
+                objectNameBuf.append(mBeanSuppliedJmxName);
+            } else if(info.getConfiguration()
+                          .getAttribute(CoreServiceManager.JMX_NAME_ATTR_NAME, null) != null)
{
+                objectNameBuf.append(info.getConfiguration()
+                                         .getAttribute(CoreServiceManager.JMX_NAME_ATTR_NAME,
null));
+            } else {
+                // if we do not have the name parts we'll construct one from the bean class
name           
+                objectNameBuf.append(genDefaultJmxName(beanClass));
+            }
+
+            if(mBeanSuppliedJmxNameAdditions != null) {
+                objectNameBuf.append(',');
+                objectNameBuf.append(mBeanSuppliedJmxNameAdditions);
+            }
+
+            on = new ObjectName(objectNameBuf.toString());
+
+            int instance = 1;
+
+            while(mbeanServer.isRegistered(on)) {
+                instance++;
+                on = new ObjectName(objectNameBuf.toString() + ",instance=" + instance);
+            }
+
+            return mbeanServer.registerMBean(mbean, on);
+        } catch(final ClassNotFoundException cnfe) {
+            // happens if a component doesn't have a MBean to support it for management
+            if(logger.isDebugEnabled()) {
+                logger.debug("Class " + beanClass.getName() +
+                             " doesn't have a supporting MBean called " + mbeanClassName);
+            }
+        } catch(final NoSuchMethodException nsme) {
+            logger.warn("MBean " + mbeanClassName +
+                        " doesn't have a constructor that accepts an instance of " +
+                        info.getServiceClassName(), nsme);
+        } catch(final InvocationTargetException ite) {
+            logger.warn("Cannot invoke constructor on class " + mbeanClassName, ite);
+        } catch(final InstantiationException ie) {
+            logger.warn("Cannot instantiate class " + mbeanClassName, ie);
+        } catch(final IllegalAccessException iae) {
+            logger.warn("Cannot access class " + mbeanClassName, iae);
+        } catch(final MalformedObjectNameException mone) {
+            logger.warn("Invalid ObjectName '" + on + "' for MBean " + mbeanClassName, mone);
+        } catch(final InstanceAlreadyExistsException iaee) {
+            logger.warn("Instance for MBean " + mbeanClassName + "already exists", iaee);
+        } catch(final NotCompliantMBeanException ncme) {
+            logger.warn("Not compliant MBean " + mbeanClassName, ncme);
+        } catch(final MBeanRegistrationException mre) {
+            logger.warn("Cannot register MBean " + mbeanClassName, mre);
+        } catch(final SecurityException se) {
+            logger.warn("Instantiation of MBean " + mbeanClassName +
+                        " is prevented by a SecurityManager", se);
+        }
+
+        return null;
+    }
+
+    /**
+     * Call a getter method on a bean
+     *
+     * @param mbean The bean to call a method from
+     * @param name The name of the method to call
+     * @param logger Logger for diagnostic messages
+     *
+     * @return DOCUMENT ME!
+     */
+    private static String callGetter(final Object mbean,
+                                     final String name,
+                                     final Logger logger) {
+        final Method[] methods = mbean.getClass().getMethods();
+
+        for(int i = 0; i < methods.length; i++) {
+            if(methods[i].getName().equals(name)) {
+                try {
+                    return methods[i].invoke(mbean, null).toString();
+                } catch(final Exception e) {
+                    logger.warn("Method '" + name + "' cannot be accessed on MBean " +
+                                mbean.getClass().getName());
+
+                    return null;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Try to find a JMX Agent (MBeanServer)
+     *
+     * @return The first MBeanServer found or null
+     */
+    private static MBeanServer getInitialMBeanServer() {
+        final List servers = MBeanServerFactory.findMBeanServer(null);
+
+        if(servers.size() > 0) {
+            return (MBeanServer)servers.get(0);
+        }
+
+        return null;
+    }
+}



Mime
View raw message