Update of /var/cvs/src/org/mmbase/cache In directory james.mmbase.org:/tmp/cvs-serv4852
Modified Files: CacheManager.java ChainedReleaseStrategy.java Log Message: moved code to parse XML to fill ChainedReleaseStrategoy to ChainedReleaseStrategy itself (this makes it reusable) See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/cache Index: CacheManager.java =================================================================== RCS file: /var/cvs/src/org/mmbase/cache/CacheManager.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -b -r1.24 -r1.25 --- CacheManager.java 23 Feb 2008 14:03:23 -0000 1.24 +++ CacheManager.java 19 Mar 2008 15:32:40 -0000 1.25 @@ -24,7 +24,7 @@ * Cache manager manages the static methods of [EMAIL PROTECTED] Cache}. If you prefer you can call them on this in stead. * * @since MMBase-1.8 - * @version $Id: CacheManager.java,v 1.24 2008/02/23 14:03:23 andre Exp $ + * @version $Id: CacheManager.java,v 1.25 2008/03/19 15:32:40 michiel Exp $ */ public class CacheManager { @@ -177,104 +177,24 @@ queryCache.getReleaseStrategy().removeAllStrategies(); log.debug("found a SearchQueryCache: " + cacheName); //see if there are globally configured release strategies - List<ReleaseStrategy> strategies = findReleaseStrategies(xmlReader, xmlReader.getElementByPath("caches")); - if(strategies != null){ - log.debug("found " + strategies.size() + " globally configured strategies"); - queryCache.addReleaseStrategies(strategies); + Element releaseStrategies = xmlReader.getElementByPath("caches.releaseStrategies"); + if (releaseStrategies != null) { + queryCache.getReleaseStrategy().fillFromXml(releaseStrategies); } + queryCache.getReleaseStrategy().fillFromXml(cacheElement); - //see if there are strategies configured for this cache - strategies = findReleaseStrategies(xmlReader, cacheElement); - if(strategies != null){ - log.debug("found " + strategies.size() + " strategies for cache " + cache.getName()); - queryCache.addReleaseStrategies(strategies); - } if (queryCache.getReleaseStrategy().size() == 0) { log.warn("No release-strategies configured for cache " + queryCache + " (nor globally configured); falling back to basic release strategy"); queryCache.addReleaseStrategy(new BasicReleaseStrategy()); } + log.service("Release strategies for " + queryCache.getName() + ": " + queryCache.getReleaseStrategy()); } } } } } - /** - * @param reader xml document reader instance - * @param parentElement the parent of the releaseStrategies element - * @return List of ReleaseStrategy instances - * @since 1.8 - */ - private static List<ReleaseStrategy> findReleaseStrategies(DocumentReader reader, Element parentElement) { - List<ReleaseStrategy> result = new ArrayList<ReleaseStrategy>(); - - List<Element> strategyParentIterator = reader.getChildElements(parentElement, "releaseStrategies"); - if(strategyParentIterator.size() == 0){ - return null; - } else{ - parentElement = strategyParentIterator.get(0); - - //now find the strategies - for (Element strategy: reader.getChildElements(parentElement, "strategy")) { - String strategyClassName = reader.getElementValue(strategy); - log.debug("found strategy in configuration: "+ strategyClassName); - try { - ReleaseStrategy releaseStrategy = getStrategyInstance(strategyClassName); - log.debug("still there after trying to get a strategy instance... Instance is " + releaseStrategy==null ? "null" : "not null"); - - //check if we got something - if(releaseStrategy != null){ - result.add(releaseStrategy); - log.debug("Successfully created and added "+releaseStrategy.getName() + " instance"); - } else { - log.error("release strategy instance is null."); - } - - } catch (CacheConfigurationException e1) { - // here we throw a runtime exception, because there is - // no way we can deal with this error. - throw new RuntimeException("Cache configuration error: " + e1.toString(), e1); - } - } - } - return result; - } - /** - * I moved this code away from <code>configure()</code> just to - * clean up a little, and keep the code readable - * XXX: Who is I? - * @param strategyClassName - * @since 1.8 - */ - private static ReleaseStrategy getStrategyInstance(String strategyClassName) throws CacheConfigurationException { - log.debug("getStrategyInstance()"); - Class strategyClass; - ReleaseStrategy strategy = null; - try { - strategyClass = Class.forName(strategyClassName); - strategy = (ReleaseStrategy) strategyClass.newInstance(); - log.debug("created strategy instance: "+strategyClassName); - - } catch (ClassCastException e){ - log.debug(strategyClassName + " can not be cast to strategy"); - throw new CacheConfigurationException(strategyClassName + " can not be cast to strategy"); - } catch (ClassNotFoundException e) { - log.debug("exception getStrategyInstance()"); - throw new CacheConfigurationException("Class "+strategyClassName + - "was not found"); - } catch (InstantiationException e) { - log.debug("exception getStrategyInstance()"); - throw new CacheConfigurationException("A new instance of " + strategyClassName + - "could not be created: " + e.toString()); - } catch (IllegalAccessException e) { - log.debug("exception getStrategyInstance()"); - throw new CacheConfigurationException("A new instance of " + strategyClassName + - "could not be created: " + e.toString()); - } - log.debug("exit getStrategyInstance()"); - return strategy; - } /** * The caches can be configured with an XML file, this file can Index: ChainedReleaseStrategy.java =================================================================== RCS file: /var/cvs/src/org/mmbase/cache/ChainedReleaseStrategy.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -b -r1.22 -r1.23 --- ChainedReleaseStrategy.java 25 Feb 2007 18:18:24 -0000 1.22 +++ ChainedReleaseStrategy.java 19 Mar 2008 15:32:40 -0000 1.23 @@ -11,11 +11,16 @@ import java.util.*; +import org.mmbase.util.xml.DocumentReader; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; import org.mmbase.core.event.*; import org.mmbase.module.core.MMObjectNode; import org.mmbase.storage.search.SearchQuery; import java.util.concurrent.CopyOnWriteArrayList; +import org.mmbase.util.logging.Logger; +import org.mmbase.util.logging.Logging; /** * This class will manage a collection of <code>ReleaseStrategy</code> @@ -23,10 +28,12 @@ * * @since MMBase-1.8 * @author Ernst Bunders - * @version $Id: ChainedReleaseStrategy.java,v 1.22 2007/02/25 18:18:24 nklasens Exp $ + * @version $Id: ChainedReleaseStrategy.java,v 1.23 2008/03/19 15:32:40 michiel Exp $ */ public class ChainedReleaseStrategy extends ReleaseStrategy { + private static final Logger log = Logging.getLoggerInstance(ChainedReleaseStrategy.class); + private final List<ReleaseStrategy> releaseStrategies = new CopyOnWriteArrayList<ReleaseStrategy>(); //this map is used to store the 'enabled' status of wrapped strategies when this one is being disabled @@ -37,6 +44,71 @@ } + /** + * @since MMBase-1.8.6 + */ + public void fillFromXml(final Element element) { + //now find the strategies + NodeList childNodes = element.getChildNodes(); + for (int k = 0; k < childNodes.getLength(); k++) { + if (childNodes.item(k) instanceof Element) { + Element childElement = (Element) childNodes.item(k); + if ("strategy".equals(childElement.getLocalName())) { + try { + String strategyClassName = DocumentReader.getNodeTextValue(childElement); + ReleaseStrategy releaseStrategy = getStrategyInstance(strategyClassName); + log.debug("still there after trying to get a strategy instance... Instance is " + releaseStrategy==null ? "null" : "not null"); + //check if we got something + if(releaseStrategy != null){ + addReleaseStrategy(releaseStrategy); + log.debug("Successfully created and added "+releaseStrategy.getName() + " instance"); + } else { + log.error("release strategy instance is null."); + } + + } catch (CacheConfigurationException e1) { + // here we throw a runtime exception, because there is + // no way we can deal with this error. + throw new RuntimeException("Cache configuration error: " + e1.toString(), e1); + } + } + } + } + } + + /** + * @param strategyClassName + * @since 1.8.6 + */ + private static ReleaseStrategy getStrategyInstance(String strategyClassName) throws CacheConfigurationException { + log.debug("getStrategyInstance()"); + Class strategyClass; + ReleaseStrategy strategy = null; + try { + strategyClass = Class.forName(strategyClassName); + strategy = (ReleaseStrategy) strategyClass.newInstance(); + log.debug("created strategy instance: "+strategyClassName); + + } catch (ClassCastException e){ + log.debug(strategyClassName + " can not be cast to strategy"); + throw new CacheConfigurationException(strategyClassName + " can not be cast to strategy"); + } catch (ClassNotFoundException e) { + log.debug("exception getStrategyInstance()"); + throw new CacheConfigurationException("Class "+strategyClassName + + "was not found"); + } catch (InstantiationException e) { + log.debug("exception getStrategyInstance()"); + throw new CacheConfigurationException("A new instance of " + strategyClassName + + "could not be created: " + e.toString()); + } catch (IllegalAccessException e) { + log.debug("exception getStrategyInstance()"); + throw new CacheConfigurationException("A new instance of " + strategyClassName + + "could not be created: " + e.toString()); + } + log.debug("exit getStrategyInstance()"); + return strategy; + } + /** * This method provides a way of globally switching off all strategies this one wraps. _______________________________________________ Cvs mailing list Cvs@lists.mmbase.org http://lists.mmbase.org/mailman/listinfo/cvs