JAL-3633 refactored/simplified LaunchUtils code
authorBen Soares <b.soares@dundee.ac.uk>
Thu, 25 Mar 2021 19:27:20 +0000 (19:27 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Thu, 25 Mar 2021 19:27:20 +0000 (19:27 +0000)
getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/Application.java
getdown/src/getdown/core/src/main/java/jalview/util/ChannelProperties.java
getdown/src/getdown/core/src/main/java/jalview/util/LaunchUtils.java [moved from getdown/src/getdown/core/src/main/java/jalview/bin/LaunchUtils.java with 62% similarity]
getdown/src/getdown/launcher/src/main/java/com/threerings/getdown/launcher/ProxyUtil.java
src/jalview/bin/Launcher.java
src/jalview/util/ChannelProperties.java
src/jalview/util/LaunchUtils.java [moved from src/jalview/bin/LaunchUtils.java with 62% similarity]

index 468796c..684844a 100644 (file)
@@ -31,8 +31,7 @@ import java.util.zip.GZIPInputStream;
 
 import jalview.bin.HiDPISetting;
 import jalview.bin.MemorySetting;
-import jalview.bin.LaunchUtils;
-import jalview.util.ChannelProperties;
+import jalview.util.LaunchUtils;
 
 import com.threerings.getdown.util.*;
 // avoid ambiguity with java.util.Base64 which we can't use as it's 1.8+
@@ -1184,17 +1183,8 @@ public class Application
         }
 
         // use saved preferences if no cmdline args
-        File channelProps = getLocalPath(ChannelProperties.CHANNEL_PROPERTIES_FILENAME);
-        if (channelProps.exists()) {
-          try {
-            InputStream is = new FileInputStream(channelProps);
-            ChannelProperties.loadProps(is);
-          } catch (IOException e) {
-            log.error(e.getMessage());
-          }
-        }
-        boolean useCustomisedSettings = Boolean.parseBoolean(LaunchUtils.getUserPreference(MemorySetting.CUSTOMISED_SETTINGS));
-        if (useCustomisedSettings) {
+        LaunchUtils.loadChannelProps(getAppDir());
+        if (LaunchUtils.getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS)) {
           if (jvmmempc == null) {
             jvmmempc = LaunchUtils.getUserPreference(MemorySetting.MEMORY_JVMMEMPC);
           }
index cf3b190..40f6110 100644 (file)
@@ -1,6 +1,9 @@
 package jalview.util;
 
 import java.awt.Image;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -122,14 +125,22 @@ public class ChannelProperties
     }
   }
 
-  public static void loadProps(InputStream is)
+  protected static void loadProps(File dir)
   {
-    try
+    File channelPropsFile = new File(dir, CHANNEL_PROPERTIES_FILENAME);
+    if (channelPropsFile.exists())
     {
-      channelProps.load(is);
-    } catch (IOException e)
-    {
-      System.err.println(e.getMessage());
+      try
+      {
+        InputStream is = new FileInputStream(channelPropsFile);
+        channelProps.load(is);
+      } catch (FileNotFoundException e)
+      {
+        System.err.println(e.getMessage());
+      } catch (IOException e)
+      {
+        System.err.println(e.getMessage());
+      }
     }
   }
 
@@ -1,4 +1,4 @@
-package jalview.bin;
+package jalview.util;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -6,11 +6,14 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Properties;
 
