Version 4.0.0: java.awt dependencies move
[nikiroo-utils.git] / src / be / nikiroo / utils / Cache.java
index 60cd74e961514c5af5957d76018dde0f098f707d..111fc769b9a5ded89c772d0ff2ede551cfcf080c 100644 (file)
@@ -19,6 +19,7 @@ public class Cache {
        private File dir;
        private long tooOldChanging;
        private long tooOldStable;
+       private TraceHandler tracer = new TraceHandler();
 
        /**
         * Create a new {@link Cache} object.
@@ -53,6 +54,29 @@ public class Cache {
                }
        }
 
+       /**
+        * The traces handler for this {@link Cache}.
+        * 
+        * @return the traces handler
+        */
+       public TraceHandler getTraceHandler() {
+               return tracer;
+       }
+
+       /**
+        * The traces handler for this {@link Cache}.
+        * 
+        * @param tracer
+        *            the new traces handler
+        */
+       public void setTraceHandler(TraceHandler tracer) {
+               if (tracer == null) {
+                       tracer = new TraceHandler(false, false, false);
+               }
+
+               this.tracer = tracer;
+       }
+
        /**
         * Check the resource to see if it is in the cache.
         * 
@@ -69,7 +93,7 @@ public class Cache {
         */
        public boolean check(URL url, boolean allowTooOld, boolean stable) {
                File file = getCached(url);
-               if (file.exists()) {
+               if (file.exists() && file.isFile()) {
                        if (allowTooOld || !isOld(file, stable)) {
                                return true;
                        }
@@ -91,24 +115,6 @@ public class Cache {
                return clean(onlyOld, dir);
        }
 
-       /**
-        * Trace information (info/error) generated by this class.
-        * <p>
-        * You can override it if you don't want the default sysout/syserr.
-        * 
-        * @param message
-        *            the message
-        * @param error
-        *            TRUE for error messages, FALSE for information messages
-        */
-       protected void trace(String message, boolean error) {
-               if (error) {
-                       System.err.println(message);
-               } else {
-                       System.out.println(message);
-               }
-       }
-
        /**
         * Clean the cache (delete the cached items) in the given cache directory.
         * 
@@ -130,8 +136,8 @@ public class Cache {
                                        if (file.delete()) {
                                                num++;
                                        } else {
-                                               trace("Cannot delete temporary file: "
-                                                               + file.getAbsolutePath(), true);
+                                               tracer.error("Cannot delete temporary file: "
+                                                               + file.getAbsolutePath());
                                        }
                                }
                        }
@@ -152,9 +158,6 @@ public class Cache {
         *            used to check if the file is too old to keep or not
         * 
         * @return the opened resource if found, NULL if not
-        * 
-        * @throws IOException
-        *             in case of I/O error
         */
        public InputStream load(String uniqueID, boolean allowTooOld, boolean stable) {
                return load(getCached(uniqueID), allowTooOld, stable);
@@ -172,19 +175,15 @@ public class Cache {
         *            used to check if the file is too old to keep or not
         * 
         * @return the opened resource if found, NULL if not
-        * 
-        * @throws IOException
-        *             in case of I/O error
         */
-       public InputStream load(URL url, boolean allowTooOld, boolean stable)
-                       throws IOException {
+       public InputStream load(URL url, boolean allowTooOld, boolean stable) {
                return load(getCached(url), allowTooOld, stable);
        }
 
        /**
         * Open a resource from the cache if it exists.
         * 
-        * @param url
+        * @param cached
         *            the resource to open
         * @param allowTooOld
         *            allow files even if they are considered too old
@@ -193,12 +192,10 @@ public class Cache {
         *            used to check if the file is too old to keep or not
         * 
         * @return the opened resource if found, NULL if not
-        * 
-        * @throws IOException
-        *             in case of I/O error
         */
        private InputStream load(File cached, boolean allowTooOld, boolean stable) {
-               if (cached.exists() && (allowTooOld || !isOld(cached, stable))) {
+               if (cached.exists() && cached.isFile()
+                               && (allowTooOld || !isOld(cached, stable))) {
                        try {
                                return new MarkableFileInputStream(new FileInputStream(cached));
                        } catch (FileNotFoundException e) {
@@ -236,6 +233,8 @@ public class Cache {
         * @param url
         *            the {@link URL} used to locate the cached resource
         * 
+        * @return the actual cache file
+        * 
         * @throws IOException
         *             in case of I/O error
         */
@@ -252,6 +251,8 @@ public class Cache {
         * @param cached
         *            the cached {@link File} to save to
         * 
+        * @return the actual cache file
+        * 
         * @throws IOException
         *             in case of I/O error
         */
@@ -260,6 +261,32 @@ public class Cache {
                return cached;
        }
 
+       /**
+        * Remove the given resource from the cache.
+        * 
+        * @param uniqueID
+        *            a unique ID used to locate the cached resource
+        * 
+        * @return TRUE if it was removed
+        */
+       public boolean remove(String uniqueID) {
+               File cached = getCached(uniqueID);
+               return cached.delete();
+       }
+
+       /**
+        * Remove the given resource from the cache.
+        * 
+        * @param url
+        *            the {@link URL} used to locate the cached resource
+        * 
+        * @return TRUE if it was removed
+        */
+       public boolean remove(URL url) {
+               File cached = getCached(url);
+               return cached.delete();
+       }
+
        /**
         * Check if the {@link File} is too old according to
         * {@link Cache#tooOldChanging}.
@@ -284,8 +311,8 @@ public class Cache {
 
                long time = new Date().getTime() - file.lastModified();
                if (time < 0) {
-                       trace("Timestamp in the future for file: " + file.getAbsolutePath(),
-                                       true);
+                       tracer.error("Timestamp in the future for file: "
+                                       + file.getAbsolutePath());
                }
 
                return time < 0 || time > max;
@@ -306,7 +333,11 @@ public class Cache {
                if (name == null || name.isEmpty()) {
                        // File
                        File file = new File(url.getFile());
-                       subdir = new File(file.getParent().replace("..", "__"));
+                       if (file.getParent() == null) {
+                               subdir = new File("+");
+                       } else {
+                               subdir = new File(file.getParent().replace("..", "__"));
+                       }
                        subdir = new File(dir, allowedChars(subdir.getPath()));
                        name = allowedChars(url.getFile());
                } else {