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

Brian Stansberry brian.stansberry at jboss.com
Thu Nov 9 21:25:03 EST 2006


  User: bstansberry
  Date: 06/11/09 21:25:03

  Modified:    src-50/org/jboss/cache/pojo/jmx  PojoCacheJmxWrapper.java
  Log:
  Refactor how things are registered in JMX
  Allow injection of PojoCache
  
  Revision  Changes    Path
  1.2       +159 -74   JBossCache/src-50/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: PojoCacheJmxWrapper.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src-50/org/jboss/cache/pojo/jmx/PojoCacheJmxWrapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- PojoCacheJmxWrapper.java	6 Nov 2006 05:01:42 -0000	1.1
  +++ PojoCacheJmxWrapper.java	10 Nov 2006 02:25:03 -0000	1.2
  @@ -10,6 +10,7 @@
   import org.jboss.cache.config.Configuration;
   import org.jboss.cache.config.ConfigurationException;
   import org.jboss.cache.jmx.CacheJmxWrapper;
  +import org.jboss.cache.jmx.CacheJmxWrapperMBean;
   import org.jboss.cache.pojo.PojoCache;
   import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
   import org.jboss.cache.pojo.PojoCacheFactory;
  @@ -25,39 +26,41 @@
      private MBeanServer server;
      private String cacheObjectName;
      private boolean selfRegister;
  -   private PojoCacheImpl cache_;
  +   private boolean selfConstructed;
  +   private PojoCache cache_;
      private CacheJmxWrapper plainCacheWrapper_;
  +   private boolean registerPlainCache;
  +   private boolean created;
   
  +   /**
  +    * Default constructor.
  +    *
  +    */
  +   public PojoCacheJmxWrapper()
  +   {
      
  -   // PojoCacheMBean
  +   }
      
  -   public PojoCache getPojoCache()
  +   public PojoCacheJmxWrapper(PojoCache toWrap)
      {
  -      return cache_;
  +      cache_ = toWrap;
      }
      
  -   public String getInternalLocation(Object pojo) throws PojoCacheAlreadyDetachedException
  +   // PojoCacheMBean
  +   
  +   public PojoCache getPojoCache()
      {
  -      return null;
  +      return cache_;
      }
      
  -   public String getPojoCacheObjectName()
  -   {
  -      if (cacheObjectName == null)
  +   public Configuration getConfiguration()
         {
  -         cacheObjectName = JmxUtil.getPojoCacheObjectName(cache_);
  -      }
  -      return cacheObjectName;
  +      return config;
      }
      
  -   public void setPojoCacheObjectName(String name) throws MalformedObjectNameException
  -   {
  -      if (name != null)
  +   public String getInternalLocation(Object pojo) throws PojoCacheAlreadyDetachedException
         {
  -         // test the name
  -         new ObjectName(name);
  -      }
  -      this.cacheObjectName = name;
  +      return null;
      }
   
      public String getUnderlyingCacheObjectName()
  @@ -70,45 +73,50 @@
         if (cache_ == null)
            constructCache();
         
  +      if (selfConstructed)
         cache_.create();
         
  -      if (server != null)
  +      if (selfRegister)
         {
  -         registerMBeans();
  +         registerWithMBeanServer();
         }
  +      
  +      created = true;
      }
   
      public void start() throws Exception
      {
  +      if (selfConstructed)
         cache_.start();
      }
   
      public void stop()
      {
  +      if (selfConstructed)
         cache_.stop();
      }
   
      public void destroy()
      {
  +      if (selfConstructed)
         cache_.destroy();
         
  -      if (server != null)
  +      if (selfRegister)
         {
  -         unregisterMBeans();
  +         unregisterWithMBeanServer();
         }
  +      
  +      created = false;
      }
   
  -   public Configuration getConfiguration()
  +   public boolean getRegisterPlainCache()
      {
  -      return config;
  +      return registerPlainCache;
      }
   
  -   public void setConfiguration(Configuration config)
  +   public void setRegisterPlainCache(boolean registerPlainCache)
      {
  -      if (config == null)
  -         throw new IllegalArgumentException("config cannot be null");
  -      
  -      this.config = config;
  +      this.registerPlainCache = registerPlainCache;
      }
      
      public boolean getRegisterInterceptors()
  @@ -121,20 +129,58 @@
         this.registerInterceptors = register;
      }
      
  -   // MBeanRegistration
  +   // -----------------------------------------------------------  MBeanRegistration
   
      /**
  -    * No-op.
  +    * Caches the provided <code>server</code> and <code>objName</code>.
  +    *  
  +    * @return either <code>objName</code>, or, if a name was previously assigned
  +    *         via {@link #setPojoCacheObjectName(String)}, that name. 
       */
  -   public void postDeregister()
  +   public ObjectName preRegister(MBeanServer server, ObjectName objName) 
  +         throws Exception
  +   {
  +      this.server = server;
  +      
  +      if (cacheObjectName == null)
  +      {
  +         if (objName == null)
      {
  +            // Calling this will create a value for cacheObjectName
  +            getPojoCacheObjectName();
  +         }
  +         else
  +         {
  +            cacheObjectName = objName.getCanonicalName();
  +         }
  +      }
  +      
  +      return new ObjectName(cacheObjectName);
      }
   
      /**
  -    * No-op.
  +    * {@link CacheJmxWrapperMBean#registerWithMBeanServer Registers the CacheJmxWrapper}, 
  +    * if {@link #getRegisterPlainCache()} is <code>true</code>.
       */
  -   public void postRegister(Boolean arg0)
  +   public void postRegister(Boolean registrationDone)
  +   {
  +      if (Boolean.TRUE.equals(registrationDone) && registerPlainCache)
  +      {         
  +         try
  +         {
  +            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_.registerWithBeanServer();
  +         }
  +         catch (Exception e)
      {
  +            log.error("Caught exception registering plain cache with JMX", e);
  +         }
  +      }
      }
   
      /**
  @@ -144,34 +190,75 @@
      { 
      }
   
  -   public ObjectName preRegister(MBeanServer server, ObjectName objName) 
  -         throws Exception
  +   /**
  +    * Unregisters the CacheJmxWrapper, if {@link #getRegisterPlainCache()} is 
  +    * <code>true</code>.
  +    */
  +   public void postDeregister()
      {
  -      this.server = server;
  -      selfRegister = false;
  +      if (registerPlainCache)
  +      {
  +         plainCacheWrapper_.unregisterMBeans();
  +      }
  +   }
         
  -      if (cacheObjectName == null)
  +   // --------------------------------------------------------------  Public methods
  +
  +   /**
  +    * Sets the configuration that the underlying cache should use.
  +    * 
  +    * @param config the configuration
  +    */
  +   public void setConfiguration(Configuration config)
         {
  -         if (objName == null)
  +      this.config = config;
  +   }
  +   
  +   public void setPojoCache(PojoCache cache)
            {    
  -            // Calling this will create a value for cacheObjectName
  -            getPojoCacheObjectName();
  +      if (created)
  +         throw new IllegalStateException("Cannot set underlying cache after call to create()");
  +   
  +   
  +      this.cache_ = cache;
  +      this.config = (cache == null ? null : cache.getCache().getConfiguration());
            }
  -         else
  +   
  +   /**
  +    * Get the name under which this object should be registered in JMX.
  +    * 
  +    * @deprecated Self-registration is a temporary workaround that will be removed.
  +    */
  +   public String getPojoCacheObjectName()
            {
  -            cacheObjectName = objName.getCanonicalName();
  +      if (cacheObjectName == null)
  +      {
  +         cacheObjectName = JmxUtil.getPojoCacheObjectName(cache_);
            }
  +      return cacheObjectName;
         }
         
  -      return new ObjectName(cacheObjectName);
  +   /**
  +    * Sets the name under which this object should be registered in JMX.
  +    * 
  +    * @deprecated Self-registration is a temporary workaround that will be removed.
  +    */
  +   public void setPojoCacheObjectName(String name) throws MalformedObjectNameException
  +   {
  +      if (name != null)
  +      {
  +         // test the name
  +         new ObjectName(name);
  +      }
  +      this.cacheObjectName = name;
      }
  -
  -   // 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. 
  +    * 
  +    * @deprecated Self-registration is a temporary workaround that will be removed.
       */
      public void setMbeanServer(MBeanServer server)
      {      
  @@ -179,7 +266,7 @@
         selfRegister = (server != null);
      }
      
  -   // Private methods
  +   // ---------------------------------------------------------------  Private methods
      
      private void constructCache() throws ConfigurationException
      {
  @@ -203,36 +290,34 @@
         
         plainCacheWrapper_.setRegisterInterceptors(getRegisterInterceptors());
         plainCacheWrapper_.setMbeanServer(server);
  -      plainCacheWrapper_.setUnderlyingCache(cache_.getCache());
  +      plainCacheWrapper_.setCache(cache_.getCache());
  +      
  +      selfConstructed = true;
      }
   
  -   private void registerMBeans() throws Exception
  +   private void registerWithMBeanServer() throws Exception
  +   {
  +      if (server != null)
      {
  -      if (selfRegister)
            JmxUtil.registerPojoCache(server, this, getPojoCacheObjectName());
  -      
  -      if (config.getServiceName() == null || config.getServiceName().length() == 0)
  +      }
  +      else
         {
  -         // We need to inject the object name into the plain cache
  -         String plainName = JmxUtil.getPlainCacheObjectName(getPojoCacheObjectName());
  -         plainCacheWrapper_.setCacheObjectName(plainName);
  +         log.warn("No MBeanServer assigned; cannot register with MBeanServer");
         }
  -      plainCacheWrapper_.registerMBeans();
      }
   
  -   private void unregisterMBeans()
  +   private void unregisterWithMBeanServer()
      {
         try
         {
  -         if (selfRegister)
  +         if (server != null)
               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