Instance.init()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / Instance.java
index 733c16cf9a6e8705e1a602ec279e2b8309d6f395..84a583e6635d8ec966c7fb8cf1ef334ecaa0bb9a 100644 (file)
@@ -17,6 +17,8 @@ import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.library.RemoteLibrary;
 import be.nikiroo.utils.Cache;
 import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.Image;
+import be.nikiroo.utils.Proxy;
 import be.nikiroo.utils.TempFiles;
 import be.nikiroo.utils.TraceHandler;
 import be.nikiroo.utils.resources.Bundles;
@@ -40,9 +42,25 @@ public class Instance {
        private static TraceHandler tracer;
        private static TempFiles tempFiles;
 
-       static {
+       private static boolean init;
+
+       /**
+        * Initialise the instance -- if already initialised, nothing will happen.
+        * <p>
+        * Before calling this method, you may call {@link Bundles#getDirectory()}
+        * if wanted.
+        */
+       static public void init() {
+               if (init) {
+                       return;
+               }
+
+               init = true;
+
                // Before we can configure it:
-               tracer = new TraceHandler(true, checkEnv("DEBUG"), checkEnv("DEBUG"));
+               Boolean debug = checkEnv("DEBUG");
+               boolean trace = debug != null && debug;
+               tracer = new TraceHandler(true, trace, trace);
 
                // config dir:
                configDir = getConfigDir();
@@ -52,20 +70,14 @@ public class Instance {
 
                // Most of the rest is dependent upon this:
                createConfigs(configDir, false);
-               
+
                // Proxy support
-               // TODO: include new nikiroo-utils version
-               // Proxy.use(Instance.getConfig().getString(Config.USE_PROXY));
+               Proxy.use(Instance.getConfig().getString(Config.USE_PROXY));
 
                // update tracer:
-               boolean debug = Instance.getConfig()
-                               .getBoolean(Config.DEBUG_ERR, false);
-               boolean trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE,
-                               false);
-
-               if (checkEnv("DEBUG")) {
-                       debug = true;
-                       trace = true;
+               if (debug == null) {
+                       debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
+                       trace = Instance.getConfig().getBoolean(Config.DEBUG_TRACE, false);
                }
 
                tracer = new TraceHandler(true, debug, trace);
@@ -74,7 +86,8 @@ public class Instance {
                remoteDir = new File(configDir, "remote");
                lib = createDefaultLibrary(remoteDir);
 
-               // create cache
+               // create cache and TMP
+               Image.setTemporaryFilesRoot(new File(configDir, "tmp.images"));
                File tmp = getFile(Config.CACHE_DIR);
                if (tmp == null) {
                        // Could have used: System.getProperty("java.io.tmpdir")
@@ -408,7 +421,8 @@ public class Instance {
                        trans.deleteFile(configDir);
                }
 
-               if (checkEnv("NOUTF")) {
+               Boolean noutf = checkEnv("NOUTF");
+               if (noutf != null && noutf) {
                        trans.setUnicode(false);
                        transGui.setUnicode(false);
                }
@@ -441,6 +455,7 @@ public class Instance {
                                                                + getFile(libDir), e));
                        }
                } else {
+                       Exception ex = null;
                        int pos = remoteLib.lastIndexOf(":");
                        if (pos >= 0) {
                                String port = remoteLib.substring(pos + 1).trim();
@@ -459,13 +474,14 @@ public class Instance {
                                                                lib);
 
                                        } catch (Exception e) {
+                                               ex = e;
                                        }
                                }
                        }
 
                        if (lib == null) {
                                tracer.error(new IOException(
-                                               "Cannot create remote library for: " + remoteLib));
+                                               "Cannot create remote library for: " + remoteLib, ex));
                        }
                }
 
@@ -549,7 +565,7 @@ public class Instance {
        private static String getLang() {
                String lang = config.getString(Config.LANG);
 
-               if (lang == null | lang.isEmpty()) {
+               if (lang == null || lang.isEmpty()) {
                        if (System.getenv("LANG") != null
                                        && !System.getenv("LANG").isEmpty()) {
                                lang = System.getenv("LANG");
@@ -571,7 +587,7 @@ public class Instance {
         * 
         * @return TRUE if it is
         */
-       private static boolean checkEnv(String key) {
+       private static Boolean checkEnv(String key) {
                String value = System.getenv(key);
                if (value != null) {
                        value = value.trim().toLowerCase();
@@ -580,8 +596,10 @@ public class Instance {
                                        || "y".equals(value)) {
                                return true;
                        }
+
+                       return false;
                }
 
-               return false;
+               return null;
        }
 }