JAL-3210 Barebones gradle/buildship/eclipse. See README
[jalview.git] / src / jalview / bin / Cache.java
index b15c317..b6ff253 100755 (executable)
@@ -20,9 +20,8 @@
  */
 package jalview.bin;
 
-import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
 import jalview.datamodel.PDBEntry;
-import jalview.gui.Preferences;
+import jalview.gui.UserDefinedColours;
 import jalview.schemes.ColourSchemeLoader;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.UserColourScheme;
@@ -33,7 +32,6 @@ import jalview.util.Platform;
 import jalview.ws.sifts.SiftsSettings;
 
 import java.awt.Color;
-import java.awt.Dimension;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
@@ -209,26 +207,8 @@ import org.apache.log4j.SimpleLayout;
  * @author $author$
  * @version $Revision$
  */
-public class Cache implements ApplicationSingletonI
+public class Cache
 {
-
-  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
    */
@@ -285,9 +265,7 @@ public class Cache implements ApplicationSingletonI
   public static Logger log;
 
   /** Jalview Properties */
-  // BH 2019.05.08 was static
-  @SuppressWarnings("serial")
-  private Properties applicationProperties = new Properties()
+  public static Properties applicationProperties = new Properties()
   {
     // override results in properties output in alphabetical order
     @Override
@@ -298,12 +276,9 @@ public class Cache implements ApplicationSingletonI
   };
 
   /** Default file is ~/.jalview_properties */
-  private String propertiesFile;
+  static String propertiesFile;
 
-  /**
-   * flag to possibly allow properties to be written to a property file
-   */
-  private boolean propsAreReadOnly = Platform.isJS();
+  private static boolean propsAreReadOnly = Platform.isJS();
 
   private final static String JS_PROPERTY_PREFIX = "jalview_";
 
@@ -328,14 +303,14 @@ public class Cache implements ApplicationSingletonI
       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
 
       laxis.setLevel(Level.toLevel(
-              getDefault("logs.Axis.Level", Level.INFO.toString())));
-      lcastor.setLevel(Level.toLevel(
-              getDefault("logs.Castor.Level", Level.INFO.toString())));
+              Cache.getDefault("logs.Axis.Level", Level.INFO.toString())));
+      lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
+              Level.INFO.toString())));
       lcastor = Logger.getLogger("org.exolab.castor.xml");
-      lcastor.setLevel(Level.toLevel(
-              getDefault("logs.Castor.Level", Level.INFO.toString())));
+      lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
+              Level.INFO.toString())));
       // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller");
-      // lcastor.setLevel(Level.toLevel(getDefault("logs.Castor.Level",
+      // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
       // Level.INFO.toString())));
       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache
               .getDefault("logs.Jalview.level", Level.INFO.toString())));
@@ -357,30 +332,20 @@ public class Cache implements ApplicationSingletonI
    */
   public static void loadProperties(String propsFile)
   {
-    getInstance().loadPropertiesImpl(propsFile);
-  }
-
-  private void loadPropertiesImpl(String propsFile)
-  {
     propertiesFile = propsFile;
-    if (!propsAreReadOnly)
+    if (propsFile == null && !propsAreReadOnly)
     {
-      // 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;
-      }
+      propertiesFile = System.getProperty("user.home") + File.separatorChar
+              + ".jalview_properties";
+    }
+    else
+    {
+      // don't corrupt the file we've been given.
+      propsAreReadOnly = true;
     }
 
     if (propertiesFile == null)
-    { // BH 2019 read properties from the Info object associated with this
-      // applet
+    { // BH 2019
       Platform.readInfoProperties(JS_PROPERTY_PREFIX,
               applicationProperties);
     }
@@ -452,9 +417,9 @@ public class Cache implements ApplicationSingletonI
     }
     if (authorDetails == null)
     {
-      applicationProperties.remove("AUTHORS");
-      applicationProperties.remove("AUTHORFNAMES");
-      applicationProperties.remove("YEAR");
+        applicationProperties.remove("AUTHORS");
+        applicationProperties.remove("AUTHORFNAMES");
+        applicationProperties.remove("YEAR");
     }
 
     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
@@ -480,7 +445,7 @@ public class Cache implements ApplicationSingletonI
     }
     if (buildDetails == null)
     {
-      applicationProperties.remove("VERSION");
+        applicationProperties.remove("VERSION");
     }
     String jnlpVersion = System.getProperty("jalview.version");
     String codeVersion = getProperty("VERSION");
@@ -498,20 +463,21 @@ public class Cache implements ApplicationSingletonI
     }
     new BuildDetails(codeVersion, null, codeInstallation);
 
