[jboss-cvs] JBossAS SVN: r72376 - trunk/cluster/src/main/org/jboss/ha/cachemanager.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 17 13:57:15 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-04-17 13:57:15 -0400 (Thu, 17 Apr 2008)
New Revision: 72376

Added:
   trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerLocator.java
Modified:
   trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java
Log:
[JBAS-5378] Fix JMX registration, register with service locator, make cache listener public

Modified: trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java	2008-04-17 17:43:41 UTC (rev 72375)
+++ trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManager.java	2008-04-17 17:57:15 UTC (rev 72376)
@@ -31,7 +31,6 @@
 import javax.management.JMException;
 import javax.management.MBeanRegistration;
 import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 import javax.naming.Context;
 import javax.naming.InitialContext;
@@ -54,7 +53,6 @@
 import org.jboss.cache.pojo.PojoCache;
 import org.jboss.cache.pojo.PojoCacheFactory;
 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapper;
-import org.jboss.ha.framework.server.ClusterPartition;
 import org.jboss.logging.Logger;
 import org.jboss.naming.NonSerializableFactory;
 import org.jgroups.ChannelFactory;
@@ -193,17 +191,19 @@
          
          if (registerCachesInJmx && mbeanServer != null)
          {
-            ObjectName oName = getObjectName(getPojoCacheJmxAttributes(), configName);
+            String oName = getObjectName(getPojoCacheJmxAttributes(), configName);
             PojoCacheJmxWrapper wrapper = new PojoCacheJmxWrapper(cache);
-            cache.getCache().addCacheListener(new StartStopListener(wrapper));
             try
             {
-               mbeanServer.registerMBean(wrapper, oName);
+               mbeanServer.registerMBean(wrapper, new ObjectName(oName));
             }
             catch (JMException e)
             {
                throw new RuntimeException("Cannot register cache under name " + oName, e);
             }
+            
+            // Synchronize the start/stop of the plain and pojo cache
+            cache.getCache().addCacheListener(new StartStopListener(wrapper));
          }
       }
    }
@@ -234,17 +234,19 @@
          
          if (registerCachesInJmx && mbeanServer != null)
          {
-            ObjectName oName = getObjectName(getCoreCacheJmxAttributes(), configName);
+            String oName = getObjectName(getCoreCacheJmxAttributes(), configName);
             CacheJmxWrapper wrapper = new CacheJmxWrapper(cache);
-            cache.addCacheListener(new StartStopListener(wrapper));
             try
             {
-               mbeanServer.registerMBean(wrapper, oName);
+               mbeanServer.registerMBean(wrapper, new ObjectName(oName));
             }
             catch (JMException e)
             {
                throw new RuntimeException("Cannot register cache under name " + oName, e);
             }
+            
+            // Synchronize the start/stop of the plain and pojo cache
+            cache.addCacheListener(new StartStopListener(wrapper));
          }
       }
    }
@@ -268,18 +270,19 @@
             
             if (registerCachesInJmx && mbeanServer != null && !getCacheNames().contains(configName))
             {
-               ObjectName oName = getObjectName(getCoreCacheJmxAttributes(), configName);
-               if (mbeanServer.isRegistered(oName))
+               String oNameStr = getObjectName(getCoreCacheJmxAttributes(), configName);
+               try
                {
-                  try
+                  ObjectName oName = new ObjectName(oNameStr);
+                  if (mbeanServer.isRegistered(oName))
                   {
                      mbeanServer.unregisterMBean(oName);
                   }
-                  catch (JMException e)
-                  {
-                     log.error("Problem unregistering PojoCacheJmxWrapper " + oName, e);
-                  }
                }
+               catch (JMException e)
+               {
+                  log.error("Problem unregistering PojoCacheJmxWrapper " + oNameStr, e);
+               }
                
             }
          }
@@ -293,6 +296,10 @@
       {
          super.start();
          
+         CacheManagerLocator locator = CacheManagerLocator.getCacheManagerLocator();
+         if (locator.getDirectlyRegisteredManager() == null)
+            locator.registerCacheManager(this);
+         
          // Bind ourself in the public JNDI space if configured to do so
          if (jndiName != null)
          {
@@ -321,8 +328,9 @@
             pojoCaches.clear();
             pojoCacheCheckouts.clear();
          }
+         
          super.stop();
-
+         
          if (jndiName != null)
          {
             InitialContext ctx = null;
@@ -359,6 +367,11 @@
                log.error("Caught exception unbinding from NonSerializableFactory", e);
             }         
          }
+
+         CacheManagerLocator locator = CacheManagerLocator.getCacheManagerLocator();
+         if (locator.getDirectlyRegisteredManager() == this)
+            locator.deregisterCacheManager();
+         
          started = false;
       }
    }
@@ -490,35 +503,30 @@
       
       if (registerCachesInJmx && mbeanServer != null)
       {
-         ObjectName oName = getObjectName(getPojoCacheJmxAttributes(), configName);
-         if (mbeanServer.isRegistered(oName))
+         String oNameStr = getObjectName(getPojoCacheJmxAttributes(), configName);
+         try
          {
-            try
+            ObjectName oName = new ObjectName(oNameStr);
+            if (mbeanServer.isRegistered(oName))
             {
                mbeanServer.unregisterMBean(oName);
             }
-            catch (JMException e)
-            {
-               log.error("Problem unregistering PojoCacheJmxWrapper " + oName, e);
-            }
          }
+         catch (JMException e)
+         {
+            log.error("Problem unregistering PojoCacheJmxWrapper " + oNameStr, e);
+         }
       }
    }
    
