KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > cache > ChainedReleaseStrategy


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.cache;
11
12 import java.util.*;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.mmbase.core.event.*;
18 import org.mmbase.storage.search.SearchQuery;
19 import org.mmbase.util.logging.Logger;
20 import org.mmbase.util.logging.Logging;
21
22 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
23
24 /**
25  * This class will manage a collection of <code>ReleaseStrategy</code>
26  * instances, and call them hierarchically.
27  *
28  * @since MMBase-1.8
29  * @author Ernst Bunders
30  * @version $Id: ChainedReleaseStrategy.java,v 1.18 2006/06/27 07:31:46 michiel Exp $
31  */

32 public class ChainedReleaseStrategy extends ReleaseStrategy {
33     private static final Logger log = Logging.getLoggerInstance(ChainedReleaseStrategy.class);
34
35     private final List JavaDoc releaseStrategies = new CopyOnWriteArrayList();
36
37     //this map is used to store the 'enabled' status of wrapped strategies when this one is being disabled
38
//so the old settings can be returned when it is enabled again
39
private final Map childStrategyMemory = new HashMap();
40
41     public ChainedReleaseStrategy() {
42     }
43
44
45
46     /**
47      * This method provides a way of globally switching off all strategies this one wraps.
48      * When this strategy is set to 'disabled', the state of all wrapped strategies is being
49      * preserved, so when it is being 'enabled' again, these settings are restored, in stead of
50      * just setting all wrapped strategies to 'enabled'.
51      */

52     // MM: very nice. When is this useful?
53
public void setEnabled(boolean newStatus) {
54         if(newStatus != isEnabled()){
55             super.setEnabled(newStatus);
56
57             //if the strategy is enabled and we have recorded settings, we must put them back
58

59
60             for(Iterator JavaDoc i = iterator(); i.hasNext();){
61                 ReleaseStrategy strategy = (ReleaseStrategy)i.next();
62
63                 //if it must be switched on, we must use the memeory if present
64
if(newStatus == true){
65                     Boolean JavaDoc memory = (Boolean JavaDoc) childStrategyMemory.get(strategy.getName());
66                     strategy.setEnabled( memory == null ? true : memory.booleanValue());
67                 } else {
68                     //if it must switch of, we must record the status
69
childStrategyMemory.put(strategy.getName(), new Boolean JavaDoc(strategy.isEnabled()));
70                     strategy.setEnabled(false);
71                     strategy.clear();
72                 }
73             }
74         }
75     }
76     /**
77      * Adds the strategy if it is not already there. Strategies should only
78      * occur once.
79      *
80      * @param strategy
81      */

82     public void addReleaseStrategy(ReleaseStrategy strategy) {
83         if (! releaseStrategies.contains(strategy)){
84             releaseStrategies.add(strategy);
85         }
86     }
87
88     public void removeStrategy(ReleaseStrategy strategy) {
89         releaseStrategies.remove(strategy);
90     }
91
92     /**
93      * removes all strategies
94      */

95     public void removeAllStrategies(){
96         for (Iterator JavaDoc i = iterator(); i.hasNext(); ){
97             removeStrategy((ReleaseStrategy)i.next());
98         }
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.mmbase.cache.ReleaseStrategy#getName()
105      */

106     public String JavaDoc getName() {
107         return "Multi Release Strategy";
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.mmbase.cache.ReleaseStrategy#getDescription()
114      */

115     public String JavaDoc getDescription() {
116         return "This is a wrapper for any number of strategies you would like to "
117             + "combine. it is used as the base strategy for QueryResultCache subclasses.";
118     }
119
120     public Iterator JavaDoc iterator() {
121         return releaseStrategies.iterator();
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.mmbase.cache.ReleaseStrategy#doEvaluate(org.mmbase.module.core.NodeEvent,
128      * org.mmbase.storage.search.SearchQuery, java.util.List)
129      */

130     protected final boolean doEvaluate(NodeEvent event, SearchQuery query, List JavaDoc cachedResult) {
131         // first do the 'basic' strategy that is allways there. (see constructor)
132
Iterator JavaDoc i = releaseStrategies.iterator();
133         // while the outcome of getResult is true (the cache should be flushed), we have to keep trying.
134
while (i.hasNext()) {
135             ReleaseStrategy strategy = (ReleaseStrategy) i.next();
136             StrategyResult result = strategy.evaluate(event, query, cachedResult);
137             if (! result.shouldRelease()) return false;
138         }
139         return true;
140     }
141     protected final boolean doEvaluate(RelationEvent event, SearchQuery query, List JavaDoc cachedResult) {
142         // first do the 'basic' strategy that is allways there. (see constructor)
143
Iterator JavaDoc i = releaseStrategies.iterator();
144         // while the outcome of getResult is true (the cache should be flushed), we have to keep trying.
145
while (i.hasNext()) {
146             ReleaseStrategy strategy = (ReleaseStrategy) i.next();
147             StrategyResult result = strategy.evaluate(event, query, cachedResult);
148             if (! result.shouldRelease()) return false;
149         }
150         return true;
151     }
152
153     public void clear(){
154         super.clear();
155         for(Iterator JavaDoc i = iterator(); i.hasNext();){
156             ReleaseStrategy rs = (ReleaseStrategy) i.next();
157             rs.clear();
158         }
159     }
160     /**
161      * @since MMBase-1.8.1
162      */

163     public int size() {
164         return releaseStrategies.size();
165     }
166
167     public String JavaDoc toString() {
168         return "" + releaseStrategies;
169     }
170 }
171
Popular Tags