X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FCache.java;h=28bab15feec6fa01417699105c77be1efd1c31db;hb=304e64fb34b32659be1bbfd39fb4e15b2f79586e;hp=50db03b7c228c9448627e8528e613f075577d034;hpb=2ea1b352349a737f513131873551bfb7899d812f;p=jalview.git diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index 50db03b..28bab15 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -52,6 +52,7 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; +import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI; import jalview.datamodel.PDBEntry; import jalview.gui.Preferences; import jalview.gui.UserDefinedColours; @@ -182,8 +183,8 @@ import jalview.ws.sifts.SiftsSettings; * when shading by annotation *
  • ANNOTATIONCOLOUR_MAX (red) Shade used for maximum value of annotation * when shading by annotation
  • - *
  • www.jalview.org (http://www.jalview.org) a property enabling all HTTP - * requests to be redirected to a mirror of http://www.jalview.org
  • + *
  • www.jalview.org (https://www.jalview.org) a property enabling all HTTP + * requests to be redirected to a mirror of https://www.jalview.org
  • *
  • FIGURE_AUTOIDWIDTH (false) Expand the left hand column of an exported * alignment figure to accommodate even the longest sequence ID or annotation * label.
  • @@ -220,8 +221,26 @@ import jalview.ws.sifts.SiftsSettings; * @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 + */ + public static Cache getInstance() + { + return (Cache) ApplicationSingletonProvider.getInstance(Cache.class); + } + /** * property giving log4j level for CASTOR loggers */ @@ -240,10 +259,8 @@ public class Cache /** * Sifts settings */ - public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System - .getProperty("user.home") + File.separatorChar - + ".sifts_downloads" + File.separatorChar; - + public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = Platform.getUserPath(".sifts_downloads/"); + private final static String DEFAULT_CACHE_THRESHOLD_IN_DAYS = "2"; private final static String DEFAULT_FAIL_SAFE_PID_THRESHOLD = "30"; @@ -251,8 +268,7 @@ public class Cache /** * Identifiers.org download settings */ - private static final String ID_ORG_FILE = System.getProperty("user.home") - + File.separatorChar + ".identifiers.org.ids.json"; + private static final String ID_ORG_FILE = Platform.getUserPath(".identifiers.org.ids.json"); /** * Allowed values are PDB or mmCIF @@ -300,7 +316,7 @@ public class Cache public static char[] proxyAuthPassword = null; /** Jalview Properties */ - public static Properties applicationProperties = new Properties() + private Properties applicationProperties = new Properties() { // override results in properties output in alphabetical order @Override @@ -370,16 +386,31 @@ public class Cache } /** - * Loads properties from the given properties file. Any existing properties - * are first cleared. + * Loads properties from the given properties file. Any existing properties are + * first cleared. */ public static void loadProperties(String propsFile) { + + getInstance().loadPropertiesImpl(propsFile); + + } + + private void loadPropertiesImpl(String propsFile) + { + propertiesFile = propsFile; + String releasePropertiesFile = null; + boolean defaultProperties = false; if (propsFile == null && !propsAreReadOnly) { - propertiesFile = System.getProperty("user.home") + File.separatorChar - + ".jalview_properties"; + // TODO: @bsoares - for 2.12 testing: check test,develop,release props are located correctly + String channelPrefsFilename = ChannelProperties + .getProperty("preferences.filename"); + String releasePrefsFilename = ".jalview_properties"; + propertiesFile = Platform.getUserPath(channelPrefsFilename); + releasePropertiesFile = Platform.getUserPath(releasePrefsFilename); + defaultProperties = true; } else { @@ -399,20 +430,29 @@ public class Cache InputStream fis; try { + // props file provided as URL fis = new 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); + String readPropertiesFile = propertiesFile; + // if we're using the usual properties file and the channel properties + // file doesn't exist, read .jalview_properties + // (but we'll still save to the channel properties file). + if (defaultProperties && (!new File(propertiesFile).exists()) + && (new File(releasePropertiesFile).exists())) + { + readPropertiesFile = releasePropertiesFile; + } + fis = new FileInputStream(readPropertiesFile); } applicationProperties.clear(); applicationProperties.load(fis); @@ -506,7 +546,7 @@ public class Cache DEFAULT_CACHE_THRESHOLD_IN_DAYS)); IdOrgSettings.setUrl(getDefault("ID_ORG_HOSTURL", - "http://www.jalview.org/services/identifiers")); + "https://www.jalview.org/services/identifiers")); IdOrgSettings.setDownloadLocation(ID_ORG_FILE); StructureImportSettings.setDefaultStructureFileFormat(jalview.bin.Cache @@ -534,7 +574,8 @@ public class Cache @Override public void run() { - String buildPropertiesUrl = Cache.getAppbaseBuildProperties(); + String remoteBuildPropertiesUrl = Cache + .getAppbaseBuildProperties(); String orgtimeout = System .getProperty("sun.net.client.defaultConnectTimeout"); @@ -549,7 +590,7 @@ public class Cache { System.setProperty("sun.net.client.defaultConnectTimeout", "5000"); - java.net.URL url = new java.net.URL(buildPropertiesUrl); + java.net.URL url = new java.net.URL(remoteBuildPropertiesUrl); BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream())); @@ -561,7 +602,7 @@ public class Cache { System.out .println("Non-fatal exception when checking version at " - + buildPropertiesUrl + ":"); + + remoteBuildPropertiesUrl + ":"); System.out.println(ex); remoteVersion = getProperty("VERSION"); } @@ -622,7 +663,7 @@ public class Cache return url; } - public static void loadBuildProperties(boolean reportVersion) + public void loadBuildProperties(boolean reportVersion) { String codeInstallation = getProperty("INSTALLATION"); boolean printVersion = codeInstallation == null; @@ -638,11 +679,21 @@ public class Cache InputStream in = localJarFileURL.openStream(); buildProperties.load(in); in.close(); - applicationProperties.put("BUILD_DATE", - buildProperties.get("BUILD_DATE")); - applicationProperties.put("INSTALLATION", - buildProperties.get("INSTALLATION")); - applicationProperties.put("VERSION", buildProperties.get("VERSION")); + if (buildProperties.getProperty("BUILD_DATE", null) != null) + { + applicationProperties.put("BUILD_DATE", + buildProperties.getProperty("BUILD_DATE")); + } + if (buildProperties.getProperty("INSTALLATION", null) != null) + { + applicationProperties.put("INSTALLATION", + buildProperties.getProperty("INSTALLATION")); + } + if (buildProperties.getProperty("VERSION", null) != null) + { + applicationProperties.put("VERSION", + buildProperties.getProperty("VERSION")); + } } catch (Exception ex) { System.out.println("Error reading build details: " + ex); @@ -670,7 +721,7 @@ public class Cache } } - private static void deleteBuildProperties() + private void deleteBuildProperties() { applicationProperties.remove("LATEST_VERSION"); applicationProperties.remove("VERSION"); @@ -682,28 +733,27 @@ public class Cache } /** - * Gets Jalview application property of given key. Returns null if key not - * found + * Gets Jalview application property of given key. Returns null if key not found * * @param key - * Name of property + * Name of property * * @return Property value */ public static String getProperty(String key) { - String prop = applicationProperties.getProperty(key); - if (prop == null && Platform.isJS()) - { - prop = applicationProperties.getProperty(Platform.getUniqueAppletID() - + "_" + JS_PROPERTY_PREFIX + key); - } + String prop = getInstance().applicationProperties.getProperty(key); + // if (prop == null && Platform.isJS()) + // { + // prop = applicationProperties.getProperty(Platform.getUniqueAppletID() + // + "_" + JS_PROPERTY_PREFIX + key); + // } return prop; } /** - * These methods are used when checking if the saved preference is different - * to the default setting + * These methods are used when checking if the saved preference is different to + * the default setting */ public static boolean getDefault(String property, boolean def) @@ -736,8 +786,8 @@ public class Cache } /** - * Answers the value of the given property, or the supplied default value if - * the property is not set + * Answers the value of the given property, or the supplied default value if the + * property is not set */ public static String getDefault(String property, String def) { @@ -749,41 +799,56 @@ public class Cache * Stores property in the file "HOME_DIR/.jalview_properties" * * @param key - * Name of object + * Name of object * @param obj - * String value of property + * String value of property * * @return previous value of property (or null) */ public static Object setProperty(String key, String obj) { - Object oldValue = null; - try - { - oldValue = applicationProperties.setProperty(key, obj); - if (propertiesFile != null && !propsAreReadOnly) - { - FileOutputStream out = new FileOutputStream(propertiesFile); - applicationProperties.store(out, "---JalviewX Properties File---"); - out.close(); - } - } catch (Exception ex) - { - System.out.println( - "Error setting property: " + key + " " + obj + "\n" + ex); - } - return oldValue; + return getInstance().setPropertyImpl(key, obj, true); } /** - * remove the specified property from the jalview properties file + * Removes the specified property from the jalview properties file * - * @param string + * @param key */ - public static void removeProperty(String string) + public static void removeProperty(String key) { - applicationProperties.remove(string); - saveProperties(); + getInstance().removePropertyImpl(key, true); + } + + /** + * Removes the named property for the running application, without saving the + * properties file + * + * BH noting that ColourMenuHelper calls this. If the intent is to save, then + * simply chanet that call to removeProperty(key). + * + * @param key + */ + public static void removePropertyNoSave(String key) + { + + getInstance(). + + removePropertyImpl(key, false); + } + + /** + * Removes the named property, and optionally saves the current properties to + * file + * + * @param key + * @param andSave + */ + private void removePropertyImpl(String key, boolean andSave) + { + applicationProperties.remove(key); + if (andSave) + saveProperties(); } /** @@ -791,6 +856,15 @@ public class Cache */ public static void saveProperties() { + getInstance().savePropertiesImpl(); + } + + /** + * save the properties to the jalview properties path + */ + private void savePropertiesImpl() + + { if (!propsAreReadOnly) { try @@ -1044,9 +1118,9 @@ public class Cache * @param property * @param colour */ - public static void setColourProperty(String property, Color colour) + public static void setColourPropertyNoSave(String property, Color colour) { - setProperty(property, jalview.util.Format.getHexString(colour)); + setPropertyNoSave(property, jalview.util.Format.getHexString(colour)); } /** @@ -1074,11 +1148,16 @@ public class Cache public static Date getDateProperty(String propertyName) { String val = getProperty(propertyName); + if (val != null) { try { - return date_format.parse(val); + if ((val = val.trim()).indexOf(",") < 0 && val.indexOf("-") >= 0 && val.indexOf(" ") == val.lastIndexOf(" ")) { + val = val.replace(" ",", ").replace('-',' '); + } + Date date = date_format.parse(val); + return date; } catch (Exception ex) { System.err.println("Invalid or corrupt date in property '" @@ -1127,11 +1206,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); } } @@ -1139,7 +1218,7 @@ public class Cache * Loads in user colour schemes from files. * * @param files - * a '|'-delimited list of file paths + * a '|'-delimited list of file paths */ public static void initUserColourSchemes(String files) { @@ -1181,7 +1260,7 @@ public class Cache } else { - applicationProperties + getInstance().applicationProperties .remove(UserDefinedColours.USER_DEFINED_COLOURS); } } @@ -1220,8 +1299,8 @@ public class Cache .append(" Installation: "); sb.append(jalview.bin.Cache.getDefault("INSTALLATION", "unknown")); sb.append("\n"); - sb.append("Build Date: "); - sb.append(jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")); + sb.append("Build Date: " + + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")); sb.append("\n"); sb.append("Java version: "); sb.append(System.getProperty("java.version")); @@ -1272,6 +1351,52 @@ public class Cache return jalview.bin.Cache.getDefault("INSTALLATION", "unknown"); } + /** + * + * For AppletParams and Preferences ok_actionPerformed and + * startupFileTextfield_mouseClicked + * + * Sets a property value for the running application, without saving it to the + * properties file + * + * @param key + * @param obj + */ + public static void setPropertyNoSave(String key, String obj) + { + getInstance().setPropertyImpl(key, obj, false); + } + + /** + * Sets a property value, and optionally also saves the current properties to + * file + * + * @param key + * @param obj + * @param andSave + * @return + */ + private Object setPropertyImpl( + String key, String obj, boolean andSave) + { + Object oldValue = null; + try + { + oldValue = applicationProperties.setProperty(key, obj); + if (andSave && !propsAreReadOnly && propertiesFile != null) + { + FileOutputStream out = new FileOutputStream(propertiesFile); + applicationProperties.store(out, "---JalviewX Properties File---"); + out.close(); + } + } catch (Exception ex) + { + System.out.println( + "Error setting property: " + key + " " + obj + "\n" + ex); + } + return oldValue; + } + public static String getStackTraceString(Throwable t) { StringWriter sw = new StringWriter(); @@ -1448,8 +1573,8 @@ public class Cache // open Preferences -> Connections String message = MessageManager .getString("label.proxy_password_required"); - Preferences.openPreferences(Preferences.CONNECTIONS_TAB, - message); + Preferences.openPreferences( + Preferences.TabRef.CONNECTIONS_TAB, message); Preferences.getInstance() .proxyAuthPasswordCheckHighlight(true, true); } @@ -1641,11 +1766,22 @@ public class Cache static { - Float specversion = Float - .parseFloat(System.getProperty("java.specification.version")); - releaseAppbase = (specversion < 9) - ? "https://www.jalview.org/getdown/release/1.8" - : "https://www.jalview.org/getdown/release/11"; + if (!Platform.isJS()) + { + Float specversion = Float + .parseFloat(System.getProperty("java.specification.version")); + releaseAppbase = (specversion < 9) + ? "https://www.jalview.org/getdown/release/1.8" + : "https://www.jalview.org/getdown/release/11"; + } + else + { + // this value currenly made up, can be changed to URL that will be + // "https://www.jalview.org/jalview-js/swingjs/j2s/build_properties" + releaseAppbase = "https://www.jalview.org/jalview-js"; + getdownAppbase = releaseAppbase; + getdownDistDir = "/swingjs/j2s"; + } } // look for properties (passed in by getdown) otherwise default to release