001: /*
002: * DirectoryCache.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: DirectoryCache.java,v 1.4 2003/02/11 17:28:03 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.Iterator;
025: import java.util.Vector;
026:
027: public final class DirectoryCache {
028: private static final int timeout = 1800000; // 30 minutes
029:
030: private static DirectoryCache cache;
031:
032: private Vector entries = new Vector();
033:
034: public static synchronized DirectoryCache getDirectoryCache() {
035: if (cache == null) {
036: cache = new DirectoryCache();
037: IdleThread idleThread = IdleThread.getInstance();
038: if (idleThread != null)
039: idleThread.maybeAddTask(PruneDirectoryCacheTask
040: .getInstance());
041: }
042: return cache;
043: }
044:
045: public synchronized String getListing(File file) {
046: String netPath = file.netPath();
047: for (int i = entries.size(); i-- > 0;) {
048: DirectoryCacheEntry entry = (DirectoryCacheEntry) entries
049: .get(i);
050: if (entry.getFile().netPath().equals(netPath)) {
051: if (entry.getWhen() + timeout < System
052: .currentTimeMillis()) {
053: Log.debug("removing cache entry for "
054: + entry.getFile().netPath());
055: entries.remove(i);
056: return null;
057: }
058: return entry.getListing();
059: }
060: }
061: return null;
062: }
063:
064: public synchronized void put(File file, String listing) {
065: String netPath = file.netPath();
066: for (int i = entries.size(); i-- > 0;) {
067: DirectoryCacheEntry entry = (DirectoryCacheEntry) entries
068: .get(i);
069: if (entry.getFile().netPath().equals(netPath)) {
070: entries.remove(i);
071: break;
072: }
073: }
074: if (listing != null && listing.length() > 0) {
075: entries.add(new DirectoryCacheEntry(file, listing, System
076: .currentTimeMillis()));
077: }
078: }
079:
080: public synchronized void purge(String hostname) {
081: for (int i = entries.size(); i-- > 0;) {
082: DirectoryCacheEntry entry = (DirectoryCacheEntry) entries
083: .get(i);
084: if (entry.getFile().getHostName().equals(hostname)) {
085: Log.debug("removing cache entry for "
086: + entry.getFile().netPath());
087: entries.remove(i);
088: }
089: }
090: }
091:
092: public synchronized void purge(File file) {
093: String netPath = file.netPath();
094: for (int i = entries.size(); i-- > 0;) {
095: DirectoryCacheEntry entry = (DirectoryCacheEntry) entries
096: .get(i);
097: if (entry.getFile().netPath().equals(netPath)) {
098: Log.debug("removing cache entry for " + netPath);
099: entries.remove(i);
100: }
101: }
102: }
103:
104: private static class PruneDirectoryCacheTask extends IdleThreadTask {
105: private static PruneDirectoryCacheTask instance;
106:
107: private long lastRun;
108:
109: private PruneDirectoryCacheTask() {
110: lastRun = System.currentTimeMillis();
111: setIdle(300000); // User must be idle for 5 minutes.
112: setRunnable(runnable);
113: }
114:
115: private static synchronized PruneDirectoryCacheTask getInstance() {
116: if (instance == null)
117: instance = new PruneDirectoryCacheTask();
118: return instance;
119: }
120:
121: private final Runnable runnable = new Runnable() {
122: public void run() {
123: // Only check every 5 minutes.
124: if (System.currentTimeMillis() - lastRun > 300000) {
125: long now = System.currentTimeMillis();
126: synchronized (cache) {
127: Iterator it = cache.entries.iterator();
128: while (it.hasNext()) {
129: DirectoryCacheEntry entry = (DirectoryCacheEntry) it
130: .next();
131: if (entry.getWhen() + timeout < now)
132: it.remove();
133: }
134: }
135: lastRun = now;
136: }
137: }
138: };
139: }
140: }
|