Author: asmuts Date: Thu Jul 16 20:40:09 2009 New Revision: 794842 URL: http://svn.apache.org/viewvc?rev=794842&view=rev Log: Preventing accidental reconfiguration of composite cache manager.
Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCacheManager.java Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCacheManager.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCacheManager.java?rev=794842&r1=794841&r2=794842&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCacheManager.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCacheManager.java Thu Jul 16 20:40:09 2009 @@ -80,7 +80,7 @@ /** Default cache attributes for this cache manager */ protected ICompositeCacheAttributes defaultCacheAttr = new CompositeCacheAttributes(); - /** Default elemeent attributes for this cache manager */ + /** Default element attributes for this cache manager */ protected IElementAttributes defaultElementAttr = new ElementAttributes(); /** Used to keep track of configured auxiliaries */ @@ -104,12 +104,18 @@ /** Should we use system property substitutions. */ private static final boolean DEFAULT_USE_SYSTEM_PROPERTIES = true; + /** Once configured, you can force a recofiguration of sorts. */ + private static final boolean DEFAULT_FORCE_RECONFIGURATION = false; + /** Those waiting for notification of a shutdown. */ private Set shutdownObservers = new HashSet(); /** Indicates whether shutdown has been called. */ private boolean isShutdown = false; + /** Indicates whether configure has been called. */ + private boolean isConfigured = false; + /** * 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 @@ -290,48 +296,110 @@ */ public void configure( Properties props, boolean useSystemProperties ) { - if ( props != null ) + configure( props, useSystemProperties, DEFAULT_FORCE_RECONFIGURATION ); + } + + /** + * Configure from properties object, overriding with values from the system properteis if + * instructed. + * <p> + * You can override a specific value by passing in a ssytem property: + * <p> + * For example, you could override this value in the cache.ccf file by starting up your program + * with the argument: -Djcs.auxiliary.LTCP.attributes.TcpListenerPort=1111 + * <p> + * @param props + * @param useSystemProperties -- if true, values starting with jcs will be put into the props + * file prior to configuring the cache. + * @param forceReconfiguration - if the manager is already configured, we will try again. This + * may not work properly. + */ + public synchronized void configure( Properties props, boolean useSystemProperties, boolean forceReconfiguration ) + { + if ( props == null ) + { + log.error( "No properties found. Please configure the cache correctly." ); + return; + } + + if ( isConfigured ) { - if ( useSystemProperties ) + if ( !forceReconfiguration ) { - // override any setting with values from the system properties. - Properties sysProps = System.getProperties(); - Set keys = sysProps.keySet(); - Iterator keyIt = keys.iterator(); - while ( keyIt.hasNext() ) + if ( log.isDebugEnabled() ) { - String key = (String) keyIt.next(); - if ( key.startsWith( SYSTEM_PROPERTY_KEY_PREFIX ) ) - { - if ( log.isInfoEnabled() ) - { - log.info( "Using system property [[" + key + "] [" + sysProps.getProperty( key ) + "]]" ); - } - props.put( key, sysProps.getProperty( key ) ); - } + log.debug( "Configure called after the manager has been configured. " + + "Force reconfiguration is false. Doing nothing" ); } + return; } + else + { + if ( log.isInfoEnabled() ) + { + log.info( "Configure called after the manager has been configured. " + + "Force reconfiguration is true. Reconfiguring as best we can." ); + } + } + } + if ( useSystemProperties ) + { + overrideWithSystemProperties( props ); + } + doConfigure( props ); + } - // We will expose this for managers that need raw properties. - this.configurationProperties = props; - - // set the props value and then configure the ThreadPoolManager - ThreadPoolManager.setProps( props ); - ThreadPoolManager poolMgr = ThreadPoolManager.getInstance(); - if ( log.isDebugEnabled() ) + /** + * Any property values will be replaced with system property values that match the key. + * <p> + * TODO move to a utility. + * <p> + * @param props + */ + private static void overrideWithSystemProperties( Properties props ) + { + // override any setting with values from the system properties. + Properties sysProps = System.getProperties(); + Set keys = sysProps.keySet(); + Iterator keyIt = keys.iterator(); + while ( keyIt.hasNext() ) + { + String key = (String) keyIt.next(); + if ( key.startsWith( SYSTEM_PROPERTY_KEY_PREFIX ) ) { - log.debug( "ThreadPoolManager = " + poolMgr ); + if ( log.isInfoEnabled() ) + { + log.info( "Using system property [[" + key + "] [" + sysProps.getProperty( key ) + "]]" ); + } + props.put( key, sysProps.getProperty( key ) ); } + } + } - // configure the cache - CompositeCacheConfigurator configurator = new CompositeCacheConfigurator( this ); + /** + * Configure the cache using the supplied properties. + * <p> + * @param props assumed not null + */ + private void doConfigure( Properties props ) + { + // We will expose this for managers that need raw properties. + this.configurationProperties = props; - configurator.doConfigure( props ); - } - else + // set the props value and then configure the ThreadPoolManager + ThreadPoolManager.setProps( props ); + ThreadPoolManager poolMgr = ThreadPoolManager.getInstance(); + if ( log.isDebugEnabled() ) { - log.error( "No properties found. Please configure the cache correctly." ); + log.debug( "ThreadPoolManager = " + poolMgr ); } + + // configure the cache + CompositeCacheConfigurator configurator = new CompositeCacheConfigurator( this ); + + configurator.doConfigure( props ); + + isConfigured = true; } /** @@ -727,6 +795,22 @@ } /** + * @return the isShutdown + */ + public boolean isShutdown() + { + return isShutdown; + } + + /** + * @return the isConfigured + */ + public boolean isConfigured() + { + return isConfigured; + } + + /** * 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. */ @@ -740,7 +824,7 @@ */ public void run() { - if ( !isShutdown ) + if ( !isShutdown() ) { log.info( "Shutdown hook activated. Shutdown was not called. Shutting down JCS." ); shutDown(); --------------------------------------------------------------------- To unsubscribe, e-mail: jcs-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: jcs-dev-h...@jakarta.apache.org