001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003:
004: package fitnesse.wiki;
005:
006: import java.util.*;
007: import java.lang.ref.SoftReference;
008:
009: public abstract class CachingPage extends CommitingPage {
010: public static int cacheTime = 3000;
011:
012: protected HashMap children = new HashMap();
013:
014: private transient SoftReference cachedData;
015:
016: private transient long cachedDataCreationTime = 0;
017:
018: public CachingPage(String name, WikiPage parent) throws Exception {
019: super (name, parent);
020: addExtention(new VirtualCouplingExtension(this ));
021: }
022:
023: public abstract boolean hasChildPage(String pageName)
024: throws Exception;
025:
026: protected abstract WikiPage createChildPage(String name)
027: throws Exception;
028:
029: protected abstract void loadChildren() throws Exception;
030:
031: protected abstract PageData makePageData() throws Exception;
032:
033: public WikiPage addChildPage(String name) throws Exception {
034: WikiPage page = createChildPage(name);
035: children.put(name, page);
036: return page;
037: }
038:
039: public List getNormalChildren() throws Exception {
040: loadChildren();
041: return getCachedChildren();
042: }
043:
044: public List getCachedChildren() throws Exception {
045: return new ArrayList(children.values());
046: }
047:
048: public void removeChildPage(String name) throws Exception {
049: if (hasCachedSubpage(name))
050: children.remove(name);
051: }
052:
053: public WikiPage getNormalChildPage(String name) throws Exception {
054: if (hasCachedSubpage(name) || hasChildPage(name))
055: return (WikiPage) children.get(name);
056: else
057: return null;
058: }
059:
060: protected boolean hasCachedSubpage(String name) {
061: return children.containsKey(name);
062: }
063:
064: public PageData getData() throws Exception {
065: if (cachedDataExpired()) {
066: PageData data = makePageData();
067: setCachedData(data);
068: }
069: PageData pageData = new PageData(getCachedData());
070: return pageData;
071: }
072:
073: private boolean cachedDataExpired() throws Exception {
074: long now = System.currentTimeMillis();
075: boolean expired = getCachedData() == null
076: || now >= (cachedDataCreationTime + cacheTime);
077: return expired;
078: }
079:
080: public void dumpExpiredCachedData() throws Exception {
081: if (cachedDataExpired()) {
082: cachedData.clear();
083: cachedData = null;
084: }
085: }
086:
087: public VersionInfo commit(PageData data) throws Exception {
088: VersionInfo versionInfo = super .commit(data);
089: setCachedData(makePageData());
090: return versionInfo;
091: }
092:
093: private void setCachedData(PageData data) throws Exception {
094: if (cachedData != null)
095: cachedData.clear();
096: cachedData = new SoftReference(data);
097: cachedDataCreationTime = System.currentTimeMillis();
098: }
099:
100: public PageData getCachedData() throws Exception {
101: if (cachedData != null)
102: return (PageData) cachedData.get();
103: else
104: return null;
105: }
106: }
|