X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=Instance.java;h=e0a072714a08a35b87f4342a0b4046c0abe0894c;hb=af6e65daddff2d602017013f498f989fefbb6be9;hp=f48d05b7d4cd84994eeb7e5e238018e99317afbd;hpb=371a36bd5f5d0e46038491ecfdd18653fe4d6f61;p=fanfix.git diff --git a/Instance.java b/Instance.java index f48d05b..e0a0727 100644 --- a/Instance.java +++ b/Instance.java @@ -15,6 +15,7 @@ import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix.library.CacheLibrary; import be.nikiroo.fanfix.library.LocalLibrary; import be.nikiroo.fanfix.library.RemoteLibrary; +import be.nikiroo.fanfix.library.WebLibrary; import be.nikiroo.utils.Cache; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.Image; @@ -48,27 +49,40 @@ public class Instance { /** * Initialise the instance -- if already initialised, nothing will happen. *
- * Before calling this method, you may call {@link Bundles#setDirectory(String)} - * if wanted. + * Before calling this method, you may call + * {@link Bundles#setDirectory(String)} if wanted. + *
+ * Note that this method will honour some environment variables, the 3 most + * important ones probably being: + *
- * Before calling this method, you may call {@link Bundles#setDirectory(String)} - * if wanted. + * Before calling this method, you may call + * {@link Bundles#setDirectory(String)} if wanted. *
- * Note: forcing the initialisation can be dangerous, so make sure to only make - * it under controlled circumstances -- for instance, at the start of the - * program, you could call {@link Instance#init()}, change some settings because - * you want to force those settings (it will also forbid users to change them!) - * and then call {@link Instance#init(boolean)} with force set to TRUE. + * Note: forcing the initialisation can be dangerous, so make sure to only + * make it under controlled circumstances -- for instance, at the start of + * the program, you could call {@link Instance#init()}, change some settings + * because you want to force those settings (it will also forbid users to + * change them!) and then call {@link Instance#init(boolean)} with + * force set to TRUE. * - * @param force force the initialisation even if already initialised + * @param force + * force the initialisation even if already initialised */ static public void init(boolean force) { synchronized (instancelock) { @@ -84,7 +98,8 @@ public class Instance { *
* Usually for DEBUG/Test purposes. * - * @param instance the actual Instance to use + * @param instance + * the actual Instance to use */ static public void init(Instance instance) { Instance.instance = instance; @@ -102,8 +117,8 @@ public class Instance { /** * Actually initialise the instance. *
- * Before calling this method, you may call {@link Bundles#setDirectory(String)} - * if wanted. + * Before calling this method, you may call + * {@link Bundles#setDirectory(String)} if wanted. */ protected Instance() { // Before we can configure it: @@ -136,10 +151,7 @@ public class Instance { lib = createDefaultLibrary(remoteDir); // create cache and TMP - File tmp = getFile(Config.CACHE_DIR, new File(configDir, "tmp")); - if (!tmp.isAbsolute()) { - tmp = new File(configDir, tmp.getPath()); - } + File tmp = getFile(Config.CACHE_DIR, configDir, "tmp"); Image.setTemporaryFilesRoot(new File(tmp.getParent(), "tmp.images")); String ua = config.getString(Config.NETWORK_USER_AGENT, ""); @@ -148,22 +160,24 @@ public class Instance { int hoursLarge = config.getInteger(Config.CACHE_MAX_TIME_STABLE, 0); cache = new DataLoader(tmp, ua, hours, hoursLarge); } catch (IOException e) { - tracer.error(new IOException("Cannot create cache (will continue without cache)", e)); + tracer.error(new IOException( + "Cannot create cache (will continue without cache)", e)); cache = new DataLoader(ua); } cache.setTraceHandler(tracer); // readerTmp / coverDir - readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, new File(configDir, "tmp-reader")); - - coverDir = getFile(Config.DEFAULT_COVERS_DIR, new File(configDir, "covers")); + readerTmp = getFile(UiConfig.CACHE_DIR_LOCAL_READER, configDir, + "tmp-reader"); + coverDir = getFile(Config.DEFAULT_COVERS_DIR, configDir, "covers"); coverDir.mkdirs(); try { tempFiles = new TempFiles("fanfix"); } catch (IOException e) { - tracer.error(new IOException("Cannot create temporary directory", e)); + tracer.error( + new IOException("Cannot create temporary directory", e)); } } @@ -181,7 +195,8 @@ public class Instance { /** * The traces handler for this {@link Cache}. * - * @param tracer the new traces handler or NULL + * @param tracer + * the new traces handler or NULL */ public void setTraceHandler(TraceHandler tracer) { if (tracer == null) { @@ -213,7 +228,8 @@ public class Instance { /** * Reset the configuration. * - * @param resetTrans also reset the translation files + * @param resetTrans + * also reset the translation files */ public void resetConfig(boolean resetTrans) { String dir = Bundles.getDirectory(); @@ -277,9 +293,9 @@ public class Instance { } /** - * Get the (unique) {@link LocalLibrary} for the program. + * Get the (unique) {@link BasicLibrary} for the program. * - * @return the {@link LocalLibrary} + * @return the {@link BasicLibrary} */ public BasicLibrary getLibrary() { if (lib == null) { @@ -289,6 +305,18 @@ public class Instance { return lib; } + /** + * Change the default {@link BasicLibrary} for this program. + *
+ * Be careful. + * + * @param lib + * the new {@link BasicLibrary} + */ + public void setLibrary(BasicLibrary lib) { + this.lib = lib; + } + /** * Return the directory where to look for default cover pages. * @@ -311,7 +339,8 @@ public class Instance { * Return the directory where to store temporary files for the remote * {@link LocalLibrary}. * - * @param host the remote for this host + * @param host + * the remote for this host * * @return the directory */ @@ -323,8 +352,10 @@ public class Instance { * Return the directory where to store temporary files for the remote * {@link LocalLibrary}. * - * @param remoteDir the base remote directory - * @param host the remote for this host + * @param remoteDir + * the base remote directory + * @param host + * the remote for this host * * @return the directory */ @@ -332,6 +363,11 @@ public class Instance { remoteDir.mkdirs(); if (host != null) { + host = host.replace("fanfix://", ""); + host = host.replace("http://", ""); + host = host.replace("https://", ""); + host = host.replaceAll("[^a-zA-Z0-9=+.-]", "_"); + return new File(remoteDir, host); } @@ -345,10 +381,13 @@ public class Instance { */ public boolean isVersionCheckNeeded() { try { - long wait = config.getInteger(Config.NETWORK_UPDATE_INTERVAL, 0) * 24 * 60 * 60 * 1000; + long wait = config.getInteger(Config.NETWORK_UPDATE_INTERVAL, 0) + * 24 * 60 * 60 * 1000; if (wait >= 0) { - String lastUpString = IOUtils.readSmallFile(new File(configDir, "LAST_UPDATE")); - long delay = new Date().getTime() - Long.parseLong(lastUpString); + String lastUpString = IOUtils + .readSmallFile(new File(configDir, "LAST_UPDATE")); + long delay = new Date().getTime() + - Long.parseLong(lastUpString); if (delay > wait) { return true; } @@ -368,7 +407,8 @@ public class Instance { */ public void setVersionChecked() { try { - IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", Long.toString(new Date().getTime())); + IOUtils.writeSmallFile(new File(configDir), "LAST_UPDATE", + Long.toString(new Date().getTime())); } catch (IOException e) { tracer.error(e); } @@ -386,8 +426,8 @@ public class Instance { } /** - * The configuration directory (will check, in order of preference, the system - * properties, the environment and then defaults to + * The configuration directory (will check, in order of preference, the + * system properties, the environment and then defaults to * {@link Instance#getHome()}/.fanfix). * * @return the config directory @@ -411,9 +451,11 @@ public class Instance { * {@link Instance#uiconfig}, {@link Instance#trans} and * {@link Instance#transGui}). * - * @param configDir the directory where to find the configuration files - * @param refresh TRUE to reset the configuration files from the default - * included ones + * @param configDir + * the directory where to find the configuration files + * @param refresh + * TRUE to reset the configuration files from the default + * included ones */ private void createConfigs(String configDir, boolean refresh) { if (!refresh) { @@ -457,41 +499,56 @@ public class Instance { /** * Create the default library as specified by the config. * - * @param remoteDir the base remote directory if needed + * @param remoteDir + * the base remote directory if needed * * @return the default {@link BasicLibrary} */ private BasicLibrary createDefaultLibrary(File remoteDir) { BasicLibrary lib = null; - boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, false); - + boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, + false); if (useRemote) { String host = null; int port = -1; try { - host = config.getString(Config.REMOTE_LIBRARY_HOST); + host = config.getString(Config.REMOTE_LIBRARY_HOST, + "fanfix://localhost"); port = config.getInteger(Config.REMOTE_LIBRARY_PORT, -1); String key = config.getString(Config.REMOTE_LIBRARY_KEY); + if (!host.startsWith("http://") && !host.startsWith("https://") + && !host.startsWith("fanfix://")) { + host = "fanfix://" + host; + } + tracer.trace("Selecting remote library " + host + ":" + port); - lib = new RemoteLibrary(key, host, port); - lib = new CacheLibrary(getRemoteDir(remoteDir, host), lib, uiconfig); + + if (host.startsWith("fanfix://")) { + lib = new RemoteLibrary(key, host, port); + } else { + lib = new WebLibrary(key, host, port); + } + + lib = new CacheLibrary(getRemoteDir(remoteDir, host), lib, + uiconfig); } catch (Exception e) { - tracer.error(new IOException("Cannot create remote library for: " + host + ":" + port, e)); + tracer.error( + new IOException("Cannot create remote library for: " + + host + ":" + port, e)); } } else { String libDir = System.getenv("BOOKS_DIR"); if (libDir == null || libDir.isEmpty()) { - libDir = config.getString(Config.LIBRARY_DIR, "$HOME/Books"); - if (!getFile(libDir).isAbsolute()) { - libDir = new File(configDir, libDir).getPath(); - } + libDir = getFile(Config.LIBRARY_DIR, configDir, "$HOME/Books") + .getPath(); } try { - lib = new LocalLibrary(getFile(libDir), config); + lib = new LocalLibrary(new File(libDir), config); } catch (Exception e) { - tracer.error(new IOException("Cannot create library for directory: " + getFile(libDir), e)); + tracer.error(new IOException( + "Cannot create library for directory: " + libDir, e)); } } @@ -501,39 +558,52 @@ public class Instance { /** * Return a path, but support the special $HOME variable. * - * @param id the key for the path, which may contain "$HOME" - * @param def the default value if none + * @param id + * the key for the path, which may contain "$HOME" + * @param configDir + * the directory to use as base if not absolute + * @param def + * the default value if none (will be configDir-rooted if needed) * @return the path, with expanded "$HOME" if needed */ - protected File getFile(Config id, File def) { - String path = config.getString(id, def.getPath()); - return getFile(path); + protected File getFile(Config id, String configDir, String def) { + String path = config.getString(id, def); + return getFile(path, configDir); } /** * Return a path, but support the special $HOME variable. * - * @param id the key for the path, which may contain "$HOME" - * @param def the default value if none + * @param id + * the key for the path, which may contain "$HOME" + * @param configDir + * the directory to use as base if not absolute + * @param def + * the default value if none (will be configDir-rooted if needed) * @return the path, with expanded "$HOME" if needed */ - protected File getFile(UiConfig id, File def) { - String path = uiconfig.getString(id, def.getPath()); - return getFile(path); + protected File getFile(UiConfig id, String configDir, String def) { + String path = uiconfig.getString(id, def); + return getFile(path, configDir); } /** * Return a path, but support the special $HOME variable. * - * @param path the path, which may contain "$HOME" + * @param path + * the path, which may contain "$HOME" + * @param configDir + * the directory to use as base if not absolute * @return the path, with expanded "$HOME" if needed */ - protected File getFile(String path) { + protected File getFile(String path, String configDir) { File file = null; if (path != null && !path.isEmpty()) { path = path.replace('/', File.separatorChar); if (path.contains("$HOME")) { path = path.replace("$HOME", getHome()); + } else if (!path.startsWith("/")) { + path = new File(configDir, path).getPath(); } file = new File(path); @@ -547,8 +617,8 @@ public class Instance { * properties. *
* The environment variable is tested first. Then, the custom property - * "fanfix.home" is tried, followed by the usual "user.home" then "java.io.tmp" - * if nothing else is found. + * "fanfix.home" is tried, followed by the usual "user.home" then + * "java.io.tmp" if nothing else is found. * * @return the home */ @@ -595,7 +665,8 @@ public class Instance { String lang = config.getString(Config.LANG); if (lang == null || lang.isEmpty()) { - if (System.getenv("LANG") != null && !System.getenv("LANG").isEmpty()) { + if (System.getenv("LANG") != null + && !System.getenv("LANG").isEmpty()) { lang = System.getenv("LANG"); } } @@ -610,7 +681,8 @@ public class Instance { /** * Check that the given environment variable is "enabled". * - * @param key the variable to check + * @param key + * the variable to check * * @return TRUE if it is */ @@ -618,7 +690,8 @@ public class Instance { String value = System.getenv(key); if (value != null) { value = value.trim().toLowerCase(); - if ("yes".equals(value) || "true".equals(value) || "on".equals(value) || "1".equals(value) + if ("yes".equals(value) || "true".equals(value) + || "on".equals(value) || "1".equals(value) || "y".equals(value)) { return true; }