*/
public class Cache
{
+
+ static Cache instance;
+
+ /**
+ * 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()
+ {
+
+ // BH 2019.05.08 need to isolate static fields in JavaScript
+
+ Cache i = instance;
+ @SuppressWarnings("unused")
+ ThreadGroup g = null;
+ if (Platform.isJS())
+ {
+ g = Thread.currentThread().getThreadGroup();
+ /**
+ * @j2sNative i = g._jalviewCacheInstance;
+ *
+ */
+ }
+ if (i == null)
+ {
+ i = new Cache();
+ if (Platform.isJS())
+ {
+ /**
+ * @j2sNative g._jalviewCacheInstance = i;
+ *
+ */
+ }
+ else
+ {
+ instance = i;
+ }
+ }
+ return i;
+ }
+
/**
* property giving log4j level for CASTOR loggers
*/
*/
public static Logger log;
+ private Cache()
+ {
+ // inaccessible
+ }
+
/** 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
};
/** Default file is ~/.jalview_properties */
+ // BH 2019.05.07 note: Instances of Jalview will share this file.
static String propertiesFile;
- private static boolean propsAreReadOnly = Platform.isJS();
+ /**
+ * 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 loadProperties(String propsFile)
{
+ getInstance().loadPropertiesImpl(propsFile);
+ }
+
+ private void loadPropertiesImpl(String propsFile)
+ {
propertiesFile = propsFile;
- if (propsFile == null && !propsAreReadOnly)
- {
- 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;
+ }
}
if (propertiesFile == null)
- { // BH 2019
+ { // BH 2019 read properties from the Info object associated with this
+ // applet
Platform.readInfoProperties(JS_PROPERTY_PREFIX,
applicationProperties);
}
}
- private static void deleteBuildProperties()
+ private void deleteBuildProperties()
{
applicationProperties.remove("LATEST_VERSION");
applicationProperties.remove("VERSION");
*/
public static String getProperty(String key)
{
+ return getInstance().getPropertyImpl(key);
+ }
+
+ private String getPropertyImpl(String key)
+ {
String prop = applicationProperties.getProperty(key);
if (prop == null && Platform.isJS())
{
*/
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)
{
FileOutputStream out = new FileOutputStream(propertiesFile);
applicationProperties.store(out, "---JalviewX Properties File---");
/**
* 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)
{
}
}
+ 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.
*/
public static boolean vamsasJarsPresent()
{
- if (vamsasJarsArePresent == -1)
+ if (vamsasJarsArePresent == UNTESTED)
{
try
{
{
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())));
}
} 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.
*/
public static boolean groovyJarsPresent()
{
- if (groovyJarsArePresent == -1)
+ if (groovyJarsArePresent == UNTESTED)
{
try
{
{
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())));
}
} 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);
}
/**
*/
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)
}
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);
}
}
}
else
{
- applicationProperties
+ getInstance().applicationProperties
.remove(UserDefinedColours.USER_DEFINED_COLOURS);
}
}