Merge branch 'develop' into improvement/JAL-4212_remove_macos_test_java_icons
[jalview.git] / src / jalview / bin / Cache.java
index 63f2782..fc9ddda 100755 (executable)
@@ -42,7 +42,9 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.TreeSet;
@@ -311,6 +313,24 @@ public class Cache
   // in-memory only storage of proxy password, safer to use char array
   public static char[] proxyAuthPassword = null;
 
+  /**
+   * Session properties, set by command line, try not to affect stored
+   * properties!
+   */
+  private static Map<String, String> sessionProperties = new HashMap<>();
+
+  private static boolean bypassSessionProperties = false;
+
+  public static void enableSessionProperties()
+  {
+    bypassSessionProperties = false;
+  }
+
+  public static void disableSessionProperties()
+  {
+    bypassSessionProperties = true;
+  }
+
   /** Jalview Properties */
   public static Properties applicationProperties = new Properties()
   {
@@ -713,7 +733,21 @@ public class Cache
    */
   public static String getProperty(String key)
   {
-    String prop = applicationProperties.getProperty(key);
+    return getProperty(key, false);
+  }
+
+  public static String getProperty(String key,
+          boolean skipSessionProperties)
+  {
+    String prop = null;
+    if (!(skipSessionProperties || bypassSessionProperties))
+    {
+      prop = getSessionProperty(key);
+    }
+    if (prop == null)
+    {
+      prop = applicationProperties.getProperty(key);
+    }
     if (prop == null && Platform.isJS())
     {
       prop = applicationProperties.getProperty(Platform.getUniqueAppletID()
@@ -783,8 +817,16 @@ public class Cache
     try
     {
       oldValue = applicationProperties.setProperty(key, obj);
-      if (propertiesFile != null && !propsAreReadOnly)
+      if (propertiesFile != null && !propsAreReadOnly
+      // don't rewrite if new value is same as old value
+              && !((obj == null && oldValue == null)
+                      || (obj != null && obj.equals(oldValue))))
       {
+        // reset the session property too
+        if (sessionProperties.containsKey(key))
+        {
+          sessionProperties.remove(key);
+        }
         FileOutputStream out = new FileOutputStream(propertiesFile);
         applicationProperties.store(out, "---JalviewX Properties File---");
         out.close();
@@ -936,8 +978,21 @@ public class Cache
               + " Desktop";
       String version = Cache.getProperty("VERSION") + "_"
               + Cache.getDefault("BUILD_DATE", "unknown");
-      String path = "/"
-              + String.join("/", appName, version, APPLICATION_STARTED);
+      String path;
+      /* we don't want to encode ':' as "%3A" for backward compatibility with the UA setup
+      try
+      {
+        path = "/" + String.join("/", URLEncoder.encode(appName, "UTF-8"),
+                URLEncoder.encode(version, "UTF-8"),
+                URLEncoder.encode(APPLICATION_STARTED, "UTF-8"));
+      } catch (UnsupportedEncodingException e)
+      {
+      */
+      path = ("/" + String.join("/", appName, version, APPLICATION_STARTED))
+              .replace(' ', '+');
+      /*
+      }
+      */
       GoogleAnalytics4 ga4 = GoogleAnalytics4.getInstance();
 
       // This will add a page_view similar to the old UA analytics.
@@ -948,7 +1003,7 @@ public class Cache
 
       // This will send a new "application_launch" event with parameters
       // including the old-style "path", the channel name and version
-      ga4.sendAnalytics("application_launch", "page_location", path);
+      ga4.sendAnalytics("application_launch", true, "page_location", path);
     }
     else
     {
@@ -1728,4 +1783,17 @@ public class Cache
     }
     return bootstrapProps;
   }
+
+  public static void setSessionProperty(String key, String val)
+  {
+    if (key != null)
+    {
+      sessionProperties.put(key, val);
+    }
+  }
+
+  public static String getSessionProperty(String key)
+  {
+    return key == null ? null : sessionProperties.get(key);
+  }
 }