Version 2.2.3: change the trace handler system
authorNiki Roo <niki@nikiroo.be>
Sun, 26 Nov 2017 19:38:04 +0000 (20:38 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 26 Nov 2017 19:38:04 +0000 (20:38 +0100)
VERSION
changelog.md
src/be/nikiroo/utils/Cache.java
src/be/nikiroo/utils/Downloader.java
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/TraceHandler.java [new file with mode: 0644]

diff --git a/VERSION b/VERSION
index b1b25a5ffae43c2f07d222b53240d871e7c1789b..585940699b5b99df6541c819a773ef738985a956 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.2.2
+2.2.3
index fb17b4338a64f1a3cd5bc0256cd6eb2a5808b29f..ab1be9e537f5967bb5f6d99ac7872b915ee5b2ab 100644 (file)
@@ -1,5 +1,10 @@
 # nikiroo-utils
 
+## Version 2.2.3
+
+- Fix in readSmallStream
+- Change traces handling
+
 ## Version 2.2.2
 
 - New method in Cache: manually delete items
index 1e590547f3842070e01d8758023a8a933102b6f1..dcbde74ea4b053ae512c7ed9149114af5f4aa77e 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,25 @@ 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) {
+               this.tracer = tracer;
+       }
+
        /**
         * Check the resource to see if it is in the cache.
         * 
@@ -91,24 +111,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 +132,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());
                                        }
                                }
                        }
@@ -304,8 +306,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;
index 67fd652b306323243365271752eb528f3e6523f2..a8a591a363958412a73eb6ef042d38ba33ae1cf9 100644 (file)
@@ -28,6 +28,7 @@ import java.util.zip.GZIPInputStream;
 public class Downloader {
        private String UA;
        private CookieManager cookies;
+       private TraceHandler tracer = new TraceHandler();
 
        /**
         * Create a new {@link Downloader}.
@@ -46,6 +47,25 @@ public class Downloader {
                CookieHandler.setDefault(cookies);
        }
 
+       /**
+        * 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) {
+               this.tracer = tracer;
+       }
+
        /**
         * Clear all the cookies currently in the jar.
         * <p>
@@ -74,6 +94,10 @@ public class Downloader {
         * 
         * @param url
         *            the {@link URL} to open
+        * @param currentReferer
+        *            the current referer, for websites that needs this info
+        * @param cookiesValues
+        *            the cookies
         * @param postParams
         *            the POST parameters
         * @param getParams
@@ -93,24 +117,6 @@ public class Downloader {
                                getParams, oauth);
        }
 
-       /**
-        * 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);
-               }
-       }
-
        /**
         * Open the given {@link URL} and update the cookies.
         * 
@@ -134,7 +140,7 @@ public class Downloader {
                        Map<String, String> postParams, Map<String, String> getParams,
                        String oauth) throws IOException {
 
-               trace("Download: " + url, false);
+               tracer.trace("Download: " + url);
 
                URLConnection conn = openConnectionWithCookies(url, currentReferer,
                                cookiesValues);
index 35481b2cf0d1576e68814191296b3d8fdafca25d..973a61b0da541362a9a44069555bbe42a5adfb45 100644 (file)
@@ -193,19 +193,19 @@ public class IOUtils {
         *             in case of I/O error
         */
        public static String readSmallStream(InputStream stream) throws IOException {
+               // do NOT close the reader, or the related stream will be closed, too
+               // reader.close();
                BufferedReader reader = new BufferedReader(
                                new InputStreamReader(stream));
-               try {
-                       StringBuilder builder = new StringBuilder();
-                       for (String line = reader.readLine(); line != null; line = reader
-                                       .readLine()) {
-                               builder.append(line);
-                               builder.append("\n");
-                       }
-                       return builder.toString();
-               } finally {
-                       reader.close();
+
+               StringBuilder builder = new StringBuilder();
+               for (String line = reader.readLine(); line != null; line = reader
+                               .readLine()) {
+                       builder.append(line);
+                       builder.append("\n");
                }
+
+               return builder.toString();
        }
 
        /**
diff --git a/src/be/nikiroo/utils/TraceHandler.java b/src/be/nikiroo/utils/TraceHandler.java
new file mode 100644 (file)
index 0000000..5fa0843
--- /dev/null
@@ -0,0 +1,88 @@
+package be.nikiroo.utils;
+
+/**
+ * A handler when a trace message is sent or when a recoverable exception was
+ * caught by the program.
+ * 
+ * @author niki
+ */
+public class TraceHandler {
+       private boolean showErrorDetails;
+       private boolean showTraces;
+
+       /**
+        * Show more details (usually equivalent to the value of DEBUG).
+        * 
+        * @return TRUE or FALSE
+        */
+       public boolean isShowErrorDetails() {
+               return showErrorDetails;
+       }
+
+       /**
+        * Show more details (usually equivalent to the value of DEBUG).
+        * 
+        * @param showErrorDetails
+        *            TRUE or FALSE
+        */
+       public void setShowErrorDetails(boolean showErrorDetails) {
+               this.showErrorDetails = showErrorDetails;
+       }
+
+       /**
+        * Show DEBUG traces.
+        * 
+        * @return TRUE or FALSE
+        */
+       public boolean isShowTraces() {
+               return showTraces;
+       }
+
+       /**
+        * Show DEBUG traces.
+        * 
+        * @param showTraces
+        *            TRUE or FALSE
+        */
+       public void setShowTraces(boolean showTraces) {
+               this.showTraces = showTraces;
+       }
+
+       /**
+        * An exception happened, log it.
+        * 
+        * @param e
+        *            the exception
+        */
+       public void error(Exception e) {
+               if (isShowErrorDetails()) {
+                       e.printStackTrace();
+               } else {
+                       error(e.getMessage());
+               }
+       }
+
+       /**
+        * An error happened, log it.
+        * 
+        * @param message
+        *            the error message
+        */
+       public void error(String message) {
+               System.err.println(message);
+       }
+
+       /**
+        * A trace happened, show it.
+        * <p>
+        * Will only be effective if {@link TraceHandler#isShowTraces()} is true.
+        * 
+        * @param message
+        *            the trace message
+        */
+       public void trace(String message) {
+               if (isShowTraces()) {
+                       System.out.println(message);
+               }
+       }
+}