001: /*
002: * Copyright 2005 Joe Walker
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 org.directwebremoting.impl;
017:
018: import java.io.IOException;
019: import java.util.concurrent.ScheduledThreadPoolExecutor;
020: import java.util.concurrent.TimeUnit;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.directwebremoting.WebContextFactory;
027: import org.directwebremoting.extend.DownloadManager;
028: import org.directwebremoting.extend.FileGenerator;
029: import org.directwebremoting.util.IdGenerator;
030:
031: /**
032: * A {@link DownloadManager} that simply stores downloads in memory until they
033: * are requested and then removes them.
034: * @author Joe Walker [joe at getahead dot ltd dot uk]
035: */
036: public abstract class PurgingDownloadManager implements DownloadManager {
037: /**
038: * Schedule cache purges
039: */
040: public PurgingDownloadManager() {
041: timer.scheduleWithFixedDelay(downloadPurge, queueSleepTime,
042: queueSleepTime, TimeUnit.MILLISECONDS);
043: }
044:
045: /* (non-Javadoc)
046: * @see org.directwebremoting.extend.DownloadManager#addFile(org.directwebremoting.extend.DownloadManager.FileGenerator)
047: */
048: public String addFile(FileGenerator generator) throws IOException {
049: String id = idGenerator.generateId(16);
050: putFileGenerator(id, generator);
051:
052: HttpServletRequest request = WebContextFactory.get()
053: .getHttpServletRequest();
054: return "'" + request.getContextPath()
055: + request.getServletPath() + downloadHandlerUrl + id
056: + "'";
057: }
058:
059: /* (non-Javadoc)
060: * @see org.directwebremoting.extend.DownloadManager#getFile(java.lang.String)
061: */
062: public FileGenerator getFile(String id) {
063: return getFileGenerator(id);
064: }
065:
066: /**
067: * The URL part which we attach to the downloads.
068: * @param downloadHandlerUrl The URL for this Handler.
069: */
070: public void setDownloadHandlerUrl(String downloadHandlerUrl) {
071: this .downloadHandlerUrl = downloadHandlerUrl;
072: }
073:
074: /**
075: * @param purgeDownloadsAfter the purgeDownloadsAfter to set
076: */
077: public void setPurgeDownloadsAfter(long purgeDownloadsAfter) {
078: this .purgeDownloadsAfter = purgeDownloadsAfter;
079: }
080:
081: /**
082: * Store a {@link FileGenerator} against a given id for
083: * later retrieval.
084: * @param id The id of the given FileGenerator
085: * @param generator The FileGenerator to store against the id
086: */
087: protected abstract void putFileGenerator(String id,
088: FileGenerator generator);
089:
090: /**
091: * Retrieve a FileGenerator given the id that it was stored under
092: * @param id The id to lookup
093: * @return The matching FileGenerator or null if no match was found
094: */
095: protected abstract FileGenerator getFileGenerator(String id);
096:
097: /**
098: * Remove all the stale entries from the cache
099: */
100: protected abstract void purge();
101:
102: /**
103: * Loop over the known downloads removing the ones that are out of date
104: */
105: public class DownloadPurge implements Runnable {
106: /* (non-Javadoc)
107: * @see java.lang.Runnable#run()
108: */
109: public void run() {
110: purge();
111: }
112: }
113:
114: /**
115: * The cron system
116: */
117: protected static ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(
118: 1);
119:
120: /**
121: * Check that the cache of files does not contain out-of-date items
122: */
123: protected DownloadPurge downloadPurge = new DownloadPurge();
124:
125: /**
126: * How often do we run purges on the data?
127: */
128: protected int queueSleepTime = 15000;
129:
130: /**
131: * After some time we will give up waiting for a file to be downloaded.
132: * By default this is 2 minutes. After this time we expect that the user
133: * is no longer waiting and has gone away.
134: */
135: protected long purgeDownloadsAfter = 120000;
136:
137: /**
138: * The URL part which we attach to the downloads.
139: */
140: protected String downloadHandlerUrl;
141:
142: /**
143: * Unique id so we can retrieve downloads when asked
144: */
145: protected IdGenerator idGenerator = new IdGenerator();
146:
147: /**
148: * The log stream
149: */
150: protected static final Log log = LogFactory
151: .getLog(PurgingDownloadManager.class);
152: }
|