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.util.Collections;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.directwebremoting.extend.DownloadManager;
024: import org.directwebremoting.extend.FileGenerator;
025:
026: /**
027: * A {@link DownloadManager} that simply stores the links in-memory.
028: * This implementation has the advantage that it is simple - no disk storage is
029: * required, however in anything but a lightly used system it could cause
030: * significant memory usage.
031: * @author Joe Walker [joe at getahead dot ltd dot uk]
032: */
033: public class InMemoryDownloadManager extends PurgingDownloadManager
034: implements DownloadManager {
035: /* (non-Javadoc)
036: * @see org.directwebremoting.impl.PurgingDownloadManager#putFileGenerator(java.lang.String, org.directwebremoting.extend.DownloadManager.FileGenerator)
037: */
038: @Override
039: protected void putFileGenerator(String id, FileGenerator generator) {
040: synchronized (contentsLock) {
041: contents.put(id, new TimedFileGenerator(generator));
042: }
043: }
044:
045: /* (non-Javadoc)
046: * @see org.directwebremoting.impl.PurgingDownloadManager#getFileGenerator(java.lang.String)
047: */
048: @Override
049: protected FileGenerator getFileGenerator(String id) {
050: synchronized (contentsLock) {
051: TimedFileGenerator generator = contents.remove(id);
052: if (generator == null) {
053: return null;
054: }
055: return generator.fileGenerator;
056: }
057: }
058:
059: /* (non-Javadoc)
060: * @see org.directwebremoting.impl.PurgingDownloadManager#purge()
061: */
062: @Override
063: protected void purge() {
064: long now = System.currentTimeMillis();
065:
066: synchronized (contentsLock) {
067: for (Iterator<Map.Entry<String, TimedFileGenerator>> it = contents
068: .entrySet().iterator(); it.hasNext();) {
069: Map.Entry<String, TimedFileGenerator> entry = it.next();
070:
071: try {
072: if (now > entry.getValue().timeInserted
073: + purgeDownloadsAfter) {
074: it.remove();
075: }
076: } catch (Exception ex) {
077: log.warn("Deletion queue processing error: "
078: + ex.getMessage());
079: }
080: }
081: }
082: }
083:
084: /**
085: * A union of a FileGenerator and the time it was inserted into this manager
086: */
087: protected static class TimedFileGenerator {
088: protected TimedFileGenerator(FileGenerator fileGenerator) {
089: this .fileGenerator = fileGenerator;
090: this .timeInserted = System.currentTimeMillis();
091: }
092:
093: FileGenerator fileGenerator;
094: long timeInserted;
095: }
096:
097: /**
098: * The lock which you must hold to read or write from the list of
099: * {@link FileGenerator}s.
100: */
101: protected Object contentsLock = new Object();
102:
103: /**
104: * The list of files in the system
105: */
106: protected final Map<String, TimedFileGenerator> contents = Collections
107: .synchronizedMap(new HashMap<String, TimedFileGenerator>());
108: }
|