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

Brian Stansberry brian.stansberry at jboss.com
Thu Nov 9 21:23:57 EST 2006


  User: bstansberry
  Date: 06/11/09 21:23:57

  Modified:    src/org/jboss/cache/jmx  CacheJmxWrapper.java
  Log:
  Refactor how things are registered in JMX
  
  Revision  Changes    Path
  1.2       +97 -70    JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheJmxWrapper.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- CacheJmxWrapper.java	6 Nov 2006 05:01:42 -0000	1.1
  +++ CacheJmxWrapper.java	10 Nov 2006 02:23:57 -0000	1.2
  @@ -44,22 +44,10 @@
      private MBeanServer server;
      private String cacheObjectName;
      private boolean selfRegister = true;
  +   private boolean selfConstructed = false;
      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;
  -   }
  +   // --------------------------------------------------------  CacheJMXWrapperMBean
      
      public boolean getRegisterInterceptors()
      {
  @@ -94,11 +82,12 @@
         if (tcpi == null)
            constructCache();
         
  +      if (selfConstructed)
         tcpi.create();
         
  -      if (server != null)
  +      if (selfRegister)
         {
  -         registerMBeans();
  +         registerWithBeanServer();
         }
         
         created = true;
  @@ -106,19 +95,22 @@
   
      public void start() throws Exception
      {
  +      if (selfConstructed)
         tcpi.start();
      }
   
      public void stop()
      {
  +      if (selfConstructed)
         tcpi.stop();
      }
   
      public void destroy()
      {
  +      if (selfConstructed)
         tcpi.destroy();
         
  -      if (server != null)
  +      if (selfRegister)
         {
            unregisterMBeans();
         }
  @@ -126,20 +118,51 @@
         created = false;
      }
      
  -   // 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
  +            getCacheObjectName();
  +         }
  +         else
  +         {
  +            cacheObjectName = objName.getCanonicalName();
  +         }
  +      }
  +      return new ObjectName(cacheObjectName);
      }
   
      /**
  -    * No-op.
  +    * Registers the cache's interceptors, if {@link #getRegisterInterceptors()} 
  +    * is <code>true</code>.
       */
  -   public void postRegister(Boolean arg0)
  +   public void postRegister(Boolean registrationDone)
  +   {
  +      if (Boolean.TRUE.equals(registrationDone) && registerInterceptors)
  +      {
  +         try
      {
  +            JmxUtil.registerInterceptors(server, tcpi.getInterceptorChain(), cacheObjectName);
  +         }
  +         catch (Exception e)
  +         {
  +            log.error("Caught exception registering cache interceptors with JMX", e);
  +         }
  +      }
      }
   
      /**
  @@ -149,28 +172,43 @@
      { 
      }
   
  -   public ObjectName preRegister(MBeanServer server, ObjectName objName) 
  -         throws Exception
  +   /**
  +    * Unregisters the interceptors, if {@link #getRegisterInterceptors()} is 
  +    * <code>true</code>.
  +    */
  +   public void postDeregister()
      {
  -      this.server = server;
  -      selfRegister = false;
  -      
  -      if (cacheObjectName == null)
  +      if (registerInterceptors)
         {
  -         if (objName == null)
  +         try
            {    
  -            // Calling this will create a value for cacheObjectName
  -            getCacheObjectName();
  +            JmxUtil.unregisterInterceptors(server, tcpi.getInterceptorChain(), getCacheObjectName());
            }
  -         else
  +         catch (Exception e)
            {
  -            cacheObjectName = objName.getCanonicalName();
  +            log.error("Exception unregistering interceptors from JMX", e);
            }
         }
  -      return new ObjectName(cacheObjectName);
      }
      
  -   // Public methods
  +   // ---------------------------------------------------------------  Public methods
  +
  +   public Configuration getConfiguration()
  +   {
  +      return config;
  +   }
  +
  +   /**
  +    * Sets the configuration that the underlying cache should use.
  +    * 
  +    * @param config the configuration
  +    * 
  +    * @throws IllegalArgumentException if <code>config</code> is <code>null</code>.
  +    */
  +   public void setConfiguration(Configuration config)
  +   {
  +      this.config = config;
  +   }
      
      /**
       * Provides a hook for dependency injecting the MBeanServer in environments where
  @@ -188,13 +226,14 @@
       * 
       * @param cache
       */
  -   public void setUnderlyingCache(Cache cache)
  +   public void setCache(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;
  +      this.config = (tcpi == null ? null : tcpi.getConfiguration());
      }
      
      public String getCacheObjectName()
  @@ -216,48 +255,35 @@
         this.cacheObjectName = name;
      }
      
  -   public void registerMBeans() throws Exception
  +   public void registerWithBeanServer() throws Exception
      {
  -      String cacheObjectName = getCacheObjectName();
  -      
  -      if (selfRegister)
  -         JmxUtil.registerCacheMBean(server, this, cacheObjectName);
  -      
  -      if (registerInterceptors)
  -         JmxUtil.registerInterceptors(server, tcpi.getInterceptorChain(), cacheObjectName);
  -      
  -   }
  -   
  -   public void unregisterMBeans()
  +      if (server != null)
      {
         String cacheObjectName = getCacheObjectName();
  -   
  -      if (selfRegister)
  -      {
  -         try
  -         {
  -            JmxUtil.unregisterCacheMBean(server, cacheObjectName);
  +         JmxUtil.registerCacheMBean(server, this, cacheObjectName);
            }
  -         catch (Exception e)
  +      else
            {
  -            log.error("Exception unregistering cache from JMX", e);
  +         log.warn("No MBeanServer assigned; cannot register with MBeanServer");
            }
         }
         
  -      if (registerInterceptors)
  +   public void unregisterMBeans()
  +   {
  +      if (server != null)
         {
            try
            {
  -            JmxUtil.unregisterInterceptors(server, tcpi.getInterceptorChain(), cacheObjectName);
  +            JmxUtil.unregisterCacheMBean(server, getCacheObjectName());
            }
            catch (Exception e)
            {
  -            log.error("Exception unregistering interceptors from JMX", e);
  +            log.error("Exception unregistering cache from JMX", e);
            }
         }      
      }
      
  -   // Private methods
  +   // --------------------------------------------------------------  Private methods
      
      private void constructCache() throws ConfigurationException
      {
  @@ -270,7 +296,8 @@
            throw new ConfigurationException("Must call setConfiguration() before call to create()");
         }
         
  -      setUnderlyingCache(DefaultCacheFactory.createCache(config, false));
  +      setCache(DefaultCacheFactory.createCache(config, false));
  +      selfConstructed = true;
      }
   
   }
  
  
  



More information about the jboss-cvs-commits mailing list