001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cocoon.components.flow;
017:
018: import org.apache.cocoon.components.flow.WebContinuation;
019: import org.apache.cocoon.components.flow.WebContinuationDataBean;
020: import org.apache.cocoon.components.flow.ContinuationsDisposer;
021: import org.apache.cocoon.components.flow.ContinuationsManager;
022: import java.util.Iterator;
023: import java.util.HashMap;
024: import java.util.ArrayList;
025:
026: /**
027: * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
028: * updated 25-6-2006:
029: * * added support for detecting outdated continuations based on unique continuation attributes
030: */
031: public class ContinuationsManagerImpl extends
032: org.apache.cocoon.components.flow.ContinuationsManagerImpl {
033:
034: public static final String DUPLICATE_CHECK_CONTINUATION_ATTR = "duplicate-check-attr";
035:
036: public ContinuationsManagerImpl() throws Exception {
037: super ();
038: }
039:
040: /**
041: * Remove all continuations which have already expired.
042: */
043: protected void expireContinuations() {
044:
045: long now = 0;
046: if (getLogger().isDebugEnabled()) {
047: now = System.currentTimeMillis();
048:
049: /* Continuations before clean up: */
050: getLogger().debug(
051: "WK: Forest before cleanup: " + forest.size());
052: displayAllContinuations();
053: displayExpireSet();
054:
055: }
056:
057: // Clean up expired continuations
058: int count = 0;
059: WebContinuation wk;
060: Iterator i = expirations.iterator();
061: String attr;
062: HashMap hm = new HashMap();
063: ArrayList outdatedWks = new ArrayList();
064:
065: while (i.hasNext()) {
066: wk = wk = (WebContinuation) i.next();
067:
068: WebContinuationsHolder continuationsHolder = null;
069:
070: attr = (String) wk
071: .getAttribute(DUPLICATE_CHECK_CONTINUATION_ATTR);
072:
073: /*
074: * First check to see whether there are any outdated continuations
075: * by
076: */
077: if (attr != null) {
078:
079: WebContinuation outdatedWk = (WebContinuation) hm
080: .get(attr);
081: if (outdatedWk != null) {
082: // outdated Continuation found, add to removal list
083: outdatedWks.add(outdatedWk);
084: hm.remove(attr);
085: }
086: if (!wk.hasExpired())
087: hm.put(attr, wk);
088: }
089:
090: if (wk.hasExpired()) {
091: i.remove();
092: continuationsHolder = null;
093: if (wk instanceof HolderAwareWebContinuation) {
094: continuationsHolder = ((HolderAwareWebContinuation) wk)
095: .getContinuationsHolder();
096: } else {
097: continuationsHolder = this .continuationsHolder;
098: }
099: removeContinuation(continuationsHolder, wk);
100: count++;
101: }
102: }
103:
104: i = outdatedWks.iterator();
105: while (i.hasNext()) {
106: wk = (WebContinuation) i.next();
107: expirations.remove(wk);
108: continuationsHolder = null;
109: if (wk instanceof HolderAwareWebContinuation) {
110: continuationsHolder = ((HolderAwareWebContinuation) wk)
111: .getContinuationsHolder();
112: } else {
113: continuationsHolder = this .continuationsHolder;
114: }
115: removeContinuation(continuationsHolder, wk);
116: }
117:
118: expirationsSize.setValue(expirations.size());
119:
120: if (getLogger().isDebugEnabled()) {
121: getLogger().debug(
122: "WK Cleaned up " + count
123: + " expired continuations, "
124: + outdatedWks.size()
125: + " outdated continuations in "
126: + (System.currentTimeMillis() - now)
127: + " ms");
128:
129: /* Continuations after clean up: */
130: // getLogger().debug("WK: Forest after cleanup: " + forest.size());
131: // displayAllContinuations();
132: // displayExpireSet();
133: }
134: }
135: }
|