-import jalview.util.ChannelProperties;
-
 public class LaunchUtils
 {
 
+  public static void loadChannelProps(File dir)
+  {
+    ChannelProperties.loadProps(dir);
+  }
+
   private static Properties userPreferences = null;
 
   public static String getUserPreference(String key)
@@ -19,8 +22,16 @@ public class LaunchUtils
     {
       String channelPrefsFilename = ChannelProperties
               .getProperty("preferences.filename");
-      String propertiesFile = System.getProperty("user.home")
-              + File.separatorChar + channelPrefsFilename;
+      if (channelPrefsFilename == null)
+      {
+        return null;
+      }
+      File propertiesFile = new File(System.getProperty("user.home"),
+              channelPrefsFilename);
+      if (!propertiesFile.exists())
+      {
+        return null;
+      }
       try
       {
         userPreferences = new Properties();
@@ -38,4 +49,8 @@ public class LaunchUtils
     return userPreferences.getProperty(key);
   }
 
+  public static boolean getBooleanUserPreference(String key)
+  {
+    return Boolean.parseBoolean(getUserPreference(key));
+  }
 }
index 6295df4..cb51ca4 100644 (file)
@@ -21,8 +21,7 @@ import java.net.URLConnection;
 import java.util.Iterator;
 import java.util.ServiceLoader;
 
-import jalview.bin.LaunchUtils;
-import jalview.util.ChannelProperties;
+import jalview.util.LaunchUtils;
 
 import ca.beq.util.win32.registry.RegistryKey;
 import ca.beq.util.win32.registry.RegistryValue;
@@ -114,25 +113,17 @@ public class ProxyUtil {
       String port = null;
       boolean proxyAuth = false;
       String username = null;
-      File channelProps = app.getLocalPath(ChannelProperties.CHANNEL_PROPERTIES_FILENAME);
-      if (channelProps.exists()) {
-        try {
-          InputStream is = new FileInputStream(channelProps);
-          ChannelProperties.loadProps(is);
-        } catch (IOException e) {
-          log.error(e.getMessage());
+      LaunchUtils.loadChannelProps(app.getAppDir());
+      if (LaunchUtils.getBooleanUserPreference("USE_PROXY")) {
+        host = LaunchUtils.getUserPreference("PROXY_SERVER_HTTPS");
+        port = LaunchUtils.getUserPreference("PROXY_PORT_HTTPS");
+        if (StringUtil.isBlank(host)) {
+          host = LaunchUtils.getUserPreference("PROXY_SERVER");
+          port = LaunchUtils.getUserPreference("PROXY_PORT");
         }
-        if (Boolean.parseBoolean(LaunchUtils.getUserPreference("USE_PROXY"))) {
-          host = LaunchUtils.getUserPreference("PROXY_SERVER_HTTPS");
-          port = LaunchUtils.getUserPreference("PROXY_PORT_HTTPS");
-          if (StringUtil.isBlank(host)) {
-            host = LaunchUtils.getUserPreference("PROXY_SERVER");
-            port = LaunchUtils.getUserPreference("PROXY_PORT");
-          }
-          proxyAuth = Boolean.parseBoolean(LaunchUtils.getUserPreference("PROXY_AUTH"));
-          if (proxyAuth) {
-            username = LaunchUtils.getUserPreference("PROXY_AUTH_USERNAME");
-          }
+        proxyAuth = LaunchUtils.getBooleanUserPreference("PROXY_AUTH");
+        if (proxyAuth) {
+          username = LaunchUtils.getUserPreference("PROXY_AUTH_USERNAME");
         }
       }
       return new String[]{ host, port, String.valueOf(proxyAuth), username };
index fe4b7ef..744f406 100644 (file)
@@ -27,6 +27,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import jalview.util.ChannelProperties;
+import jalview.util.LaunchUtils;
 
 /**
  * A Launcher class for Jalview. This class is used to launch Jalview from the
@@ -105,8 +106,8 @@ public class Launcher
     }
 
     // use saved preferences if no cmdline args
-    boolean useCustomisedSettings = Boolean.parseBoolean(LaunchUtils
-            .getUserPreference(MemorySetting.CUSTOMISED_SETTINGS));
+    boolean useCustomisedSettings = LaunchUtils
+            .getBooleanUserPreference(MemorySetting.CUSTOMISED_SETTINGS);
     if (useCustomisedSettings)
     {
       if (jvmmempc == null)
index cf3b190..40f6110 100644 (file)
@@ -1,6 +1,9 @@
 package jalview.util;
 
 import java.awt.Image;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -122,14 +125,22 @@ public class ChannelProperties
     }
   }
 
-  public static void loadProps(InputStream is)
+  protected static void loadProps(File dir)
   {
-    try
+    File channelPropsFile = new File(dir, CHANNEL_PROPERTIES_FILENAME);
+    if (channelPropsFile.exists())
     {
-      channelProps.load(is);
-    } catch (IOException e)
-    {
-      System.err.println(e.getMessage());
+      try
+      {
+        InputStream is = new FileInputStream(channelPropsFile);
+        channelProps.load(is);
+      } catch (FileNotFoundException e)
+      {
+        System.err.println(e.getMessage());
+      } catch (IOException e)
+      {
+        System.err.println(e.getMessage());
+      }
     }
   }
 
similarity index 62%
rename from src/jalview/bin/LaunchUtils.java
rename to src/jalview/util/LaunchUtils.java
index 89d6069..3302dba 100644 (file)
@@ -1,4 +1,4 @@
-package jalview.bin;
+package jalview.util;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -6,11 +6,14 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Properties;
 
-import jalview.util.ChannelProperties;
-
 public class LaunchUtils
 {
 
+  public static void loadChannelProps(File dir)
+  {
+    ChannelProperties.loadProps(dir);
+  }
+
   private static Properties userPreferences = null;
 
   public static String getUserPreference(String key)
@@ -19,8 +22,16 @@ public class LaunchUtils
     {
       String channelPrefsFilename = ChannelProperties
               .getProperty("preferences.filename");
-      String propertiesFile = System.getProperty("user.home")
-              + File.separatorChar + channelPrefsFilename;
+      if (channelPrefsFilename == null)
+      {
+        return null;
+      }
+      File propertiesFile = new File(System.getProperty("user.home"),
+              channelPrefsFilename);
+      if (!propertiesFile.exists())
+      {
+        return null;
+      }
       try
       {
         userPreferences = new Properties();
@@ -38,4 +49,8 @@ public class LaunchUtils
     return userPreferences.getProperty(key);
   }
 
+  public static boolean getBooleanUserPreference(String key)
+  {
+    return Boolean.parseBoolean(getUserPreference(key));
+  }
 }