1 package be
.nikiroo
.utils
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.FileNotFoundException
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
12 * A generic cache system, with special support for {@link URL}s.
14 * This cache also manages timeout information.
20 private long tooOldChanging
;
21 private long tooOldStable
;
22 private TraceHandler tracer
= new TraceHandler();
25 * Only for inheritance.
31 * Create a new {@link Cache} object.
34 * the directory to use as cache
35 * @param hoursChanging
36 * the number of hours after which a cached file that is thought
37 * to change ~often is considered too old (or -1 for
40 * the number of hours after which a cached file that is thought
41 * to change rarely is considered too old (or -1 for
45 * in case of I/O error
47 public Cache(File dir
, int hoursChanging
, int hoursStable
)
50 this.tooOldChanging
= 1000 * 60 * 60 * hoursChanging
;
51 this.tooOldStable
= 1000 * 60 * 60 * hoursStable
;
53 if (dir
!= null && !dir
.exists()) {
57 if (dir
== null || !dir
.exists()) {
58 throw new IOException("Cannot create the cache directory: "
59 + (dir
== null ?
"null" : dir
.getAbsolutePath()));
64 * The traces handler for this {@link Cache}.
66 * @return the traces handler
68 public TraceHandler
getTraceHandler() {
73 * The traces handler for this {@link Cache}.
76 * the new traces handler
78 public void setTraceHandler(TraceHandler tracer
) {
80 tracer
= new TraceHandler(false, false, false);
87 * Check the resource to see if it is in the cache.
90 * the resource to check
92 * allow files even if they are considered too old
94 * a stable file (that dones't change too often) -- parameter
95 * used to check if the file is too old to keep or not
97 * @return TRUE if it is
100 public boolean check(String uniqueID
, boolean allowTooOld
, boolean stable
) {
101 return check(getCached(uniqueID
), allowTooOld
, stable
);
105 * Check the resource to see if it is in the cache.
108 * the resource to check
110 * allow files even if they are considered too old
112 * a stable file (that dones't change too often) -- parameter
113 * used to check if the file is too old to keep or not
115 * @return TRUE if it is
118 public boolean check(URL url
, boolean allowTooOld
, boolean stable
) {
119 return check(getCached(url
), allowTooOld
, stable
);
123 * Check the resource to see if it is in the cache.
126 * the resource to check
128 * allow files even if they are considered too old
130 * a stable file (that dones't change too often) -- parameter
131 * used to check if the file is too old to keep or not
133 * @return TRUE if it is
136 private boolean check(File cached
, boolean allowTooOld
, boolean stable
) {
137 if (cached
.exists() && cached
.isFile()) {
138 if (allowTooOld
|| !isOld(cached
, stable
)) {
147 * Clean the cache (delete the cached items).
150 * only clean the files that are considered too old for a stable
153 * @return the number of cleaned items
155 public int clean(boolean onlyOld
) {
156 return clean(onlyOld
, dir
);
160 * Clean the cache (delete the cached items) in the given cache directory.
163 * only clean the files that are considered too old for stable
166 * the cache directory to clean
168 * @return the number of cleaned items
170 private int clean(boolean onlyOld
, File cacheDir
) {
172 for (File file
: cacheDir
.listFiles()) {
173 if (file
.isDirectory()) {
174 num
+= clean(onlyOld
, file
);
176 if (!onlyOld
|| isOld(file
, true)) {
180 tracer
.error("Cannot delete temporary file: "
181 + file
.getAbsolutePath());
191 * Open a resource from the cache if it exists.
196 * allow files even if they are considered too old
198 * a stable file (that dones't change too often) -- parameter
199 * used to check if the file is too old to keep or not
201 * @return the opened resource if found, NULL if not
203 public InputStream
load(String uniqueID
, boolean allowTooOld
, boolean stable
) {
204 return load(getCached(uniqueID
), allowTooOld
, stable
);
208 * Open a resource from the cache if it exists.
211 * the resource to open
213 * allow files even if they are considered too old
215 * a stable file (that dones't change too often) -- parameter
216 * used to check if the file is too old to keep or not
218 * @return the opened resource if found, NULL if not
220 public InputStream
load(URL url
, boolean allowTooOld
, boolean stable
) {
221 return load(getCached(url
), allowTooOld
, stable
);
225 * Open a resource from the cache if it exists.
228 * the resource to open
230 * allow files even if they are considered too old
232 * a stable file (that dones't change too often) -- parameter
233 * used to check if the file is too old to keep or not
235 * @return the opened resource if found, NULL if not
237 private InputStream
load(File cached
, boolean allowTooOld
, boolean stable
) {
238 if (cached
.exists() && cached
.isFile()
239 && (allowTooOld
|| !isOld(cached
, stable
))) {
241 return new MarkableFileInputStream(new FileInputStream(cached
));
242 } catch (FileNotFoundException e
) {
251 * Save the given resource to the cache.
256 * a unique ID used to locate the cached resource
258 * @throws IOException
259 * in case of I/O error
261 public void save(InputStream in
, String uniqueID
) throws IOException
{
262 File cached
= getCached(uniqueID
);
263 cached
.getParentFile().mkdirs();
268 * Save the given resource to the cache.
273 * the {@link URL} used to locate the cached resource
275 * @throws IOException
276 * in case of I/O error
278 public void save(InputStream in
, URL url
) throws IOException
{
279 File cached
= getCached(url
);
284 * Save the given resource to the cache.
289 * the cached {@link File} to save to
291 * @throws IOException
292 * in case of I/O error
294 private void save(InputStream in
, File cached
) throws IOException
{
295 IOUtils
.write(in
, cached
);
299 * Remove the given resource from the cache.
302 * a unique ID used to locate the cached resource
304 * @return TRUE if it was removed
306 public boolean remove(String uniqueID
) {
307 File cached
= getCached(uniqueID
);
308 return cached
.delete();
312 * Remove the given resource from the cache.
315 * the {@link URL} used to locate the cached resource
317 * @return TRUE if it was removed
319 public boolean remove(URL url
) {
320 File cached
= getCached(url
);
321 return cached
.delete();
325 * Check if the {@link File} is too old according to
326 * {@link Cache#tooOldChanging}.
331 * TRUE to denote stable files, that are not supposed to change
334 * @return TRUE if it is
336 private boolean isOld(File file
, boolean stable
) {
337 long max
= tooOldChanging
;
346 long time
= new Date().getTime() - file
.lastModified();
348 tracer
.error("Timestamp in the future for file: "
349 + file
.getAbsolutePath());
352 return time
< 0 || time
> max
;
356 * Return the associated cache {@link File} from this {@link URL}.
361 * @return the cached {@link File} version of this {@link URL}
363 private File
getCached(URL url
) {
366 String name
= url
.getHost();
367 if (name
== null || name
.isEmpty()) {
369 File file
= new File(url
.getFile());
370 if (file
.getParent() == null) {
371 subdir
= new File("+");
373 subdir
= new File(file
.getParent().replace("..", "__"));
375 subdir
= new File(dir
, allowedChars(subdir
.getPath()));
376 name
= allowedChars(url
.getFile());
379 File subsubDir
= new File(dir
, allowedChars(url
.getHost()));
380 subdir
= new File(subsubDir
, "_" + allowedChars(url
.getPath()));
381 name
= allowedChars("_" + url
.getQuery());
384 File cacheFile
= new File(subdir
, name
);
391 * Get the basic cache resource file corresponding to this unique ID.
393 * Note that you may need to add a sub-directory in some cases.
398 * @return the cached version if present, NULL if not
400 private File
getCached(String uniqueID
) {
401 File file
= new File(dir
, allowedChars(uniqueID
));
402 File subdir
= new File(file
.getParentFile(), "_");
403 return new File(subdir
, file
.getName());
407 * Replace not allowed chars (in a {@link File}) by "_".
410 * the raw {@link String}
412 * @return the sanitised {@link String}
414 private String
allowedChars(String raw
) {
415 return raw
.replace('/', '_').replace(':', '_').replace("\\", "_");