Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 688E210AAC for ; Mon, 7 Apr 2014 19:22:30 +0000 (UTC) Received: (qmail 42359 invoked by uid 500); 7 Apr 2014 19:22:28 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 42252 invoked by uid 500); 7 Apr 2014 19:22:28 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 42245 invoked by uid 99); 7 Apr 2014 19:22:27 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Apr 2014 19:22:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Apr 2014 19:22:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 1221123889ED; Mon, 7 Apr 2014 19:22:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1585570 - in /commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control: CompositeCacheConfigurator.java CompositeCacheManager.java Date: Mon, 07 Apr 2014 19:22:01 -0000 To: commits@commons.apache.org From: tv@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140407192202.1221123889ED@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tv Date: Mon Apr 7 19:22:01 2014 New Revision: 1585570 URL: http://svn.apache.org/r1585570 Log: Attempt to fix the lifecycle Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheConfigurator.java commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheConfigurator.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheConfigurator.java?rev=1585570&r1=1585569&r2=1585570&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheConfigurator.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheConfigurator.java Mon Apr 7 19:22:01 2014 @@ -193,7 +193,7 @@ public class CompositeCacheConfigurator protected void setDefaultAuxValues( Properties props ) { String value = OptionConverter.findAndSubst( DEFAULT_REGION, props ); - compositeCacheManager.defaultAuxValues = value; + compositeCacheManager.setDefaultAuxValues(value); if ( log.isInfoEnabled() ) { @@ -248,10 +248,8 @@ public class CompositeCacheConfigurator { cache = parseRegion( props, regionName, value, null, SYSTEM_REGION_PREFIX ); } - compositeCacheManager.systemCaches.put( regionName, cache ); - // to be available for remote reference they need to be here as - // well - compositeCacheManager.caches.put( regionName, cache ); + + compositeCacheManager.addCache( regionName, cache ); } } } @@ -281,7 +279,7 @@ public class CompositeCacheConfigurator { cache = parseRegion( props, regionName, auxiliaryList ); } - compositeCacheManager.caches.put( regionName, cache ); + compositeCacheManager.addCache( regionName, cache ); } } Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java?rev=1585570&r1=1585569&r2=1585570&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java Mon Apr 7 19:22:01 2014 @@ -78,12 +78,8 @@ public class CompositeCacheManager /** JMX object name */ private static final String JMX_OBJECT_NAME = "org.apache.commons.jcs:type=JCSAdminBean"; - /** Caches managed by this cache manager */ // TODO privatise somehow - protected Hashtable> caches = - new Hashtable>(); - - /** Internal system caches for this cache manager */ // TODO privatise somehow - protected Hashtable> systemCaches = + /** Caches managed by this cache manager */ + private final Hashtable> caches = new Hashtable>(); /** Number of clients accessing this cache manager */ @@ -96,18 +92,18 @@ public class CompositeCacheManager private IElementAttributes defaultElementAttr = new ElementAttributes(); /** Used to keep track of configured auxiliaries */ - private Hashtable auxiliaryFactoryRegistry = + private final Hashtable auxiliaryFactoryRegistry = new Hashtable( 11 ); /** Used to keep track of attributes for auxiliaries. */ - private Hashtable auxiliaryAttributeRegistry = + private final Hashtable auxiliaryAttributeRegistry = new Hashtable( 11 ); /** Properties with which this manager was configured. This is exposed for other managers. */ private Properties configurationProperties; /** The default auxiliary caches to be used if not preconfigured */ - protected String defaultAuxValues;// TODO privatise somehow + private String defaultAuxValues; /** The Singleton Instance */ private static CompositeCacheManager instance; @@ -127,8 +123,11 @@ public class CompositeCacheManager /** The central background scheduler. */ private ScheduledExecutorService scheduledExecutor; - /** Indicates whether shutdown has been called. */ - private boolean isShutdown = false; + /** Shutdown hook thread instance */ + private ShutdownHook shutdownHook; + + /** Indicates whether the instance has been initialized. */ + private boolean isInitialized = false; /** Indicates whether configure has been called. */ private boolean isConfigured = false; @@ -146,18 +145,7 @@ public class CompositeCacheManager */ public static synchronized CompositeCacheManager getInstance() throws CacheException { - if ( instance == null ) - { - log.debug( "Instance is null, creating with default config" ); - - instance = createInstance(); - - instance.configure(); - } - - instance.incrementClients(); - - return instance; + return getInstance( CacheConstants.DEFAULT_CONFIG ); } /** @@ -173,11 +161,14 @@ public class CompositeCacheManager { if ( log.isInfoEnabled() ) { - log.info( "Instance is null, creating with default config [" + propsFilename + "]" ); + log.info( "Instance is null, creating with config [" + propsFilename + "]" ); } instance = createInstance(); + } + if (!instance.isConfigured()) + { instance.configure( propsFilename ); } @@ -220,48 +211,61 @@ public class CompositeCacheManager return new CompositeCacheManager(); } - /** Creates a shutdown hook and starts the scheduler service */ + /** + * Default constructor + */ protected CompositeCacheManager() { - ShutdownHook shutdownHook = new ShutdownHook(); - try - { - Runtime.getRuntime().addShutdownHook( shutdownHook ); - } - catch ( AccessControlException e ) - { - log.error( "Could not register shutdown hook.", e ); - } + initialize(); + } - this.scheduledExecutor = Executors.newScheduledThreadPool(4, new ThreadFactory() + /** Creates a shutdown hook and starts the scheduler service */ + protected void initialize() + { + if (!isInitialized) { - @Override - public Thread newThread(Runnable runner) + this.shutdownHook = new ShutdownHook(); + try + { + Runtime.getRuntime().addShutdownHook( shutdownHook ); + } + catch ( AccessControlException e ) { - Thread t = new Thread( runner ); - String oldName = t.getName(); - t.setName( "JCS-Scheduler-" + oldName ); - t.setDaemon( true ); - t.setPriority( Thread.MIN_PRIORITY ); - return t; + log.error( "Could not register shutdown hook.", e ); } - }); - // Register JMX bean - if (!isJMXRegistered) - { - 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 ); - } + this.scheduledExecutor = Executors.newScheduledThreadPool(4, new ThreadFactory() + { + @Override + public Thread newThread(Runnable runner) + { + Thread t = new Thread( runner ); + String oldName = t.getName(); + t.setName( "JCS-Scheduler-" + oldName ); + t.setDaemon( true ); + t.setPriority( Thread.MIN_PRIORITY ); + return t; + } + }); + + // Register JMX bean + if (!isJMXRegistered) + { + 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 ); + } + } + + isInitialized = true; } } @@ -625,52 +629,54 @@ public class CompositeCacheManager */ public void shutDown() { - isShutdown = true; + synchronized (CompositeCacheManager.class) + { + isInitialized = false; - // shutdown all scheduled jobs - this.scheduledExecutor.shutdownNow(); + // shutdown all scheduled jobs + this.scheduledExecutor.shutdownNow(); - // notify any observers - synchronized ( shutdownObservers ) - { - // We don't need to worry about locking the set. - // since this is a shutdown command, nor do we need - // to queue these up. - for (IShutdownObserver observer : shutdownObservers) + // notify any observers + synchronized ( shutdownObservers ) { - observer.shutdown(); + // We don't need to worry about locking the set. + // since this is a shutdown command, nor do we need + // to queue these up. + for (IShutdownObserver observer : shutdownObservers) + { + observer.shutdown(); + } } - } - // 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 ); - } + // 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 ); + } - isJMXRegistered = false; - } + isJMXRegistered = false; + } - // do the traditional shutdown of the regions. - String[] names = getCacheNames(); - int len = names.length; - for ( int i = 0; i < len; i++ ) - { - String name = names[i]; - freeCache( name ); + // do the traditional shutdown of the regions. + for (String name : getCacheNames()) + { + freeCache( name ); + } + + isConfigured = false; } } /** */ - protected void incrementClients() + private void incrementClients() { clients++; } @@ -775,6 +781,25 @@ public class CompositeCacheManager } /** + * Add a cache to the map of registered caches + * + * @param cacheName the region name + * @param cache the cache instance + */ + void addCache(String cacheName, ICache cache) + { + caches.put(cacheName, cache); + } + + /** + * @param defaultAuxValues the defaultAuxValues to set + */ + void setDefaultAuxValues(String defaultAuxValues) + { + this.defaultAuxValues = defaultAuxValues; + } + + /** * Gets stats for debugging. This calls gets statistics and then puts all the results in a * string. This returns data for all regions. *

@@ -874,11 +899,11 @@ public class CompositeCacheManager } /** - * @return the isShutdown + * @return the isInitialized */ - public boolean isShutdown() + public boolean isInitialized() { - return isShutdown; + return isInitialized; } /** @@ -904,7 +929,7 @@ public class CompositeCacheManager @Override public void run() { - if ( !isShutdown() ) + if ( isInitialized() ) { log.info( "Shutdown hook activated. Shutdown was not called. Shutting down JCS." ); shutDown();