Update of /var/cvs/src/org/mmbase/cache
In directory james.mmbase.org:/tmp/cvs-serv5065

Modified Files:
      Tag: MMBase-1_8
        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.6.2.1
retrieving revision 1.6.2.2
diff -u -b -r1.6.2.1 -r1.6.2.2
--- CacheManager.java   4 Sep 2006 10:48:30 -0000       1.6.2.1
+++ CacheManager.java   19 Mar 2008 15:38:04 -0000      1.6.2.2
@@ -25,7 +25,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.6.2.1 2006/09/04 10:48:30 michiel Exp $
+ * @version $Id: CacheManager.java,v 1.6.2.2 2008/03/19 15:38:04 michiel Exp $
  */
 public class CacheManager {
 
@@ -55,6 +55,12 @@
     public static Set getCaches() {
         return Collections.unmodifiableSet(caches.keySet());
     }
+    /**
+     * @since MMBase-1.8.6
+     */
+    public static Map getMap() {
+        return Collections.unmodifiableMap(caches);
+    }
 
 
     /**
@@ -150,106 +156,23 @@
                         queryCache.getReleaseStrategy().removeAllStrategies();
                         log.debug("found a SearchQueryCache: " + cacheName);
                         //see if there are globally configured release 
strategies
-                        List 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 findReleaseStrategies(DocumentReader reader, Element 
parentElement) {
-        List result = new ArrayList();
-        Iterator strategyParentIterator = 
reader.getChildElements(parentElement, "releaseStrategies");
-        if(!strategyParentIterator.hasNext()){
-            return null;
-        }else{
-            parentElement = (Element) strategyParentIterator.next();
-
-            //now find the strategies
-            Iterator strategyIterator = reader.getChildElements(parentElement, 
"strategy");
-            while(strategyIterator.hasNext()){
-                String strategyClassName =
-                    reader.getElementValue((Element)strategyIterator.next());
-                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.18
retrieving revision 1.18.2.1
diff -u -b -r1.18 -r1.18.2.1
--- ChainedReleaseStrategy.java 27 Jun 2006 07:31:46 -0000      1.18
+++ ChainedReleaseStrategy.java 19 Mar 2008 15:38:04 -0000      1.18.2.1
@@ -10,9 +10,10 @@
 package org.mmbase.cache;
 
 import java.util.*;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+
+import org.mmbase.util.xml.DocumentReader;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 import org.mmbase.core.event.*;
 import org.mmbase.storage.search.SearchQuery;
@@ -27,7 +28,7 @@
  *
  * @since MMBase-1.8
  * @author Ernst Bunders
- * @version $Id: ChainedReleaseStrategy.java,v 1.18 2006/06/27 07:31:46 
michiel Exp $
+ * @version $Id: ChainedReleaseStrategy.java,v 1.18.2.1 2008/03/19 15:38:04 
michiel Exp $
  */
 public class ChainedReleaseStrategy extends ReleaseStrategy {
     private static final Logger log = 
Logging.getLoggerInstance(ChainedReleaseStrategy.class);
@@ -44,6 +45,73 @@
 
 
     /**
+     * @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.
      * When this strategy is set to 'disabled', the state of all wrapped 
strategies is being
      * preserved, so when it is being 'enabled' again, these settings are 
restored, in stead of
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to