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: package fitnesse.wiki;
004:
005: import fitnesse.http.*;
006: import java.util.*;
007: import java.net.*;
008: import java.io.*;
009:
010: public class ProxyPage extends CachingPage implements Serializable {
011:
012: private static final long serialVersionUID = 1L;
013:
014: public static int retrievalCount = 0;
015:
016: private String host;
017:
018: private int hostPort;
019:
020: private WikiPagePath realPath;
021:
022: public ResponseParser parser;
023:
024: private long lastLoadChildrenTime = 0;
025:
026: public ProxyPage(WikiPage original) throws Exception {
027: super (original.getName(), null);
028: realPath = original.getPageCrawler().getFullPath(original);
029:
030: List children = original.getChildren();
031: for (Iterator iterator = children.iterator(); iterator
032: .hasNext();) {
033: ProxyPage child = new ProxyPage((WikiPage) iterator.next());
034: child.parent = this ;
035: this .children.put(child.getName(), child);
036: }
037: }
038:
039: protected ProxyPage(String name, WikiPage parent) throws Exception {
040: super (name, parent);
041: }
042:
043: public ProxyPage(String name, WikiPage parent, String host,
044: int port, WikiPagePath path) throws Exception {
045: super (name, parent);
046: this .host = host;
047: hostPort = port;
048: realPath = path;
049: }
050:
051: public static ProxyPage retrievePage(String urlString)
052: throws Exception {
053: retrievalCount++;
054: URL url = new URL(urlString + "?responder=proxy&type=bones");
055: ProxyPage page = (ProxyPage) getObjectFromUrl(url);
056: page.setTransientValues(url.getHost(), new Date().getTime());
057: int port = url.getPort();
058: page.setHostPort((port == -1) ? 80 : port);
059: page.lastLoadChildrenTime = System.currentTimeMillis();
060: return page;
061: }
062:
063: protected WikiPage createChildPage(String name) throws Exception {
064: WikiPagePath childPath = realPath.copy().addName(name);
065: return new ProxyPage(name, this , host, getHostPort(), childPath);
066: }
067:
068: protected void loadChildren() throws Exception {
069: if (cacheTime <= (System.currentTimeMillis() - lastLoadChildrenTime)) {
070: ProxyPage page = retrievePage(getThisPageUrl());
071: children.clear();
072: for (Iterator iterator = page.children.values().iterator(); iterator
073: .hasNext();) {
074: ProxyPage child = (ProxyPage) iterator.next();
075: child.parent = this ;
076: children.put(child.getName(), child);
077: }
078: lastLoadChildrenTime = System.currentTimeMillis();
079: }
080: }
081:
082: public String getThisPageUrl() {
083: StringBuffer url = new StringBuffer("http://");
084: url.append(host);
085: url.append(":").append(getHostPort());
086: url.append("/").append(PathParser.render(realPath));
087: return url.toString();
088: }
089:
090: public boolean hasChildPage(String pageName) throws Exception {
091: if (children.containsKey(pageName))
092: return true;
093: else {
094: loadChildren();
095: return children.containsKey(pageName);
096: }
097: }
098:
099: public void setTransientValues(String host, long lastLoadTime) {
100: this .host = host;
101: lastLoadChildrenTime = lastLoadTime;
102: for (Iterator i = children.values().iterator(); i.hasNext();) {
103: ProxyPage page = (ProxyPage) i.next();
104: page.setTransientValues(host, lastLoadTime);
105: }
106: }
107:
108: public String getHost() {
109: return host;
110: }
111:
112: public void setHostPort(int port) {
113: hostPort = port;
114: for (Iterator i = children.values().iterator(); i.hasNext();) {
115: ProxyPage page = (ProxyPage) i.next();
116: page.setHostPort(port);
117: }
118: }
119:
120: public int getHostPort() {
121: return hostPort;
122: }
123:
124: public PageData getMeat() throws Exception {
125: return getMeat(null);
126: }
127:
128: public PageData getMeat(String versionName) throws Exception {
129: StringBuffer urlString = new StringBuffer(getThisPageUrl());
130: urlString.append("?responder=proxy&type=meat");
131: if (versionName != null)
132: urlString.append("&version=").append(versionName);
133: URL url = new URL(urlString.toString());
134: PageData data = (PageData) getObjectFromUrl(url);
135: if (data != null)
136: data.setWikiPage(this );
137: return data;
138: }
139:
140: private static Object getObjectFromUrl(URL url) throws Exception {
141: Object obj;
142: InputStream is = url.openStream();
143: ObjectInputStream ois = new ObjectInputStream(is);
144: obj = ois.readObject();
145: ois.close();
146: return obj;
147: }
148:
149: protected PageData makePageData() throws Exception {
150: return getMeat();
151: }
152:
153: public PageData getDataVersion(String versionName) throws Exception {
154: PageData data = getMeat(versionName);
155: if (data == null)
156: throw new NoSuchVersionException("There is no version '"
157: + versionName + "'");
158: return data;
159: }
160:
161: // TODO-MdM these are not needed
162: // We expect this to go away when we do the checkout model
163: protected VersionInfo makeVersion() throws Exception {
164: return null;
165: }
166:
167: protected void doCommit(PageData data) throws Exception {
168: }
169: }
|