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 * Create a new {@link Cache} object.
28 * the directory to use as cache
29 * @param hoursChanging
30 * the number of hours after which a cached file that is thought
31 * to change ~often is considered too old (or -1 for
34 * the number of hours after which a cached file that is thought
35 * to change rarely is considered too old (or -1 for
39 * in case of I/O error
41 public Cache(File dir
, int hoursChanging
, int hoursStable
)
44 this.tooOldChanging
= 1000 * 60 * 60 * hoursChanging
;
45 this.tooOldStable
= 1000 * 60 * 60 * hoursStable
;
47 if (dir
!= null && !dir
.exists()) {
51 if (dir
== null || !dir
.exists()) {
52 throw new IOException("Cannot create the cache directory: "
53 + (dir
== null ?
"null" : dir
.getAbsolutePath()));
58 * The traces handler for this {@link Cache}.
60 * @return the traces handler
62 public TraceHandler
getTraceHandler() {
67 * The traces handler for this {@link Cache}.
70 * the new traces handler
72 public void setTraceHandler(TraceHandler tracer
) {
77 * Check the resource to see if it is in the cache.
80 * the resource to check
82 * allow files even if they are considered too old
84 * a stable file (that dones't change too often) -- parameter
85 * used to check if the file is too old to keep or not
87 * @return TRUE if it is
90 public boolean check(URL url
, boolean allowTooOld
, boolean stable
) {
91 File file
= getCached(url
);
93 if (allowTooOld
|| !isOld(file
, stable
)) {
102 * Clean the cache (delete the cached items).
105 * only clean the files that are considered too old for a stable
108 * @return the number of cleaned items
110 public int clean(boolean onlyOld
) {
111 return clean(onlyOld
, dir
);
115 * Clean the cache (delete the cached items) in the given cache directory.
118 * only clean the files that are considered too old for stable
121 * the cache directory to clean
123 * @return the number of cleaned items
125 private int clean(boolean onlyOld
, File cacheDir
) {
127 for (File file
: cacheDir
.listFiles()) {
128 if (file
.isDirectory()) {
129 num
+= clean(onlyOld
, file
);
131 if (!onlyOld
|| isOld(file
, true)) {
135 tracer
.error("Cannot delete temporary file: "
136 + file
.getAbsolutePath());
146 * Open a resource from the cache if it exists.
151 * allow files even if they are considered too old
153 * a stable file (that dones't change too often) -- parameter
154 * used to check if the file is too old to keep or not
156 * @return the opened resource if found, NULL if not
158 public InputStream
load(String uniqueID
, boolean allowTooOld
, boolean stable
) {
159 return load(getCached(uniqueID
), allowTooOld
, stable
);
163 * Open a resource from the cache if it exists.
166 * the resource to open
168 * allow files even if they are considered too old
170 * a stable file (that dones't change too often) -- parameter
171 * used to check if the file is too old to keep or not
173 * @return the opened resource if found, NULL if not
175 public InputStream
load(URL url
, boolean allowTooOld
, boolean stable
) {
176 return load(getCached(url
), allowTooOld
, stable
);
180 * Open a resource from the cache if it exists.
183 * the resource to open
185 * allow files even if they are considered too old
187 * a stable file (that dones't change too often) -- parameter
188 * used to check if the file is too old to keep or not
190 * @return the opened resource if found, NULL if not
192 private InputStream
load(File cached
, boolean allowTooOld
, boolean stable
) {
193 if (cached
.exists() && (allowTooOld
|| !isOld(cached
, stable
))) {
195 return new MarkableFileInputStream(new FileInputStream(cached
));
196 } catch (FileNotFoundException e
) {
205 * Save the given resource to the cache.
210 * a unique ID used to locate the cached resource
212 * @return the resulting {@link File}
214 * @throws IOException
215 * in case of I/O error
217 public File
save(InputStream in
, String uniqueID
) throws IOException
{
218 File cached
= getCached(uniqueID
);
219 cached
.getParentFile().mkdirs();
220 return save(in
, cached
);
224 * Save the given resource to the cache.
229 * the {@link URL} used to locate the cached resource
231 * @return the actual cache file
233 * @throws IOException
234 * in case of I/O error
236 public File
save(InputStream in
, URL url
) throws IOException
{
237 File cached
= getCached(url
);
238 return save(in
, cached
);
242 * Save the given resource to the cache.
247 * the cached {@link File} to save to
249 * @return the actual cache file
251 * @throws IOException
252 * in case of I/O error
254 private File
save(InputStream in
, File cached
) throws IOException
{
255 IOUtils
.write(in
, cached
);
260 * Remove the given resource from the cache.
263 * a unique ID used to locate the cached resource
265 * @return TRUE if it was removed
267 public boolean remove(String uniqueID
) {
268 File cached
= getCached(uniqueID
);
269 return cached
.delete();
273 * Remove the given resource from the cache.
276 * the {@link URL} used to locate the cached resource
278 * @return TRUE if it was removed
280 public boolean remove(URL url
) {
281 File cached
= getCached(url
);
282 return cached
.delete();
286 * Check if the {@link File} is too old according to
287 * {@link Cache#tooOldChanging}.
292 * TRUE to denote stable files, that are not supposed to change
295 * @return TRUE if it is
297 private boolean isOld(File file
, boolean stable
) {
298 long max
= tooOldChanging
;
307 long time
= new Date().getTime() - file
.lastModified();
309 tracer
.error("Timestamp in the future for file: "
310 + file
.getAbsolutePath());
313 return time
< 0 || time
> max
;
317 * Return the associated cache {@link File} from this {@link URL}.
322 * @return the cached {@link File} version of this {@link URL}
324 private File
getCached(URL url
) {
327 String name
= url
.getHost();
328 if (name
== null || name
.isEmpty()) {
330 File file
= new File(url
.getFile());
331 if (file
.getParent() == null) {
332 subdir
= new File("+");
334 subdir
= new File(file
.getParent().replace("..", "__"));
336 subdir
= new File(dir
, allowedChars(subdir
.getPath()));
337 name
= allowedChars(url
.getFile());
340 File subsubDir
= new File(dir
, allowedChars(url
.getHost()));
341 subdir
= new File(subsubDir
, "_" + allowedChars(url
.getPath()));
342 name
= allowedChars("_" + url
.getQuery());
345 File cacheFile
= new File(subdir
, name
);
352 * Get the basic cache resource file corresponding to this unique ID.
354 * Note that you may need to add a sub-directory in some cases.
359 * @return the cached version if present, NULL if not
361 private File
getCached(String uniqueID
) {
362 File file
= new File(dir
, allowedChars(uniqueID
));
363 File subdir
= new File(file
.getParentFile(), "_");
364 return new File(subdir
, file
.getName());
368 * Replace not allowed chars (in a {@link File}) by "_".
371 * the raw {@link String}
373 * @return the sanitised {@link String}
375 private String
allowedChars(String raw
) {
376 return raw
.replace('/', '_').replace(':', '_').replace("\\", "_");