Mercurial > hg > digilib-old
comparison common/src/main/java/digilib/io/DocuDirCache.java @ 903:7779b37d1d05
refactored into maven modules per servlet type.
can build servlet-api 2.3 and 3.0 via profile now!
author | robcast |
---|---|
date | Tue, 26 Apr 2011 20:24:31 +0200 |
parents | servlet/src/main/java/digilib/io/DocuDirCache.java@ba1eb2d821a2 |
children | 7bcc6765c209 |
comparison
equal
deleted
inserted
replaced
902:89ba3ffcf552 | 903:7779b37d1d05 |
---|---|
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 * | |
20 * Created on 03.03.2003 | |
21 */ | |
22 | |
23 package digilib.io; | |
24 | |
25 import java.io.File; | |
26 import java.util.LinkedList; | |
27 import java.util.List; | |
28 import java.util.concurrent.ConcurrentHashMap; | |
29 import java.util.concurrent.ConcurrentMap; | |
30 | |
31 import org.apache.log4j.Logger; | |
32 | |
33 import digilib.io.FileOps.FileClass; | |
34 import digilib.servlet.DigilibConfiguration; | |
35 | |
36 /** | |
37 * @author casties | |
38 */ | |
39 public class DocuDirCache { | |
40 | |
41 /** general logger for this class */ | |
42 Logger logger = Logger.getLogger(this.getClass()); | |
43 | |
44 /** HashMap of directories */ | |
45 ConcurrentMap<String, DocuDirectory> map = new ConcurrentHashMap<String, DocuDirectory>(); | |
46 | |
47 /** names of base directories */ | |
48 String[] baseDirNames = null; | |
49 | |
50 /** array of allowed file classes (image/text) */ | |
51 private FileClass[] fileClasses = null; | |
52 | |
53 /** number of files in the whole cache (approximate) */ | |
54 long numFiles = 0; | |
55 | |
56 /** number of cache hits */ | |
57 long hits = 0; | |
58 | |
59 /** number of cache misses */ | |
60 long misses = 0; | |
61 | |
62 /** the root directory element */ | |
63 public static Directory ROOT = null; | |
64 | |
65 /** | |
66 * Constructor with array of base directory names and file classes. | |
67 * | |
68 * @param bd | |
69 * base directory names | |
70 */ | |
71 public DocuDirCache(String[] bd, FileClass[] fcs, | |
72 DigilibConfiguration dlConfig) { | |
73 baseDirNames = bd; | |
74 this.fileClasses = fcs; | |
75 } | |
76 | |
77 /** | |
78 * Constructor with array of base directory names. | |
79 * | |
80 * @param bd | |
81 * base directory names | |
82 */ | |
83 public DocuDirCache(String[] bd) { | |
84 baseDirNames = bd; | |
85 // default file class is CLASS_IMAGE | |
86 fileClasses = new FileClass[] { FileClass.IMAGE }; | |
87 } | |
88 | |
89 /** | |
90 * The number of directories in the cache. | |
91 * | |
92 * @return | |
93 */ | |
94 public int size() { | |
95 return (map != null) ? map.size() : 0; | |
96 } | |
97 | |
98 /** | |
99 * Add a DocuDirectory to the cache. | |
100 * Always returns the correct Object from the cache, | |
101 * either newdir one or another one. | |
102 * | |
103 * @param newdir | |
104 * @return dir | |
105 */ | |
106 public DocuDirectory put(DocuDirectory newdir) { | |
107 String s = newdir.getDirName(); | |
108 logger.debug("DocuDirCache.put for "+s+" in "+this); | |
109 DocuDirectory olddir = map.putIfAbsent(s, newdir); | |
110 if (olddir != null) { | |
111 logger.warn("Duplicate key in DocuDirCache.put -- ignoring!"); | |
112 return olddir; | |
113 } | |
114 numFiles += newdir.size(); | |
115 return newdir; | |
116 } | |
117 | |
118 /** | |
119 * Add a directory to the cache and check its parents. | |
120 * Always returns the correct Object from the cache, | |
121 * either newDir one or another one. | |
122 * | |
123 * @param newDir | |
124 * @return dir | |
125 */ | |
126 public DocuDirectory putDir(DocuDirectory newDir) { | |
127 DocuDirectory dd = put(newDir); | |
128 if (dd.getParent() == null) { | |
129 // no parent link yet | |
130 String parent = FileOps.parent(newDir.getDirName()); | |
131 if (parent != "") { | |
132 // check the parent in the cache | |
133 DocuDirectory pd = map.get(parent); | |
134 if (pd == null) { | |
135 // the parent is unknown | |
136 pd = new DocuDirectory(parent, this); | |
137 pd = putDir(pd); | |
138 } | |
139 newDir.setParent(pd); | |
140 } | |
141 } | |
142 return dd; | |
143 } | |
144 | |
145 /** | |
146 * Get a list with all children of a directory. | |
147 * | |
148 * Returns a List of DocuDirectory's. Returns an empty List if the directory | |
149 * has no children. If recurse is false then only direct children are | |
150 * returned. | |
151 * | |
152 * @param dirname | |
153 * @param recurse | |
154 * find all children and their children. | |
155 * @return | |
156 */ | |
157 public List<DocuDirectory> getChildren(String dirname, boolean recurse) { | |
158 List<DocuDirectory> l = new LinkedList<DocuDirectory>(); | |
159 for (DocuDirectory dd: map.values()) { | |
160 if (recurse) { | |
161 if (dd.getDirName().startsWith(dirname)) { | |
162 l.add(dd); | |
163 } | |
164 } else { | |
165 if (FileOps.parent(dd.getDirName()).equals(dirname)) { | |
166 l.add(dd); | |
167 } | |
168 } | |
169 } | |
170 return l; | |
171 } | |
172 | |
173 /** | |
174 * Returns the DocuDirent with the pathname <code>fn</code> and the index | |
175 * <code>in</code> and the class <code>fc</code>. | |
176 * | |
177 * If <code>fn</code> is a file then the corresponding DocuDirent is | |
178 * returned and the index is ignored. | |
179 * | |
180 * @param fn | |
181 * digilib pathname | |
182 * @param in | |
183 * file index | |
184 * @param fc | |
185 * file class | |
186 * @return | |
187 */ | |
188 public DocuDirent getFile(String fn, int in, FileClass fc) { | |
189 DocuDirectory dd; | |
190 // file number is 1-based, vector index is 0-based | |
191 int n = in - 1; | |
192 // first, assume fn is a directory and look in the cache | |
193 dd = map.get(fn); | |
194 if (dd == null) { | |
195 // cache miss | |
196 misses++; | |
197 /* | |
198 * see if fn is a directory | |
199 */ | |
200 File f = new File(baseDirNames[0], fn); | |
201 if (f.isDirectory()) { | |
202 // logger.debug(fn + " is a dir"); | |
203 dd = new DocuDirectory(fn, this); | |
204 if (dd.isValid()) { | |
205 // add to the cache | |
206 dd = putDir(dd); | |
207 } | |
208 } else { | |
209 /* | |
210 * maybe it's a file | |
211 */ | |
212 // get the parent directory string (like we store it in the | |
213 // cache) | |
214 String d = FileOps.parent(fn); | |
215 // try it in the cache | |
216 // logger.debug(fn + " is a file in dir " + d); | |
217 dd = map.get(d); | |
218 if (dd == null) { | |
219 // try to read from disk | |
220 dd = new DocuDirectory(d, this); | |
221 if (dd.isValid()) { | |
222 // add to the cache | |
223 // logger.debug(dd + " is valid"); | |
224 dd = putDir(dd); | |
225 } else { | |
226 // invalid path | |
227 return null; | |
228 } | |
229 } else { | |
230 // it was not a real cache miss | |
231 misses--; | |
232 } | |
233 // get the file's index | |
234 n = dd.indexOf(f.getName(), fc); | |
235 } | |
236 } else { | |
237 // cache hit | |
238 hits++; | |
239 } | |
240 dd.refresh(); | |
241 if (dd.isValid()) { | |
242 try { | |
243 return dd.get(n, fc); | |
244 } catch (IndexOutOfBoundsException e) { | |
245 // logger.debug(fn + " not found in directory"); | |
246 } | |
247 } | |
248 return null; | |
249 } | |
250 | |
251 /** | |
252 * Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
253 * | |
254 * If <code>fn</code> is a file then its parent directory is returned. | |
255 * | |
256 * @param fn | |
257 * digilib pathname | |
258 * @return | |
259 */ | |
260 public DocuDirectory getDirectory(String fn) { | |
261 DocuDirectory dd; | |
262 // first, assume fn is a directory and look in the cache | |
263 dd = map.get(fn); | |
264 if (dd == null) { | |
265 // cache miss | |
266 misses++; | |
267 // see if it's a directory | |
268 File f = new File(baseDirNames[0], fn); | |
269 if (f.isDirectory()) { | |
270 dd = new DocuDirectory(fn, this); | |
271 if (dd.isValid()) { | |
272 // add to the cache | |
273 dd = putDir(dd); | |
274 } | |
275 } else { | |
276 // maybe it's a file | |
277 if (f.canRead()) { | |
278 // try the parent directory in the cache | |
279 dd = map.get(f.getParent()); | |
280 if (dd == null) { | |
281 // try to read from disk | |
282 dd = new DocuDirectory(f.getParent(), this); | |
283 if (dd.isValid()) { | |
284 // add to the cache | |
285 dd = putDir(dd); | |
286 } else { | |
287 // invalid path | |
288 return null; | |
289 } | |
290 } else { | |
291 // not a real cache miss then | |
292 misses--; | |
293 } | |
294 } else { | |
295 // it's not even a file :-( | |
296 return null; | |
297 } | |
298 } | |
299 } else { | |
300 // cache hit | |
301 hits++; | |
302 } | |
303 dd.refresh(); | |
304 if (dd.isValid()) { | |
305 return dd; | |
306 } | |
307 return null; | |
308 } | |
309 | |
310 /** | |
311 * @return String[] | |
312 */ | |
313 public String[] getBaseDirNames() { | |
314 return baseDirNames; | |
315 } | |
316 | |
317 /** | |
318 * @return long | |
319 */ | |
320 public long getNumFiles() { | |
321 return numFiles; | |
322 } | |
323 | |
324 /** | |
325 * Sets the baseDirNames. | |
326 * | |
327 * @param baseDirNames | |
328 * The baseDirNames to set | |
329 */ | |
330 public void setBaseDirNames(String[] baseDirNames) { | |
331 this.baseDirNames = baseDirNames; | |
332 } | |
333 | |
334 /** | |
335 * @return long | |
336 */ | |
337 public long getHits() { | |
338 return hits; | |
339 } | |
340 | |
341 /** | |
342 * @return long | |
343 */ | |
344 public long getMisses() { | |
345 return misses; | |
346 } | |
347 | |
348 /** | |
349 * @return | |
350 */ | |
351 public FileClass[] getFileClasses() { | |
352 return fileClasses; | |
353 } | |
354 | |
355 /** | |
356 * @param fileClasses | |
357 */ | |
358 public void setFileClasses(FileClass[] fileClasses) { | |
359 this.fileClasses = fileClasses; | |
360 } | |
361 | |
362 } |