X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FCache.java;h=2d334780ae0c7925655a5317b3aa46c81e5aecf4;hb=2b08c849bd54a0799ddf251f602a421d4696005e;hp=3c919cb4d2f76b4083ef05177e1315b84320ea9e;hpb=3da878124135ff033f42d19d8733891b09e953cd;p=jalview.git diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index 3c919cb..2d33478 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -20,8 +20,9 @@ */ package jalview.bin; +import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; import jalview.datamodel.PDBEntry; -import jalview.gui.UserDefinedColours; +import jalview.gui.Preferences; import jalview.schemes.ColourSchemeLoader; import jalview.schemes.ColourSchemes; import jalview.schemes.UserColourScheme; @@ -207,8 +208,26 @@ import org.apache.log4j.SimpleLayout; * @author $author$ * @version $Revision$ */ -public class Cache +public class Cache implements ApplicationSingletonI { + + private Cache() + { + // private singleton + } + + /** + * In Java, this will be a static field instance, which will be + * application-specific; in JavaScript it will be an applet-specific instance + * tied to the applet's ThreadGroup. + * + * @return + */ + private static Cache getInstance() + { + return (Cache) ApplicationSingletonProvider.getInstance(Cache.class); + } + /** * property giving log4j level for CASTOR loggers */ @@ -265,7 +284,9 @@ public class Cache public static Logger log; /** Jalview Properties */ - public static Properties applicationProperties = new Properties() + // BH 2019.05.08 was static + @SuppressWarnings("serial") + private Properties applicationProperties = new Properties() { // override results in properties output in alphabetical order @Override @@ -276,10 +297,14 @@ public class Cache }; /** Default file is ~/.jalview_properties */ - static String propertiesFile; + private String propertiesFile; - private static boolean propsAreReadOnly = /** @j2sNative true || */ - false; + /** + * flag to possibly allow properties to be written to a property file + */ + private boolean propsAreReadOnly = Platform.isJS(); + + private final static String JS_PROPERTY_PREFIX = "jalview_"; public static void initLogger() { @@ -302,14 +327,14 @@ public class Cache jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview"); laxis.setLevel(Level.toLevel( - Cache.getDefault("logs.Axis.Level", Level.INFO.toString()))); - lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level", - Level.INFO.toString()))); + getDefault("logs.Axis.Level", Level.INFO.toString()))); + lcastor.setLevel(Level.toLevel( + getDefault("logs.Castor.Level", Level.INFO.toString()))); lcastor = Logger.getLogger("org.exolab.castor.xml"); - lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level", - Level.INFO.toString()))); + lcastor.setLevel(Level.toLevel( + getDefault("logs.Castor.Level", Level.INFO.toString()))); // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller"); - // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level", + // lcastor.setLevel(Level.toLevel(getDefault("logs.Castor.Level", // Level.INFO.toString()))); jalview.bin.Cache.log.setLevel(Level.toLevel(Cache .getDefault("logs.Jalview.level", Level.INFO.toString()))); @@ -331,50 +356,67 @@ public class Cache */ public static void loadProperties(String propsFile) { + getInstance().loadPropertiesImpl(propsFile); + } + + private void loadPropertiesImpl(String propsFile) + { propertiesFile = propsFile; - if (propsFile == null) - { - propertiesFile = System.getProperty("user.home") + File.separatorChar - + ".jalview_properties"; - } - else + if (!propsAreReadOnly) { - // don't corrupt the file we've been given. - propsAreReadOnly = true; + // Java only + if (propsFile == null) + { + propertiesFile = System.getProperty("user.home") + + File.separatorChar + ".jalview_properties"; + } + else + { + // don't corrupt the file we've been given. + propsAreReadOnly = true; + } } - try + if (propertiesFile == null) + { // BH 2019 read properties from the Info object associated with this + // applet + Platform.readInfoProperties(JS_PROPERTY_PREFIX, + applicationProperties); + } + else { - InputStream fis; try { - fis = new java.net.URL(propertiesFile).openStream(); - System.out.println( - "Loading jalview properties from : " + propertiesFile); - System.out.println( - "Disabling Jalview writing to user's local properties file."); - propsAreReadOnly = true; + InputStream fis; + try + { + fis = new java.net.URL(propertiesFile).openStream(); + System.out.println( + "Loading jalview properties from : " + propertiesFile); + System.out.println( + "Disabling Jalview writing to user's local properties file."); + propsAreReadOnly = true; + + } catch (Exception ex) + { + fis = null; + } + if (fis == null) + { + fis = new FileInputStream(propertiesFile); + } + applicationProperties.clear(); + applicationProperties.load(fis); + // remove any old build properties + + deleteBuildProperties(); + fis.close(); } catch (Exception ex) { - fis = null; - } - if (fis == null) - { - fis = new FileInputStream(propertiesFile); + System.out.println("Error reading properties file: " + ex); } - applicationProperties.clear(); - applicationProperties.load(fis); - - // remove any old build properties - - deleteBuildProperties(); - fis.close(); - } catch (Exception ex) - { - System.out.println("Error reading properties file: " + ex); } - if (getDefault("USE_PROXY", false)) { String proxyServer = getDefault("PROXY_SERVER", ""), @@ -388,59 +430,57 @@ public class Cache } // LOAD THE AUTHORS FROM THE authors.props file - boolean ignore = Platform.isJS(); - if (!ignore) + String authorDetails = (Platform.isJS() ? null + : "jar:".concat(Cache.class.getProtectionDomain() + .getCodeSource().getLocation().toString() + .concat("!/authors.props"))); + try { - String authorDetails = "jar:" - .concat(Cache.class.getProtectionDomain().getCodeSource() - .getLocation().toString().concat("!/authors.props")); - - java.net.URL localJarFileURL = new java.net.URL(authorDetails); - - InputStream in = localJarFileURL.openStream(); - applicationProperties.load(in); - in.close(); - + if (authorDetails != null) + { + java.net.URL localJarFileURL = new java.net.URL(authorDetails); + InputStream in = localJarFileURL.openStream(); + applicationProperties.load(in); + in.close(); + } } catch (Exception ex) { System.out.println("Error reading author details: " + ex); - ignore = true; + authorDetails = null; } - - if (ignore) { - applicationProperties.remove("AUTHORS"); - applicationProperties.remove("AUTHORFNAMES"); - applicationProperties.remove("YEAR"); + if (authorDetails == null) + { + applicationProperties.remove("AUTHORS"); + applicationProperties.remove("AUTHORFNAMES"); + applicationProperties.remove("YEAR"); } - + // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW - // BH 2019.01.25 switching to Platform.isJS() - ignore = Platform.isJS(); - if (!ignore) - try + String buildDetails = (Platform.isJS() ? null + : "jar:".concat(Cache.class.getProtectionDomain() + .getCodeSource().getLocation().toString() + .concat("!/.build_properties"))); + if (buildDetails != null) { - String buildDetails = "jar:".concat(Cache.class.getProtectionDomain() - .getCodeSource().getLocation().toString() - .concat("!/.build_properties")); - - java.net.URL localJarFileURL = new java.net.URL(buildDetails); - - InputStream in = localJarFileURL.openStream(); - applicationProperties.load(in); - in.close(); - } catch (Exception ex) - { - System.out.println("Error reading build details: " + ex); - ignore = true; + try + { + java.net.URL localJarFileURL = new java.net.URL(buildDetails); + InputStream in = localJarFileURL.openStream(); + applicationProperties.load(in); + in.close(); + } catch (Exception ex) + { + System.out.println("Error reading build details: " + ex); + buildDetails = null; + } } - - if (ignore) { - applicationProperties.remove("VERSION"); + if (buildDetails == null) + { + applicationProperties.remove("VERSION"); } - String jnlpVersion = System.getProperty("jalview.version"); String codeVersion = getProperty("VERSION"); String codeInstallation = getProperty("INSTALLATION"); @@ -457,18 +497,17 @@ public class Cache } new BuildDetails(codeVersion, null, codeInstallation); - SiftsSettings - .setMapWithSifts(Cache.getDefault("MAP_WITH_SIFTS", false)); + SiftsSettings.setMapWithSifts(getDefault("MAP_WITH_SIFTS", false)); SiftsSettings.setSiftDownloadDirectory(jalview.bin.Cache .getDefault("sifts_download_dir", DEFAULT_SIFTS_DOWNLOAD_DIR)); SiftsSettings.setFailSafePIDThreshold( - jalview.bin.Cache.getDefault("sifts_fail_safe_pid_threshold", + getDefault("sifts_fail_safe_pid_threshold", DEFAULT_FAIL_SAFE_PID_THRESHOLD)); SiftsSettings.setCacheThresholdInDays( - jalview.bin.Cache.getDefault("sifts_cache_threshold_in_days", + getDefault("sifts_cache_threshold_in_days", DEFAULT_CACHE_THRESHOLD_IN_DAYS)); IdOrgSettings.setUrl(getDefault("ID_ORG_HOSTURL", @@ -483,13 +522,12 @@ public class Cache StructureImportSettings .setDefaultPDBFileParser(DEFAULT_PDB_FILE_PARSER); // StructureImportSettings - // .setDefaultPDBFileParser(jalview.bin.Cache.getDefault( + // .setDefaultPDBFileParser(jalview.bin.getDefault( // "DEFAULT_PDB_FILE_PARSER", DEFAULT_PDB_FILE_PARSER)); // jnlpVersion will be null if we're using InstallAnywhere // Dont do this check if running in headless mode - if (jnlpVersion == null && getDefault("VERSION_CHECK", true) - && (System.getProperty("java.awt.headless") == null || System - .getProperty("java.awt.headless").equals("false"))) + if (jnlpVersion == null && !Jalview.isHeadlessMode() + && getDefault("VERSION_CHECK", true)) { class VersionChecker extends Thread @@ -560,13 +598,10 @@ public class Cache setProperty("VERSION", codeVersion); // LOAD USERDEFINED COLOURS - jalview.bin.Cache - .initUserColourSchemes(getProperty("USER_DEFINED_COLOURS")); - jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER", - false); + initUserColourSchemes(getProperty(Preferences.USER_DEFINED_COLOURS)); } - private static void deleteBuildProperties() + private void deleteBuildProperties() { applicationProperties.remove("LATEST_VERSION"); applicationProperties.remove("VERSION"); @@ -588,7 +623,7 @@ public class Cache */ public static String getProperty(String key) { - return applicationProperties.getProperty(key); + return getInstance().applicationProperties.getProperty(key); } /** @@ -647,11 +682,21 @@ public class Cache */ public static Object setProperty(String key, String obj) { + return getInstance().setPropertyImpl(key, obj, true); + } + + public static void setPropertyNoSave(String key, String obj) + { + getInstance().setPropertyImpl(key, obj, false); + } + + private Object setPropertyImpl(String key, String obj, boolean andSave) + { Object oldValue = null; try { oldValue = applicationProperties.setProperty(key, obj); - if (!propsAreReadOnly) + if (andSave && !propsAreReadOnly && propertiesFile != null) { FileOutputStream out = new FileOutputStream(propertiesFile); applicationProperties.store(out, "---JalviewX Properties File---"); @@ -668,18 +713,36 @@ public class Cache /** * remove the specified property from the jalview properties file * - * @param string + * @param key */ - public static void removeProperty(String string) + public static void removeProperty(String key) + { + getInstance().removePropertyImpl(key, true); + } + + public static void removePropertyNoSave(String key) + { + getInstance().removePropertyImpl(key, false); + } + + private void removePropertyImpl(String key, boolean andSave) + { + applicationProperties.remove(key); + if (andSave) + { + savePropertiesImpl(); + } + } + + public static void saveProperties() { - applicationProperties.remove(string); - saveProperties(); + getInstance().savePropertiesImpl(); } /** * save the properties to the jalview properties path */ - public static void saveProperties() + private void savePropertiesImpl() { if (!propsAreReadOnly) { @@ -695,10 +758,17 @@ public class Cache } } + private final static int UNTESTED = -1; + + private final static int TRUE = 1; + + private final static int FALSE = 0; + /** * internal vamsas class discovery state */ - private static int vamsasJarsArePresent = -1; + private static int vamsasJarsArePresent = (Platform.isJS() ? FALSE + : UNTESTED); /** * Searches for vamsas client classes on class path. @@ -707,7 +777,7 @@ public class Cache */ public static boolean vamsasJarsPresent() { - if (vamsasJarsArePresent == -1) + if (vamsasJarsArePresent == UNTESTED) { try { @@ -716,7 +786,7 @@ public class Cache { jalview.bin.Cache.log.debug( "Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)"); - vamsasJarsArePresent = 1; + vamsasJarsArePresent = TRUE; Logger lvclient = Logger.getLogger("uk.ac.vamsas"); lvclient.setLevel(Level.toLevel(Cache .getDefault("logs.Vamsas.Level", Level.INFO.toString()))); @@ -727,17 +797,18 @@ public class Cache } } catch (Exception e) { - vamsasJarsArePresent = 0; + vamsasJarsArePresent = FALSE; jalview.bin.Cache.log.debug("Vamsas Classes are not present"); } } - return (vamsasJarsArePresent > 0); + return (vamsasJarsArePresent == TRUE); } /** * internal vamsas class discovery state */ - private static int groovyJarsArePresent = -1; + private static int groovyJarsArePresent = (Platform.isJS() ? FALSE + : UNTESTED); /** * Searches for vamsas client classes on class path. @@ -746,7 +817,7 @@ public class Cache */ public static boolean groovyJarsPresent() { - if (groovyJarsArePresent == -1) + if (groovyJarsArePresent == UNTESTED) { try { @@ -755,7 +826,7 @@ public class Cache { jalview.bin.Cache.log.debug( "Found Groovy (groovy.lang.GroovyObject can be loaded)"); - groovyJarsArePresent = 1; + groovyJarsArePresent = TRUE; Logger lgclient = Logger.getLogger("groovy"); lgclient.setLevel(Level.toLevel(Cache .getDefault("logs.Groovy.Level", Level.INFO.toString()))); @@ -766,15 +837,15 @@ public class Cache } } catch (Error e) { - groovyJarsArePresent = 0; + groovyJarsArePresent = FALSE; jalview.bin.Cache.log.debug("Groovy Classes are not present", e); } catch (Exception e) { - groovyJarsArePresent = 0; + groovyJarsArePresent = FALSE; jalview.bin.Cache.log.debug("Groovy Classes are not present"); } } - return (groovyJarsArePresent > 0); + return (groovyJarsArePresent == TRUE); } /** @@ -783,15 +854,18 @@ public class Cache */ protected static Object tracker = null; - protected static Class trackerfocus = null; + protected static Class trackerfocus = null; - protected static Class jgoogleanalyticstracker = null; + protected static Class jgoogleanalyticstracker = null; /** * Initialise the google tracker if it is not done already. */ public static void initGoogleTracker() { + + // TODO: SwingJS JavaScript tracker? + if (tracker == null) { if (jgoogleanalyticstracker == null) @@ -826,8 +900,7 @@ public class Cache .newInstance(new Object[] { "Jalview Desktop", (vrs = jalview.bin.Cache.getProperty("VERSION") + "_" - + jalview.bin.Cache.getDefault("BUILD_DATE", - "unknown")), + + getDefault("BUILD_DATE", "unknown")), "UA-9060947-1" }); jgoogleanalyticstracker .getMethod("trackAsynchronously", new Class[] @@ -1015,11 +1088,11 @@ public class Cache } if (value == null || value.trim().length() < 1) { - Cache.applicationProperties.remove(propName); + getInstance().applicationProperties.remove(propName); } else { - Cache.applicationProperties.setProperty(propName, value); + getInstance().applicationProperties.setProperty(propName, value); } } @@ -1064,29 +1137,15 @@ public class Cache { if (coloursFound.toString().length() > 1) { - setProperty(UserDefinedColours.USER_DEFINED_COLOURS, + setProperty(Preferences.USER_DEFINED_COLOURS, coloursFound.toString()); } else { - applicationProperties - .remove(UserDefinedColours.USER_DEFINED_COLOURS); + getInstance().applicationProperties + .remove(Preferences.USER_DEFINED_COLOURS); } } } - /** - * Add a known domain that implements access-control-allow-origin:* bh 2018 - * - * @param defaultUniprotDomain - */ - public static void addJ2SDirectDatabaseCall(String domain) - { - - /** - * @j2sNative - * - * J2S.addDirectDatabaseCall(domain); - */ - } }