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 Map<String, WikiPage> children = new HashMap<String, WikiPage>();
013: private transient SoftReference cachedData;
014: private transient long cachedDataCreationTime = 0;
015:
016: public CachingPage(String name, WikiPage parent) throws Exception {
017: super (name, parent);
018: addExtention(new VirtualCouplingExtension(this ));
019: }
020:
021: public abstract boolean hasChildPage(String pageName)
022: throws Exception;
023:
024: protected abstract WikiPage createChildPage(String name)
025: throws Exception;
026:
027: protected abstract void loadChildren() throws Exception;
028:
029: protected abstract PageData makePageData() throws Exception;
030:
031: public WikiPage addChildPage(String name) throws Exception {
032: WikiPage page = createChildPage(name);
033: children.put(name, page);
034: return page;
035: }
036:
037: public List getNormalChildren() throws Exception {
038: loadChildren();
039: return getCachedChildren();
040: }
041:
042: public List<WikiPage> getCachedChildren() throws Exception {
043: return new ArrayList<WikiPage>(children.values());
044: }
045:
046: public void removeChildPage(String name) throws Exception {
047: if (hasCachedSubpage(name))
048: children.remove(name);
049: }
050:
051: public WikiPage getNormalChildPage(String name) throws Exception {
052: if (hasCachedSubpage(name) || hasChildPage(name))
053: return (WikiPage) children.get(name);
054: else
055: return null;
056: }
057:
058: protected boolean hasCachedSubpage(String name) {
059: return children.containsKey(name);
060: }
061:
062: public PageData getData() throws Exception {
063: if (cachedDataExpired()) {
064: PageData data = makePageData();
065: setCachedData(data);
066: }
067: return new PageData(getCachedData());
068: }
069:
070: private boolean cachedDataExpired() throws Exception {
071: long now = System.currentTimeMillis();
072: return getCachedData() == null
073: || now >= (cachedDataCreationTime + cacheTime);
074: }
075:
076: public void dumpExpiredCachedData() throws Exception {
077: if (cachedDataExpired()) {
078: cachedData.clear();
079: cachedData = null;
080: }
081: }
082:
083: public VersionInfo commit(PageData data) throws Exception {
084: VersionInfo versionInfo = super .commit(data);
085: setCachedData(makePageData());
086: return versionInfo;
087: }
088:
089: private void setCachedData(PageData data) throws Exception {
090: if (cachedData != null)
091: cachedData.clear();
092: cachedData = new SoftReference<PageData>(data);
093: cachedDataCreationTime = System.currentTimeMillis();
094: }
095:
096: public PageData getCachedData() throws Exception {
097: if (cachedData != null)
098: return (PageData) cachedData.get();
099: else
100: return null;
101: }
102: }
|