[jboss-cvs] JBossCache/src/org/jboss/cache/jmx ...

Brian Stansberry brian.stansberry at jboss.com
Mon Nov 6 00:01:42 EST 2006


  User: bstansberry
  Date: 06/11/06 00:01:42

  Added:       src/org/jboss/cache/jmx   CacheJmxWrapperMBean.java
                        CacheJmxWrapper.java
  Log:
  Add wrapper classes to support removing JMX registration from core JBC/PojoCache code
  
  Revision  Changes    Path
  1.1      date: 2006/11/06 05:01:42;  author: bstansberry;  state: Exp;JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapperMBean.java
  
  Index: CacheJmxWrapperMBean.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.cache.jmx;
  
  import javax.management.MBeanServer;
  import javax.management.MalformedObjectNameException;
  import javax.management.ObjectName;
  
  import org.jboss.cache.config.Configuration;
  
  /**
   * StandardMBean interface for {@link CacheJmxWrapper}.
   * 
   * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public interface CacheJmxWrapperMBean extends CacheMBean
  {   
     public String getCacheObjectName();
     
     public void setCacheObjectName(String name) throws MalformedObjectNameException;
     
     /**
      * Ensures the underlying cache is initialized and that needed JMX registrations
      * are done, and then calls {@link org.jboss.cache.Cache#create() create()} on the
      * underlying cache.
      * <p/>
      * If the underlying cache has not been instantiated, instantiates it.  Then,
      * if an MBeanServer has been passed to {@link #setMbeanServer(MBeanServer)},
      * this object registers itself with that server.  Then, if an MBeanServer is
      * available (either through <code>setMbeanServer</code> or 
      * {@link #preRegister(MBeanServer, ObjectName)}) and property 
      * <code>registerInterceptors</code> is <code>true</code>, registers the 
      * cache's interceptors with JMX.
      */
     void create() throws Exception;
     
     /**
      * Sets the configuration that the underlying cache should use.
      * 
      * @param config the configuration
      * 
      * @throws IllegalArgumentException if <code>config</code> is <code>null</code>.
      */
     void setConfiguration(Configuration config);
     
     /**
      * Gets whether this object should register the cache's interceptors
      * with JMX during {@link LifeCycle#create() create()}.
      */
     boolean getRegisterInterceptors();
     
     /**
      * Sets whether this object should register the cache's interceptors
      * with JMX during {@link LifeCycle#create() create()}.
      */
     void setRegisterInterceptors(boolean register);
  }
  
  
  
  1.1      date: 2006/11/06 05:01:42;  author: bstansberry;  state: Exp;JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java
  
  Index: CacheJmxWrapper.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.cache.jmx;
  
  import javax.management.MBeanRegistration;
  import javax.management.MBeanServer;
  import javax.management.MalformedObjectNameException;
  import javax.management.ObjectName;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Cache;
  import org.jboss.cache.TreeCacheProxyImpl;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.config.ConfigurationException;
  import org.jboss.cache.factories.DefaultCacheFactory;
  
  public class CacheJmxWrapper implements CacheJmxWrapperMBean, MBeanRegistration
  {
     private Log log = LogFactory.getLog(getClass().getName());
     
     private boolean registerInterceptors = true;
     private TreeCacheProxyImpl tcpi;
     private Configuration config;
     private MBeanServer server;
     private String cacheObjectName;
     private boolean selfRegister = true;
     private boolean created;
     
     // CacheJMXWrapperMBean impl
  
     public Configuration getConfiguration()
     {
        return config;
     }
  
     public void setConfiguration(Configuration config)
     {
        if (config == null)
           throw new IllegalArgumentException("config cannot be null");
        
        this.config = config;
     }
     
     public boolean getRegisterInterceptors()
     {
        return registerInterceptors;
     }
  
     public void setRegisterInterceptors(boolean register)
     {
        this.registerInterceptors = register;
     }
  
     public Cache getCache()
     {      
        if (tcpi == null)
        {
           if (config == null)
              return null;
           else
              constructCache();
        }
        
        return tcpi;
     }
  
     public String printCacheDetails()
     {
        return tcpi == null ? null : tcpi.treeCache.printDetails();
     }
  
     public void create() throws Exception
     {
        if (tcpi == null)
           constructCache();
        
        tcpi.create();
        
        if (server != null)
        {
           registerMBeans();
        }
        
        created = true;
     }
  
     public void start() throws Exception
     {
        tcpi.start();
     }
  
     public void stop()
     {
        tcpi.stop();
     }
  
     public void destroy()
     {
        tcpi.destroy();
        
        if (server != null)
        {
           unregisterMBeans();
        }
        
        created = false;
     }
     
     // MBeanRegistration
  
     /**
      * No-op.
      */
     public void postDeregister()
     {
     }
  
     /**
      * No-op.
      */
     public void postRegister(Boolean arg0)
     {
     }
  
     /**
      * No-op.
      */
     public void preDeregister() throws Exception
     { 
     }
  
     public ObjectName preRegister(MBeanServer server, ObjectName objName) 
           throws Exception
     {
        this.server = server;
        selfRegister = false;
        
        if (cacheObjectName == null)
        {
           if (objName == null)
           {    
              // Calling this will create a value for cacheObjectName
              getCacheObjectName();
           }
           else
           {
              cacheObjectName = objName.getCanonicalName();
           }
        }
        return new ObjectName(cacheObjectName);
     }
     
     // Public methods
     
     /**
      * Provides a hook for dependency injecting the MBeanServer in environments where
      * this object will not be externally registered in JMX.  If this setter is called,
      * this object will register itself in JMX during the create() phase. 
      */
     public void setMbeanServer(MBeanServer server)
     {      
        this.server = server;
        selfRegister = (server != null);
     }
     
     /**
      * Allows direct injection of the underlying cache.
      * 
      * @param cache
      */
     public void setUnderlyingCache(Cache cache)
     {     
        if (created)
           throw new IllegalStateException("Cannot set underlying cache after call to create()");
        
        // FIXME -- the only reason we need to cast here is to support printCacheDetails 
        this.tcpi = (TreeCacheProxyImpl) cache;
     }
     
     public String getCacheObjectName()
     {
        if (cacheObjectName == null)
        {
           cacheObjectName = JmxUtil.getCacheObjectName(config, TreeCacheProxyImpl.class.getName());
        }
        return cacheObjectName;
     }
     
     public void setCacheObjectName(String name) throws MalformedObjectNameException
     {
        if (name != null)
        {
           // test the name
           new ObjectName(name);
        }
        this.cacheObjectName = name;
     }
     
     public void registerMBeans() throws Exception
     {
        String cacheObjectName = getCacheObjectName();
        
        if (selfRegister)
           JmxUtil.registerCacheMBean(server, this, cacheObjectName);
        
        if (registerInterceptors)
           JmxUtil.registerInterceptors(server, tcpi.getInterceptorChain(), cacheObjectName);
        
     }
     
     public void unregisterMBeans()
     {
        String cacheObjectName = getCacheObjectName();
     
        if (selfRegister)
        {
           try
           {
              JmxUtil.unregisterCacheMBean(server, cacheObjectName);
           }
           catch (Exception e)
           {
              log.error("Exception unregistering cache from JMX", e);
           }
        }
        
        if (registerInterceptors)
        {
           try
           {
              JmxUtil.unregisterInterceptors(server, tcpi.getInterceptorChain(), cacheObjectName);
           }
           catch (Exception e)
           {
              log.error("Exception unregistering interceptors from JMX", e);
           }
        }      
     }
     
     // Private methods
     
     private void constructCache() throws ConfigurationException
     {
        if (config != null)
        {
           config.setUseMbean(false);
        }
        else
        {
           throw new ConfigurationException("Must call setConfiguration() before call to create()");
        }
        
        setUnderlyingCache(DefaultCacheFactory.createCache(config, false));
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list