Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/DocuDirCache.java @ 176:67ff8c7fecb9
Servlet version 1.17b2
- new mapping file for "virtual directories"
- direct file URLs now work without extension (even with wrong ones)
author | robcast |
---|---|
date | Mon, 10 Nov 2003 20:59:00 +0100 |
parents | e743b853efca |
children | afe7ff98bb71 |
rev | line source |
---|---|
176 | 1 /* |
2 * DocuDirCache.java | |
3 * | |
4 * Digital Image Library servlet components | |
5 * | |
6 * Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify it | |
9 * under the terms of the GNU General Public License as published by the Free | |
10 * Software Foundation; either version 2 of the License, or (at your option) | |
11 * any later version. | |
12 * | |
13 * Please read license.txt for the full details. A copy of the GPL may be found | |
14 * at http://www.gnu.org/copyleft/lgpl.html | |
15 * | |
16 * You should have received a copy of the GNU General Public License along with | |
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
18 * Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
86 | 20 * Created on 03.03.2003 |
21 */ | |
22 | |
23 package digilib.io; | |
24 | |
25 import java.io.File; | |
91 | 26 import java.util.HashMap; |
159 | 27 import java.util.Iterator; |
28 import java.util.LinkedList; | |
29 import java.util.List; | |
86 | 30 |
31 /** | |
32 * @author casties | |
33 */ | |
91 | 34 public class DocuDirCache { |
86 | 35 |
159 | 36 /** HashMap of directories */ |
176 | 37 protected HashMap map = null; |
159 | 38 /** names of base directories */ |
86 | 39 private String[] baseDirNames = null; |
159 | 40 /** array of allowed file classes (image/text) */ |
41 private int[] fileClasses = null; | |
42 /** number of files in the whole cache (approximate) */ | |
86 | 43 private long numFiles = 0; |
159 | 44 /** number of cache hits */ |
86 | 45 private long hits = 0; |
159 | 46 /** number of cache misses */ |
86 | 47 private long misses = 0; |
48 | |
176 | 49 /** |
50 * Constructor with array of base directory names and file classes. | |
51 * | |
52 * @param bd | |
53 * base directory names | |
159 | 54 */ |
55 public DocuDirCache(String[] bd, int[] fileClasses) { | |
56 baseDirNames = bd; | |
57 map = new HashMap(); | |
58 this.fileClasses = fileClasses; | |
59 } | |
176 | 60 /** |
61 * Constructor with array of base directory names. | |
62 * | |
63 * @param bd | |
64 * base directory names | |
86 | 65 */ |
66 public DocuDirCache(String[] bd) { | |
67 baseDirNames = bd; | |
91 | 68 map = new HashMap(); |
159 | 69 // default file class is CLASS_IMAGE |
70 fileClasses = new int[1]; | |
71 fileClasses[0] = FileOps.CLASS_IMAGE; | |
91 | 72 } |
73 | |
176 | 74 /** |
75 * The number of directories in the cache. | |
76 * | |
91 | 77 * @return |
78 */ | |
79 public int size() { | |
80 return (map != null) ? map.size() : 0; | |
86 | 81 } |
82 | |
176 | 83 /** |
84 * Add a DocuDirectory to the cache. | |
86 | 85 * |
86 * @param newdir | |
87 */ | |
88 public void put(DocuDirectory newdir) { | |
89 String s = newdir.getDirName(); | |
91 | 90 if (map.containsKey(s)) { |
159 | 91 System.out.println("Baah, duplicate key in DocuDirCache.put!"); |
86 | 92 } else { |
91 | 93 map.put(s, newdir); |
86 | 94 numFiles += newdir.size(); |
95 } | |
96 } | |
97 | |
176 | 98 /** |
99 * Add a directory to the cache and check its parents. | |
151 | 100 * |
101 * @param newDir | |
102 */ | |
103 public void putDir(DocuDirectory newDir) { | |
104 put(newDir); | |
105 String parent = newDir.getParentDirName(); | |
106 if (parent != null) { | |
107 // check the parent in the cache | |
176 | 108 DocuDirectory pd = (DocuDirectory) map.get(parent); |
151 | 109 if (pd == null) { |
110 // the parent is unknown | |
111 pd = new DocuDirectory(parent, this); | |
112 putDir(pd); | |
113 } | |
114 newDir.setParent(pd); | |
115 } | |
116 } | |
117 | |
176 | 118 /** |
119 * Get a list with all children of a directory. | |
159 | 120 * |
176 | 121 * Returns a List of DocuDirectory's. Returns an empty List if the |
122 * directory has no children. If recurse is false then only direct children | |
123 * are returned. | |
159 | 124 * |
125 * @param dirname | |
176 | 126 * @param recurse |
127 * find all children and their children. | |
159 | 128 * @return |
129 */ | |
130 public List getChildren(String dirname, boolean recurse) { | |
131 List l = new LinkedList(); | |
176 | 132 for (Iterator i = map.keySet().iterator(); i.hasNext();) { |
133 DocuDirectory dd = (DocuDirectory) i.next(); | |
159 | 134 if (recurse) { |
135 if (dd.getDirName().startsWith(dirname)) { | |
136 l.add(dd); | |
137 } | |
138 } else { | |
139 if (dd.getParentDirName().equals(dirname)) { | |
140 l.add(dd); | |
141 } | |
142 } | |
143 } | |
144 return l; | |
145 } | |
146 | |
176 | 147 /** |
148 * Returns the ImageFileset with the pathname <code>fn</code> and the | |
159 | 149 * index <code>in</code> and the class <code>fc</code>. |
91 | 150 * |
176 | 151 * If <code>fn</code> is a file then the corresponding Fileset is |
91 | 152 * returned and the index is ignored. |
153 * | |
176 | 154 * @param fn |
155 * digilib pathname | |
156 * @param in | |
157 * file index | |
158 * @return | |
91 | 159 */ |
159 | 160 public DocuDirent getFile(String fn, int in, int fc) { |
86 | 161 DocuDirectory dd; |
162 // file number is 1-based, vector index is 0-based | |
163 int n = in - 1; | |
164 // first, assume fn is a directory and look in the cache | |
91 | 165 dd = (DocuDirectory) map.get(fn); |
166 if (dd == null) { | |
167 // cache miss | |
168 misses++; | |
176 | 169 /* |
170 * see if it's a directory | |
171 */ | |
152 | 172 File f = new File(baseDirNames[0], fn); |
91 | 173 if (f.isDirectory()) { |
151 | 174 dd = new DocuDirectory(fn, this); |
91 | 175 if (dd.isValid()) { |
176 // add to the cache | |
151 | 177 putDir(dd); |
91 | 178 } |
179 } else { | |
176 | 180 /* |
181 * maybe it's a file | |
182 */ | |
183 // get the parent directory string (like we store it in the | |
184 // cache) | |
185 String d = fn.substring(0, fn.lastIndexOf("/")); | |
186 // try it in the cache | |
187 dd = (DocuDirectory) map.get(d); | |
188 if (dd == null) { | |
189 // try to read from disk | |
190 dd = new DocuDirectory(d, this); | |
191 if (dd.isValid()) { | |
192 // add to the cache | |
193 putDir(dd); | |
91 | 194 } else { |
176 | 195 // invalid path |
196 return null; | |
91 | 197 } |
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
198 } else { |
176 | 199 // it was not a real cache miss |
200 misses--; | |
91 | 201 } |
176 | 202 // get the file's index |
203 n = dd.indexOf(f.getName(), fc); | |
91 | 204 } |
205 } else { | |
206 // cache hit | |
207 hits++; | |
208 } | |
209 dd.refresh(); | |
210 if (dd.isValid()) { | |
211 try { | |
159 | 212 return dd.get(n, fc); |
91 | 213 } catch (ArrayIndexOutOfBoundsException e) { |
214 } | |
215 } | |
216 return null; | |
217 } | |
218 | |
176 | 219 /** |
220 * Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
91 | 221 * |
222 * If <code>fn</code> is a file then its parent directory is returned. | |
223 * | |
176 | 224 * @param fn |
225 * digilib pathname | |
91 | 226 * @return |
227 */ | |
228 public DocuDirectory getDirectory(String fn) { | |
229 DocuDirectory dd; | |
230 // first, assume fn is a directory and look in the cache | |
231 dd = (DocuDirectory) map.get(fn); | |
86 | 232 if (dd == null) { |
233 // cache miss | |
234 misses++; | |
235 // see if it's a directory | |
152 | 236 File f = new File(baseDirNames[0], fn); |
86 | 237 if (f.isDirectory()) { |
151 | 238 dd = new DocuDirectory(fn, this); |
86 | 239 if (dd.isValid()) { |
240 // add to the cache | |
151 | 241 putDir(dd); |
86 | 242 } |
243 } else { | |
244 // maybe it's a file | |
245 if (f.canRead()) { | |
246 // try the parent directory in the cache | |
91 | 247 dd = (DocuDirectory) map.get(f.getParent()); |
86 | 248 if (dd == null) { |
249 // try to read from disk | |
151 | 250 dd = new DocuDirectory(f.getParent(), this); |
86 | 251 if (dd.isValid()) { |
252 // add to the cache | |
151 | 253 putDir(dd); |
86 | 254 } else { |
255 // invalid path | |
256 return null; | |
257 } | |
91 | 258 } else { |
259 // not a real cache miss then | |
260 misses--; | |
86 | 261 } |
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
262 } else { |
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
263 // it's not even a file :-( |
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
264 return null; |
86 | 265 } |
266 } | |
267 } else { | |
268 // cache hit | |
269 hits++; | |
270 } | |
271 dd.refresh(); | |
272 if (dd.isValid()) { | |
273 return dd; | |
274 } | |
275 return null; | |
276 } | |
277 | |
278 /** | |
279 * @return String[] | |
280 */ | |
281 public String[] getBaseDirNames() { | |
282 return baseDirNames; | |
283 } | |
284 | |
285 /** | |
286 * @return long | |
287 */ | |
288 public long getNumFiles() { | |
289 return numFiles; | |
290 } | |
291 | |
292 /** | |
293 * Sets the baseDirNames. | |
176 | 294 * |
295 * @param baseDirNames | |
296 * The baseDirNames to set | |
86 | 297 */ |
298 public void setBaseDirNames(String[] baseDirNames) { | |
299 this.baseDirNames = baseDirNames; | |
300 } | |
301 | |
302 /** | |
303 * @return long | |
304 */ | |
305 public long getHits() { | |
306 return hits; | |
307 } | |
308 | |
309 /** | |
310 * @return long | |
311 */ | |
312 public long getMisses() { | |
313 return misses; | |
314 } | |
315 | |
159 | 316 /** |
317 * @return | |
318 */ | |
319 public int[] getFileClasses() { | |
320 return fileClasses; | |
321 } | |
322 | |
323 /** | |
324 * @param fileClasses | |
325 */ | |
326 public void setFileClasses(int[] fileClasses) { | |
327 this.fileClasses = fileClasses; | |
328 } | |
176 | 329 |
86 | 330 } |