-   private ObjectName getObjectName(String attributesBase, String configName)
+   private String getObjectName(String attributesBase, String configName)
    {
-      try
-      {
-         return new ObjectName(getJmxDomain() + "=" + attributesBase + configName);
-      }
-      catch (MalformedObjectNameException e)
-      {
-         throw new IllegalStateException("Illegal object name", e);
-      }
+      String base = getJmxDomain() == null ? "" : getJmxDomain();
+      return base + ":" + attributesBase + configName;
    }
    
    @CacheListener
-   private class StartStopListener
+   public static class StartStopListener
    {
       private final CacheJmxWrapper plainWrapper;
       private final PojoCacheJmxWrapper pojoWrapper;

Added: trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerLocator.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerLocator.java	                        (rev 0)
+++ trunk/cluster/src/main/org/jboss/ha/cachemanager/CacheManagerLocator.java	2008-04-17 17:57:15 UTC (rev 72376)
@@ -0,0 +1,185 @@
+/*
+ * 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.ha.cachemanager;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+
+import org.jboss.ha.framework.interfaces.HAPartition;
+import org.jboss.logging.Logger;
+
+/**
+ * Service Locator utility for locating a {@link PojoCacheManager}. Maintains
+ * an internal ref to a manager, and if it that is null,
+ * will attempt to find it in JNDI using a standard naming pattern.
+ * 
+ * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1.1 $
+ */
+public class CacheManagerLocator
+{
+   private static final Logger log = Logger.getLogger(CacheManagerLocator.class);
+   
+   private static CacheManagerLocator sharedInstance = new CacheManagerLocator();
+   
+   /** Name of the standard JNDI context under which HAPartitions are bound */
+   public static final String STANDARD_JNDI_NAME = "java:CacheManager";
+   
+   private PojoCacheManager registeredManager;
+   
+   /**
+    * Gets the 
+    * @return the shared CacheManagerLocator; will not be <code>null</code>
+    */
+   public static CacheManagerLocator getCacheManagerLocator()
+   {
+      return sharedInstance;
+   }
+   
+   /**
+    * Appends the partition name to {@link #STANDARD_JNDI_NAME}; e.g.
+    * <code>/HAPartition/DefaultPartition</code>.
+    * 
+    * @param partitionName the name of the partition
+    * @return a string representing the standard name under which a 
+    *         partition is bound in JNDI.
+    */
+   public static final String getStandardJndiBinding()
+   {
+      return STANDARD_JNDI_NAME;
+   }
+   
+   /**
+    * Allows replacement of the default implementation.
+    * 
+    * @param shared the locator to use. Cannot be <code>null</code>.
+    */
+   protected static void setSharedInstance(CacheManagerLocator shared)
+   {
+      if (shared == null)
+         throw new IllegalArgumentException("shared cannot be null");
+      
+      sharedInstance = shared;
+   }
+   
+   /**
+    * Allow subclasses to create a new HAPartitionLocator.
+    */
+   protected CacheManagerLocator()
+   {      
+   }
+   
+   /**
+    * Locates and returns the {@link HAPartition} whose partition name matches
+    * the given <code>partitionName</code>.
+    * 
+    * @param jndiProperties any naming properties to pass to new InitialContext() 
+    *                       if JNDI lookup is needed.
+    * @return the partition. Will not return <code>null</code>
+    * 
+    * @throws IllegalStateException if no cache manager can be found
+    */
+   public PojoCacheManager getCacheManager(Hashtable jndiProperties)
+   {
+      PojoCacheManager manager = registeredManager;
+      if (manager == null)
+      {
+         try
+         {
+            manager = findInJndi(jndiProperties);
+         }
+         catch (NamingException e)
+         {
+            log.error("Problem finding CacheManager in JNDI", e);        
+         }
+      }
+      
+      if (manager == null)
+         throw new IllegalStateException("CacheManager not found");
+      
+      return manager;
+   }
+   
+   /**
+    * Register a CacheManager with this locator.
+    * 
+    * @param partition the partition
+    */
+   public void registerCacheManager(PojoCacheManager manager)
+   {
+      if (manager != null)
+      {
+         if (registeredManager != null && manager != registeredManager)
+            throw new IllegalStateException("CacheManager already registered");
+         registeredManager = manager;
+      }
+   }
+   
+   public PojoCacheManager getDirectlyRegisteredManager()
+   {
+      return registeredManager;
+   }
+   
+   /**
+    * Deregister a partition from this locator.
+    * 
+    * @param partition the partition
+    */
+   public void deregisterCacheManager()
+   {
+      registeredManager = null;
+   }
+   
+   /**
+    * Find the given CacheManager in JNDI under a 
+    * {@link #getStandardJndiBinding() standard binding}.
+    * 
+    * @param jndiProperties any naming properties to pass to new InitialContext() 
+    * 
+    * @return the CacheManager, or <code>null</code>
+    * 
+    * @throws NamingException if there is a problem with the naming context. Will
+    *                         not throw NameNotFoundException; if not found, will
+    *                         return <code>null</code>.
+    *                         
+    * @see #getStandardJndiBinding(String)
+    */
+   protected PojoCacheManager findInJndi(Hashtable jndiProperties) throws NamingException
+   {
+      try
+      {
+         Context ctx = new InitialContext(jndiProperties);
+         return (PojoCacheManager) ctx.lookup(getStandardJndiBinding());
+      }
+      catch (NameNotFoundException e)
+      {
+         // just not there
+         return null;
+      }     
+      
+   }
+}




More information about the jboss-cvs-commits mailing list