You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rm...@apache.org on 2014/05/12 19:15:18 UTC

svn commit: r1594030 - in /commons/proper/jcs/trunk: commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java

Author: rmannibucau
Date: Mon May 12 17:15:18 2014
New Revision: 1594030

URL: http://svn.apache.org/r1594030
Log:
fixing build + cleaning up little details about jmx

Modified:
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
    commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java?rev=1594030&r1=1594029&r2=1594030&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java Mon May 12 17:15:18 2014
@@ -74,7 +74,7 @@ public class CompositeCacheManager
     private static final Log log = LogFactory.getLog( CompositeCacheManager.class );
 
     /** JMX object name */
-    private static final String JMX_OBJECT_NAME = "org.apache.commons.jcs:type=JCSAdminBean";
+    public static final String JMX_OBJECT_NAME = "org.apache.commons.jcs:type=JCSAdminBean";
 
     /** Caches managed by this cache manager */
     private final Map<String, ICache<?, ?>> caches =
@@ -136,6 +136,8 @@ public class CompositeCacheManager
     /** Indicates whether JMX bean has been registered. */
     private boolean isJMXRegistered = false;
 
+    private String jmxName = JMX_OBJECT_NAME;
+
     /**
      * Gets the CacheHub instance. For backward compatibility, if this creates the instance it will
      * attempt to configure it with the default configuration. If you want to configure from your
@@ -222,19 +224,12 @@ public class CompositeCacheManager
         return new CompositeCacheManager();
     }
 
-    protected static CompositeCacheManager createInitializedInstance()
-    {
-        final CompositeCacheManager compositeCacheManager = new CompositeCacheManager();
-        compositeCacheManager.initialize();
-        return compositeCacheManager;
-    }
-
     /**
      * Default constructor
      */
     protected CompositeCacheManager()
     {
-    	// empty
+        // empty
     }
 
     /** Creates a shutdown hook and starts the scheduler service */
@@ -256,20 +251,20 @@ public class CompositeCacheManager
                     new DaemonThreadFactory("JCS-Scheduler-", Thread.MIN_PRIORITY));
 
             // Register JMX bean
-            if (!isJMXRegistered)
+            if (!isJMXRegistered && jmxName != null)
             {
-    	        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-    	        JCSAdminBean adminBean = new JCSAdminBean(this);
-    	        try
-    	        {
-    	        	ObjectName jmxObjectName = new ObjectName(JMX_OBJECT_NAME);
-    				mbs.registerMBean(adminBean, jmxObjectName);
-    				isJMXRegistered = true;
-    			}
-    	        catch (Exception e)
-    	        {
-    	            log.warn( "Could not register JMX bean.", e );
-    			}
+                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+                JCSAdminBean adminBean = new JCSAdminBean(this);
+                try
+                {
+                    ObjectName jmxObjectName = new ObjectName(jmxName);
+                    mbs.registerMBean(adminBean, jmxObjectName);
+                    isJMXRegistered = true;
+                }
+                catch (Exception e)
+                {
+                    log.warn( "Could not register JMX bean.", e );
+                }
             }
 
             this.elementEventQueue = new ElementEventQueue();
@@ -671,18 +666,18 @@ public class CompositeCacheManager
             // Unregister JMX bean
             if (isJMXRegistered)
             {
-    	        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-    	        try
-    	        {
-    	        	ObjectName jmxObjectName = new ObjectName(JMX_OBJECT_NAME);
-    				mbs.unregisterMBean(jmxObjectName);
-    			}
-    	        catch (Exception e)
-    	        {
-    	            log.warn( "Could not unregister JMX bean.", e );
-    			}
+                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+                try
+                {
+                    ObjectName jmxObjectName = new ObjectName(jmxName);
+                    mbs.unregisterMBean(jmxObjectName);
+                }
+                catch (Exception e)
+                {
+                    log.warn( "Could not unregister JMX bean.", e );
+                }
 
-    			isJMXRegistered = false;
+                isJMXRegistered = false;
             }
 
             // do the traditional shutdown of the regions.
@@ -693,16 +688,16 @@ public class CompositeCacheManager
 
             if (shutdownHook != null)
             {
-            	try
-            	{
-					Runtime.getRuntime().removeShutdownHook(shutdownHook);
-				}
-            	catch (IllegalStateException e)
-            	{
-					// May fail if the JVM is already shutting down
-				}
+                try
+                {
+                    Runtime.getRuntime().removeShutdownHook(shutdownHook);
+                }
+                catch (IllegalStateException e)
+                {
+                    // May fail if the JVM is already shutting down
+                }
 
-            	this.shutdownHook = null;
+                this.shutdownHook = null;
             }
 
             isConfigured = false;
@@ -762,7 +757,7 @@ public class CompositeCacheManager
      */
     public String[] getCacheNames()
     {
-    	return caches.keySet().toArray(new String[caches.size()]);
+        return caches.keySet().toArray(new String[caches.size()]);
     }
 
     /**
@@ -949,6 +944,14 @@ public class CompositeCacheManager
         return isConfigured;
     }
 
+    public void setJmxName(final String name) {
+        if (isJMXRegistered)
+        {
+            throw new IllegalStateException("Too late, MBean registration is done");
+        }
+        jmxName = name;
+    }
+
     /**
      * Called on shutdown. This gives use a chance to store the keys and to optimize even if the
      * cache manager's shutdown method was not called manually.

Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java?rev=1594030&r1=1594029&r2=1594030&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java Mon May 12 17:15:18 2014
@@ -38,9 +38,14 @@ public class JCSCachingManager implement
 {
     private static class InternalManager extends CompositeCacheManager
     {
-        protected static CompositeCacheManager create()
+        protected static InternalManager create()
         {
-            return createInitializedInstance();
+            return new InternalManager();
+        }
+
+        @Override // needed to call it from JCSCachingManager
+        protected void initialize() {
+            super.initialize();
         }
     }
 
@@ -51,7 +56,7 @@ public class JCSCachingManager implement
     private final ConcurrentMap<String, Cache<?, ?>> caches = new ConcurrentHashMap<String, Cache<?, ?>>();
     private final Properties configProperties;
     private volatile boolean closed = false;
-    private CompositeCacheManager delegate = InternalManager.create();
+    private InternalManager delegate = InternalManager.create();
 
     public JCSCachingManager(final CachingProvider provider, final URI uri, final ClassLoader loader, final Properties properties)
     {
@@ -61,7 +66,13 @@ public class JCSCachingManager implement
         this.properties = readConfig(uri, loader, properties);
         this.configProperties = properties;
 
-        delegate.configure(properties);
+        delegate.setJmxName(CompositeCacheManager.JMX_OBJECT_NAME
+                + ",provider=" + provider.hashCode()
+                + ",uri=" + uri.toString().replaceAll(",|:|=|\n", ".")
+                + ",classloader=" + loader.hashCode()
+                + ",properties=" + this.properties.hashCode());
+        delegate.initialize();
+        delegate.configure(this.properties);
     }
 
     private Properties readConfig(final URI uri, final ClassLoader loader, final Properties properties) {