[jboss-cvs] JBossCache/src-50/org/jboss/cache/pojo/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-50/org/jboss/cache/pojo/jmx  
                        PojoCacheJmxWrapperMBean.java
                        PojoCacheJmxWrapper.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-50/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapperMBean.java
  
  Index: PojoCacheJmxWrapperMBean.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.pojo.jmx;
  
  import javax.management.MalformedObjectNameException;
  
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.jmx.LifeCycle;
  import org.jboss.cache.pojo.PojoCache;
  
  /**
   * StandardMBean interface for {@link PojoCacheJmxWrapperMBean}.
   * 
   * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public interface PojoCacheJmxWrapperMBean 
     extends PojoCacheMBean
  {
     
     /**
      * Get the PojoCache.
      * 
      * @return
      */
     public PojoCache getPojoCache();
     
     public String getPojoCacheObjectName();
     
     public void setPojoCacheObjectName(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-50/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java
  
  Index: PojoCacheJmxWrapper.java
  ===================================================================
  package org.jboss.cache.pojo.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.config.Configuration;
  import org.jboss.cache.config.ConfigurationException;
  import org.jboss.cache.jmx.CacheJmxWrapper;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.impl.PojoCacheImpl;
  
  public class PojoCacheJmxWrapper
     implements PojoCacheJmxWrapperMBean, MBeanRegistration
  {
     private Log log = LogFactory.getLog(getClass().getName());
     
     private boolean registerInterceptors = true;
     private Configuration config;
     private MBeanServer server;
     private String cacheObjectName;
     private boolean selfRegister;
     private PojoCacheImpl cache_;
     private CacheJmxWrapper plainCacheWrapper_;
  
     
     // PojoCacheMBean
     
     public PojoCache getPojoCache()
     {
        return cache_;
     }
     
     public String getInternalLocation(Object pojo) throws PojoCacheAlreadyDetachedException
     {
        return null;
     }
     
     public String getPojoCacheObjectName()
     {
        if (cacheObjectName == null)
        {
           cacheObjectName = JmxUtil.getPojoCacheObjectName(cache_);
        }
        return cacheObjectName;
     }
     
     public void setPojoCacheObjectName(String name) throws MalformedObjectNameException
     {
        if (name != null)
        {
           // test the name
           new ObjectName(name);
        }
        this.cacheObjectName = name;
     }
  
     public String getUnderlyingCacheObjectName()
     {
        return plainCacheWrapper_ == null ? null : plainCacheWrapper_.getCacheObjectName();
     }
  
     public void create() throws Exception
     {
        if (cache_ == null)
           constructCache();
        
        cache_.create();
        
        if (server != null)
        {
           registerMBeans();
        }
     }
  
     public void start() throws Exception
     {
        cache_.start();
     }
  
     public void stop()
     {
        cache_.stop();
     }
  
     public void destroy()
     {
        cache_.destroy();
        
        if (server != null)
        {
           unregisterMBeans();
        }
     }
  
     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;
     }
     
     // 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
              getPojoCacheObjectName();
           }
           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);
     }
     
     // Private methods
     
     private void constructCache() throws ConfigurationException
     {
        Configuration config = getConfiguration();
        if (config != null)
        {
           config.setUseMbean(false);
        }
        else
        {
           throw new ConfigurationException("Must call setConfiguration() before call to create()");
        }
        
        cache_ = (PojoCacheImpl) PojoCacheFactory.createCache(config, false);
        
        if (plainCacheWrapper_ == null)
        {
           plainCacheWrapper_ = new CacheJmxWrapper();
           plainCacheWrapper_.setConfiguration(config);
        }
        
        plainCacheWrapper_.setRegisterInterceptors(getRegisterInterceptors());
        plainCacheWrapper_.setMbeanServer(server);
        plainCacheWrapper_.setUnderlyingCache(cache_.getCache());
     }
  
     private void registerMBeans() throws Exception
     {
        if (selfRegister)
           JmxUtil.registerPojoCache(server, this, getPojoCacheObjectName());
        
        if (config.getServiceName() == null || config.getServiceName().length() == 0)
        {
           // We need to inject the object name into the plain cache
           String plainName = JmxUtil.getPlainCacheObjectName(getPojoCacheObjectName());
           plainCacheWrapper_.setCacheObjectName(plainName);
        }
        plainCacheWrapper_.registerMBeans();
     }
  
     private void unregisterMBeans()
     {
        try
        {
           if (selfRegister)
              JmxUtil.unregisterPojoCache(server, getPojoCacheObjectName());
        }
        catch (Exception e)
        {
           log.error("Exception unregistering pojo cache" ,e);
        }
        
        plainCacheWrapper_.unregisterMBeans();
     }   
     
  }
  
  
  



More information about the jboss-cvs-commits mailing list