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
)) {
139 if (!cached
.delete()) {
140 tracer
.error("Cannot delete temporary file: "
141 + cached
.getAbsolutePath());
152 * Clean the cache (delete the cached items).
155 * only clean the files that are considered too old for a stable
158 * @return the number of cleaned items
160 public int clean(boolean onlyOld
) {
161 return clean(onlyOld
, dir
);
165 * Clean the cache (delete the cached items) in the given cache directory.
168 * only clean the files that are considered too old for stable
171 * the cache directory to clean
173 * @return the number of cleaned items
175 private int clean(boolean onlyOld
, File cacheDir
) {
177 for (File file
: cacheDir
.listFiles()) {
178 if (file
.isDirectory()) {
179 num
+= clean(onlyOld
, file
);
181 if (!onlyOld
|| isOld(file
, true)) {
185 tracer
.error("Cannot delete temporary file: "
186 + file
.getAbsolutePath());
196 * Open a resource from the cache if it exists.
201 * allow files even if they are considered too old
203 * a stable file (that dones't change too often) -- parameter
204 * used to check if the file is too old to keep or not
206 * @return the opened resource if found, NULL if not
208 public InputStream
load(String uniqueID
, boolean allowTooOld
, boolean stable
) {
209 return load(getCached(uniqueID
), allowTooOld
, stable
);
213 * Open a resource from the cache if it exists.
216 * the resource to open
218 * allow files even if they are considered too old
220 * a stable file (that dones't change too often) -- parameter
221 * used to check if the file is too old to keep or not
223 * @return the opened resource if found, NULL if not
225 public InputStream
load(URL url
, boolean allowTooOld
, boolean stable
) {
226 return load(getCached(url
), allowTooOld
, stable
);
230 * Open a resource from the cache if it exists.
233 * the resource to open
235 * allow files even if they are considered too old
237 * a stable file (that dones't change too often) -- parameter
238 * used to check if the file is too old to keep or not
240 * @return the opened resource if found, NULL if not
242 private InputStream
load(File cached
, boolean allowTooOld
, boolean stable
) {
243 if (cached
.exists() && cached
.isFile()
244 && (allowTooOld
|| !isOld(cached
, stable
))) {
246 return new MarkableFileInputStream(new FileInputStream(cached
));
247 } catch (FileNotFoundException e
) {
256 * Save the given resource to the cache.
261 * a unique ID used to locate the cached resource
263 * @throws IOException
264 * in case of I/O error
266 public void save(InputStream in
, String uniqueID
) throws IOException
{
267 File cached
= getCached(uniqueID
);
268 cached
.getParentFile().mkdirs();
273 * Save the given resource to the cache.
278 * the {@link URL} used to locate the cached resource
280 * @throws IOException
281 * in case of I/O error
283 public void save(InputStream in
, URL url
) throws IOException
{
284 File cached
= getCached(url
);
289 * Save the given resource to the cache.
294 * the cached {@link File} to save to
296 * @throws IOException
297 * in case of I/O error
299 private void save(InputStream in
, File cached
) throws IOException
{
300 IOUtils
.write(in
, cached
);
304 * Remove the given resource from the cache.
307 * a unique ID used to locate the cached resource
309 * @return TRUE if it was removed
311 public boolean remove(String uniqueID
) {
312 File cached
= getCached(uniqueID
);
313 return cached
.delete();
317 * Remove the given resource from the cache.
320 * the {@link URL} used to locate the cached resource
322 * @return TRUE if it was removed
324 public boolean remove(URL url
) {
325 File cached
= getCached(url
);
326 return cached
.delete();
330 * Check if the {@link File} is too old according to
331 * {@link Cache#tooOldChanging}.
336 * TRUE to denote stable files, that are not supposed to change
339 * @return TRUE if it is
341 private boolean isOld(File file
, boolean stable
) {
342 long max
= tooOldChanging
;
351 long time
= new Date().getTime() - file
.lastModified();
353 tracer
.error("Timestamp in the future for file: "
354 + file
.getAbsolutePath());
357 return time
< 0 || time
> max
;
361 * Return the associated cache {@link File} from this {@link URL}.
366 * @return the cached {@link File} version of this {@link URL}
368 private File
getCached(URL url
) {
371 String name
= url
.getHost();
372 if (name
== null || name
.isEmpty()) {
374 File file
= new File(url
.getFile());
375 if (file
.getParent() == null) {
376 subdir
= new File("+");
378 subdir
= new File(file
.getParent().replace("..", "__"));
380 subdir
= new File(dir
, allowedChars(subdir
.getPath()));
381 name
= allowedChars(url
.getFile());
384 File subsubDir
= new File(dir
, allowedChars(url
.getHost()));
385 subdir
= new File(subsubDir
, "_" + allowedChars(url
.getPath()));
386 name
= allowedChars("_" + url
.getQuery());
389 File cacheFile
= new File(subdir
, name
);
396 * Get the basic cache resource file corresponding to this unique ID.
398 * Note that you may need to add a sub-directory in some cases.
403 * @return the cached version if present, NULL if not
405 private File
getCached(String uniqueID
) {
406 File file
= new File(dir
, allowedChars(uniqueID
));
407 File subdir
= new File(file
.getParentFile(), "_");
408 return new File(subdir
, file
.getName());
412 * Replace not allowed chars (in a {@link File}) by "_".
415 * the raw {@link String}
417 * @return the sanitised {@link String}
419 private String
allowedChars(String raw
) {
420 return raw
.replace('/', '_').replace(':', '_').replace("\\", "_");