-    SiftsSettings.setMapWithSifts(getDefault("MAP_WITH_SIFTS", false));
+    SiftsSettings
+            .setMapWithSifts(Cache.getDefault("MAP_WITH_SIFTS", false));
 
     SiftsSettings.setSiftDownloadDirectory(jalview.bin.Cache
             .getDefault("sifts_download_dir", DEFAULT_SIFTS_DOWNLOAD_DIR));
 
     SiftsSettings.setFailSafePIDThreshold(
-            getDefault("sifts_fail_safe_pid_threshold",
+            jalview.bin.Cache.getDefault("sifts_fail_safe_pid_threshold",
                     DEFAULT_FAIL_SAFE_PID_THRESHOLD));
 
     SiftsSettings.setCacheThresholdInDays(
-            getDefault("sifts_cache_threshold_in_days",
+            jalview.bin.Cache.getDefault("sifts_cache_threshold_in_days",
                     DEFAULT_CACHE_THRESHOLD_IN_DAYS));
 
-    IdOrgSettings.setUrl(getDefault(Preferences.ID_ORG_HOSTURL,
+    IdOrgSettings.setUrl(getDefault("ID_ORG_HOSTURL",
             "http://www.jalview.org/services/identifiers"));
     IdOrgSettings.setDownloadLocation(ID_ORG_FILE);
 
@@ -519,16 +485,17 @@ public class Cache implements ApplicationSingletonI
             .println("Jalview Version: " + codeVersion + codeInstallation);
 
     StructureImportSettings.setDefaultStructureFileFormat(jalview.bin.Cache
-            .getDefault(Preferences.PDB_DOWNLOAD_FORMAT, PDB_DOWNLOAD_FORMAT));
+            .getDefault("PDB_DOWNLOAD_FORMAT", PDB_DOWNLOAD_FORMAT));
     StructureImportSettings
             .setDefaultPDBFileParser(DEFAULT_PDB_FILE_PARSER);
     // StructureImportSettings
-    // .setDefaultPDBFileParser(jalview.bin.getDefault(
+    // .setDefaultPDBFileParser(jalview.bin.Cache.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 && !Jalview.isHeadlessMode()
-            && getDefault("VERSION_CHECK", true))
+    if (jnlpVersion == null && getDefault("VERSION_CHECK", true)
+            && (System.getProperty("java.awt.headless") == null || System
+                    .getProperty("java.awt.headless").equals("false")))
     {
 
       class VersionChecker extends Thread
@@ -599,10 +566,14 @@ public class Cache implements ApplicationSingletonI
     setProperty("VERSION", codeVersion);
 
     // LOAD USERDEFINED COLOURS
-    initUserColourSchemes(getProperty(Preferences.USER_DEFINED_COLOURS));
+    jalview.bin.Cache
+            .initUserColourSchemes(getProperty("USER_DEFINED_COLOURS"));
+    jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER",
+            false);
   }
 
-  private void deleteBuildProperties()
+
+  private static void deleteBuildProperties()
   {
     applicationProperties.remove("LATEST_VERSION");
     applicationProperties.remove("VERSION");
@@ -624,7 +595,13 @@ public class Cache implements ApplicationSingletonI
    */
   public static String getProperty(String key)
   {
-    return getInstance().applicationProperties.getProperty(key);
+    String prop = applicationProperties.getProperty(key);
+    if (prop == null && Platform.isJS())
+    {
+      prop = applicationProperties.getProperty(Platform.getUniqueAppletID()
+              + "_" + JS_PROPERTY_PREFIX + key);
+    }
+    return prop;
   }
 
   /**
@@ -662,40 +639,6 @@ public class Cache implements ApplicationSingletonI
   }
 
   /**
-   * retrieve a dimension, such as for Jmol
-   * 
-   * @param property
-   * @param def
-   * @return
-   */
-  public static Dimension getDefaultDim(String property, Dimension def)
-  {
-    String s = getProperty(property);
-    if (s != null)
-    {
-      if (s.indexOf(',') < 0)
-      {
-        s = s.trim().replace(' ', ',');
-        if (s.indexOf(',') < 0)
-        {
-          s += "," + s;
-        }
-      }
-      try
-      {
-        int pt = s.indexOf(",");
-        return new Dimension(Integer.parseInt(s.substring(0, pt)),
-                Integer.parseInt(s.substring(pt + 1)));
-      } catch (NumberFormatException e)
-      {
-        System.out.println("Error parsing Dimension property '" + property
-                + "' with value '" + s + "'");
-      }
-    }
-    return def;
-  }
-
-  /**
    * Answers the value of the given property, or the supplied default value if
    * the property is not set
    */
@@ -717,21 +660,11 @@ public class Cache implements ApplicationSingletonI
    */
   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 (andSave && !propsAreReadOnly && propertiesFile != null)
+      if (!propsAreReadOnly)
       {
         FileOutputStream out = new FileOutputStream(propertiesFile);
         applicationProperties.store(out, "---JalviewX Properties File---");
@@ -748,36 +681,18 @@ public class Cache implements ApplicationSingletonI
   /**
    * remove the specified property from the jalview properties file
    * 
-   * @param key
+   * @param string
    */
-  public static void removeProperty(String key)
+  public static void removeProperty(String string)
   {
-    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()
-  {
-    getInstance().savePropertiesImpl();
+    applicationProperties.remove(string);
+    saveProperties();
   }
 
   /**
    * save the properties to the jalview properties path
    */
-  private void savePropertiesImpl()
+  public static void saveProperties()
   {
     if (!propsAreReadOnly)
     {
@@ -793,17 +708,10 @@ public class Cache implements ApplicationSingletonI
     }
   }
 
-  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 = (Platform.isJS() ? FALSE
-          : UNTESTED);
+  private static int vamsasJarsArePresent = -1;
 
   /**
    * Searches for vamsas client classes on class path.
@@ -812,7 +720,7 @@ public class Cache implements ApplicationSingletonI
    */
   public static boolean vamsasJarsPresent()
   {
-    if (vamsasJarsArePresent == UNTESTED)
+    if (vamsasJarsArePresent == -1)
     {
       try
       {
@@ -821,7 +729,7 @@ public class Cache implements ApplicationSingletonI
         {
           jalview.bin.Cache.log.debug(
                   "Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
-          vamsasJarsArePresent = TRUE;
+          vamsasJarsArePresent = 1;
           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
           lvclient.setLevel(Level.toLevel(Cache
                   .getDefault("logs.Vamsas.Level", Level.INFO.toString())));
@@ -832,18 +740,17 @@ public class Cache implements ApplicationSingletonI
         }
       } catch (Exception e)
       {
-        vamsasJarsArePresent = FALSE;
+        vamsasJarsArePresent = 0;
         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
       }
     }
-    return (vamsasJarsArePresent == TRUE);
+    return (vamsasJarsArePresent > 0);
   }
 
   /**
    * internal vamsas class discovery state
    */
-  private static int groovyJarsArePresent = (Platform.isJS() ? FALSE
-          : UNTESTED);
+  private static int groovyJarsArePresent = -1;
 
   /**
    * Searches for vamsas client classes on class path.
@@ -852,7 +759,7 @@ public class Cache implements ApplicationSingletonI
    */
   public static boolean groovyJarsPresent()
   {
-    if (groovyJarsArePresent == UNTESTED)
+    if (groovyJarsArePresent == -1)
     {
       try
       {
@@ -861,7 +768,7 @@ public class Cache implements ApplicationSingletonI
         {
           jalview.bin.Cache.log.debug(
                   "Found Groovy (groovy.lang.GroovyObject can be loaded)");
-          groovyJarsArePresent = TRUE;
+          groovyJarsArePresent = 1;
           Logger lgclient = Logger.getLogger("groovy");
           lgclient.setLevel(Level.toLevel(Cache
                   .getDefault("logs.Groovy.Level", Level.INFO.toString())));
@@ -872,15 +779,15 @@ public class Cache implements ApplicationSingletonI
         }
       } catch (Error e)
       {
-        groovyJarsArePresent = FALSE;
+        groovyJarsArePresent = 0;
         jalview.bin.Cache.log.debug("Groovy Classes are not present", e);
       } catch (Exception e)
       {
-        groovyJarsArePresent = FALSE;
+        groovyJarsArePresent = 0;
         jalview.bin.Cache.log.debug("Groovy Classes are not present");
       }
     }
-    return (groovyJarsArePresent == TRUE);
+    return (groovyJarsArePresent > 0);
   }
 
   /**
@@ -889,18 +796,15 @@ public class Cache implements ApplicationSingletonI
    */
   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)
@@ -935,7 +839,8 @@ public class Cache implements ApplicationSingletonI
                 .newInstance(new Object[]
                 { "Jalview Desktop",
                     (vrs = jalview.bin.Cache.getProperty("VERSION") + "_"
-                            + getDefault("BUILD_DATE", "unknown")),
+                            + jalview.bin.Cache.getDefault("BUILD_DATE",
+                                    "unknown")),
                     "UA-9060947-1" });
         jgoogleanalyticstracker
                 .getMethod("trackAsynchronously", new Class[]
@@ -1123,11 +1028,11 @@ public class Cache implements ApplicationSingletonI
     }
     if (value == null || value.trim().length() < 1)
     {
-      getInstance().applicationProperties.remove(propName);
+      Cache.applicationProperties.remove(propName);
     }
     else
     {
-      getInstance().applicationProperties.setProperty(propName, value);
+      Cache.applicationProperties.setProperty(propName, value);
     }
   }
 
@@ -1172,13 +1077,13 @@ public class Cache implements ApplicationSingletonI
     {
       if (coloursFound.toString().length() > 1)
       {
-        setProperty(Preferences.USER_DEFINED_COLOURS,
+        setProperty(UserDefinedColours.USER_DEFINED_COLOURS,
                 coloursFound.toString());
       }
       else
       {
-        getInstance().applicationProperties
-                .remove(Preferences.USER_DEFINED_COLOURS);
+        applicationProperties
+                .remove(UserDefinedColours.USER_DEFINED_COLOURS);
       }
     }
   }