KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > DirectoryCache


1 /*
2  * DirectoryCache.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: DirectoryCache.java,v 1.4 2003/02/11 17:28:03 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 public final class DirectoryCache
28 {
29     private static final int timeout = 1800000; // 30 minutes
30

31     private static DirectoryCache cache;
32
33     private Vector JavaDoc entries = new Vector JavaDoc();
34
35     public static synchronized DirectoryCache getDirectoryCache()
36     {
37         if (cache == null) {
38             cache = new DirectoryCache();
39             IdleThread idleThread = IdleThread.getInstance();
40             if (idleThread != null)
41                 idleThread.maybeAddTask(PruneDirectoryCacheTask.getInstance());
42         }
43         return cache;
44     }
45
46     public synchronized String JavaDoc getListing(File file)
47     {
48         String JavaDoc netPath = file.netPath();
49         for (int i = entries.size(); i-- > 0;) {
50             DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i);
51             if (entry.getFile().netPath().equals(netPath)) {
52                 if (entry.getWhen() + timeout < System.currentTimeMillis()) {
53                     Log.debug("removing cache entry for " + entry.getFile().netPath());
54                     entries.remove(i);
55                     return null;
56                 }
57                 return entry.getListing();
58             }
59         }
60         return null;
61     }
62
63     public synchronized void put(File file, String JavaDoc listing)
64     {
65         String JavaDoc netPath = file.netPath();
66         for (int i = entries.size(); i-- > 0;) {
67             DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i);
68             if (entry.getFile().netPath().equals(netPath)) {
69                 entries.remove(i);
70                 break;
71             }
72         }
73         if (listing != null && listing.length() > 0) {
74             entries.add(new DirectoryCacheEntry(file, listing,
75                 System.currentTimeMillis()));
76         }
77     }
78
79     public synchronized void purge(String JavaDoc hostname)
80     {
81         for (int i = entries.size(); i-- > 0;) {
82             DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i);
83             if (entry.getFile().getHostName().equals(hostname)) {
84                 Log.debug("removing cache entry for " + entry.getFile().netPath());
85                 entries.remove(i);
86             }
87         }
88     }
89
90     public synchronized void purge(File file)
91     {
92         String JavaDoc netPath = file.netPath();
93         for (int i = entries.size(); i-- > 0;) {
94             DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i);
95             if (entry.getFile().netPath().equals(netPath)) {
96                 Log.debug("removing cache entry for " + netPath);
97                 entries.remove(i);
98             }
99         }
100     }
101
102     private static class PruneDirectoryCacheTask extends IdleThreadTask
103     {
104         private static PruneDirectoryCacheTask instance;
105
106         private long lastRun;
107
108         private PruneDirectoryCacheTask()
109         {
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         {
117             if (instance == null)
118                 instance = new PruneDirectoryCacheTask();
119             return instance;
120         }
121
122         private final Runnable JavaDoc runnable = new Runnable JavaDoc() {
123             public void run()
124             {
125                 // Only check every 5 minutes.
126
if (System.currentTimeMillis() - lastRun > 300000) {
127                     long now = System.currentTimeMillis();
128                     synchronized (cache) {
129                         Iterator JavaDoc it = cache.entries.iterator();
130                         while (it.hasNext()) {
131                             DirectoryCacheEntry entry =
132                                 (DirectoryCacheEntry) it.next();
133                             if (entry.getWhen() + timeout < now)
134                                 it.remove();
135                         }
136                     }
137                     lastRun = now;
138                 }
139             }
140         };
141     }
142 }
143
Popular Tags