From b9bf3044b32b62771648842169f79ca8e7050c7f Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 5 Nov 2020 16:01:23 +0000 Subject: [PATCH] JAL-3608 cherry pick of 92cb745e7 --- src/jalview/bin/Cache.java | 99 +++++++++++------ src/jalview/bin/Jalview.java | 245 ++++++++++++++++++++++++------------------ 2 files changed, 205 insertions(+), 139 deletions(-) diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index ff475b6..40fccb0 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -20,17 +20,6 @@ */ package jalview.bin; -import jalview.datamodel.PDBEntry; -import jalview.gui.UserDefinedColours; -import jalview.schemes.ColourSchemeLoader; -import jalview.schemes.ColourSchemes; -import jalview.schemes.UserColourScheme; -import jalview.structure.StructureImportSettings; -import jalview.urls.IdOrgSettings; -import jalview.util.ColorUtils; -import jalview.util.Platform; -import jalview.ws.sifts.SiftsSettings; - import java.awt.Color; import java.io.BufferedReader; import java.io.File; @@ -38,6 +27,8 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.StringWriter; import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -58,6 +49,17 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; +import jalview.datamodel.PDBEntry; +import jalview.gui.UserDefinedColours; +import jalview.schemes.ColourSchemeLoader; +import jalview.schemes.ColourSchemes; +import jalview.schemes.UserColourScheme; +import jalview.structure.StructureImportSettings; +import jalview.urls.IdOrgSettings; +import jalview.util.ColorUtils; +import jalview.util.Platform; +import jalview.ws.sifts.SiftsSettings; + /** * Stores and retrieves Jalview Application Properties Lists and fields within * list entries are separated by '|' symbols unless otherwise stated (|) clauses @@ -318,7 +320,8 @@ public class Cache // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level", // Level.INFO.toString()))); // we shouldn't need to do this - org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO); + org.apache.log4j.Logger.getRootLogger() + .setLevel(org.apache.log4j.Level.INFO); jalview.bin.Cache.log.setLevel(Level.toLevel(Cache .getDefault("logs.Jalview.level", Level.INFO.toString()))); @@ -364,7 +367,7 @@ public class Cache InputStream fis; try { - fis = new java.net.URL(propertiesFile).openStream(); + fis = new URL(propertiesFile).openStream(); System.out.println( "Loading jalview properties from : " + propertiesFile); System.out.println( @@ -422,9 +425,9 @@ public class Cache } if (authorDetails == null) { - applicationProperties.remove("AUTHORS"); - applicationProperties.remove("AUTHORFNAMES"); - applicationProperties.remove("YEAR"); + applicationProperties.remove("AUTHORS"); + applicationProperties.remove("AUTHORFNAMES"); + applicationProperties.remove("YEAR"); } loadBuildProperties(false); @@ -457,15 +460,16 @@ public class Cache String jnlpVersion = System.getProperty("jalview.version"); - // jnlpVersion will be null if a latest version check for the channel needs to - // be done + // jnlpVersion will be null if a latest version check for the channel needs + // to be done // Dont do this check if running in headless mode if (jnlpVersion == null && getDefault("VERSION_CHECK", true) && (System.getProperty("java.awt.headless") == null || System .getProperty("java.awt.headless").equals("false"))) { - new Thread() + + class VersionChecker extends Thread { @Override public void run() @@ -513,7 +517,10 @@ public class Cache setProperty("LATEST_VERSION", remoteVersion); } - }.start(); + } + + VersionChecker vc = new VersionChecker(); + vc.start(); } else { @@ -550,8 +557,8 @@ public class Cache url = Cache.class.getResource(resourcePath).toString(); } catch (Exception ex) { - System.err.println("Failed to resolve resource " + resourcePath + ": " - + ex.getMessage()); + System.err.println("Failed to resolve resource " + resourcePath + + ": " + ex.getMessage()); } } else @@ -600,8 +607,8 @@ public class Cache new BuildDetails(codeVersion, null, codeInstallation); if (printVersion && reportVersion) { - System.out - .println("Jalview Version: " + codeVersion + codeInstallation); + System.out.println( + "Jalview Version: " + codeVersion + codeInstallation); } } @@ -1145,16 +1152,23 @@ public class Cache public static String getVersionDetailsForConsole() { StringBuilder sb = new StringBuilder(); - sb.append("Jalview Version: " + jalview.bin.Cache.getDefault("VERSION", "TEST")); + sb.append("Jalview Version: "); + sb.append(jalview.bin.Cache.getDefault("VERSION", "TEST")); sb.append("\n"); - sb.append("Jalview Installation: " - + jalview.bin.Cache.getDefault("INSTALLATION", "unknown")); + sb.append("Jalview Installation: "); + sb.append(jalview.bin.Cache.getDefault("INSTALLATION", "unknown")); sb.append("\n"); - sb.append("Build Date: " + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")); + sb.append("Build Date: "); + sb.append(jalview.bin.Cache.getDefault("BUILD_DATE", "unknown")); sb.append("\n"); - sb.append("Java version: " + System.getProperty("java.version")); + sb.append("Java version: "); + sb.append(System.getProperty("java.version")); sb.append("\n"); - sb.append(System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version")); + sb.append(System.getProperty("os.arch")); + sb.append(" "); + sb.append(System.getProperty("os.name")); + sb.append(" "); + sb.append(System.getProperty("os.version")); sb.append("\n"); appendIfNotNull(sb, "Install4j version: ", System.getProperty("sys.install4jVersion"), "\n", null); @@ -1163,11 +1177,18 @@ public class Cache appendIfNotNull(sb, "Launcher version: ", System.getProperty("launcher_version"), "\n", null); LookAndFeel laf = UIManager.getLookAndFeel(); - String lafName = laf == null?"Not obtained":laf.getName(); - String lafClass = laf == null?"unknown":laf.getClass().getName(); - appendIfNotNull(sb, "LookAndFeel: ", lafName+" ("+lafClass+")", "\n", null); - // Not displayed in release version ( determined by possible version number regex 9[9.]*9[.-_a9]* ) - if (Pattern.matches("^\\d[\\d\\.]*\\d[\\.\\-\\w]*$", jalview.bin.Cache.getDefault("VERSION", "TEST"))) { + String lafName = laf == null ? "Not obtained" : laf.getName(); + String lafClass = laf == null ? "unknown" : laf.getClass().getName(); + sb.append("LookAndFeel: "); + sb.append(lafName); + sb.append(" ("); + sb.append(lafClass); + sb.append(")\n"); + // Not displayed in release version ( determined by possible version number + // regex 9[9.]*9[.-_a9]* ) + if (Pattern.matches("^\\d[\\d\\.]*\\d[\\.\\-\\w]*$", + jalview.bin.Cache.getDefault("VERSION", "TEST"))) + { appendIfNotNull(sb, "Getdown appdir: ", System.getProperty("getdownappdir"), "\n", null); appendIfNotNull(sb, "Java home: ", System.getProperty("java.home"), @@ -1186,4 +1207,12 @@ public class Cache // eg 'built from Source' or update channel return jalview.bin.Cache.getDefault("INSTALLATION", "unknown"); } + + public static String getStackTraceString(Throwable t) + { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + return sw.toString(); + } } diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index e130277..06f3a47 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -93,8 +93,9 @@ public class Jalview Platform.getURLCommandArguments(); } - // singleton instance of this class - + /* + * singleton instance of this class + */ private static Jalview instance; private Desktop desktop; @@ -285,6 +286,17 @@ public class Jalview ArgsParser aparser = new ArgsParser(args); boolean headless = false; + if (aparser.contains("help") || aparser.contains("h")) + { + showUsage(); + System.exit(0); + } + if (aparser.contains("nodisplay") || aparser.contains("nogui") + || aparser.contains("headless")) + { + System.setProperty("java.awt.headless", "true"); + headless = true; + } String usrPropsFile = aparser.getValue("props"); Cache.loadProperties(usrPropsFile); // must do this before if (usrPropsFile != null) @@ -327,8 +339,8 @@ public class Jalview "Invalid jabaws parameter: " + jabawsUrl + " ignored"); } } - } + String defs = aparser.getValue("setprop"); while (defs != null) { @@ -344,6 +356,9 @@ public class Jalview { Cache.setProperty(defs.substring(0, p), defs.substring(p + 1)); } + // DISABLED FOR SECURITY REASONS + // TODO: add a property to allow properties to be overriden by cli args + // Cache.setProperty(defs.substring(0,p), defs.substring(p+1)); } defs = aparser.getValue("setprop"); } @@ -367,91 +382,7 @@ public class Jalview desktop = null; - // property laf = "crossplatform", "system", "gtk", "metal" or "mac" - // If not set (or chosen laf fails), use the normal SystemLaF and if on Mac, - // try Quaqua/Vaqua. - String lafProp = System.getProperty("laf"); - String lafSetting = Cache.getDefault("PREFERRED_LAF", null); - String laf = "none"; - if (lafProp != null) - { - laf = lafProp; - } - else if (lafSetting != null) - { - laf = lafSetting; - } - boolean lafSet = false; - switch (laf) - { - case "crossplatform": - lafSet = setCrossPlatformLookAndFeel(); - if (!lafSet) - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "system": - lafSet = setSystemLookAndFeel(); - if (!lafSet) - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "gtk": - lafSet = setGtkLookAndFeel(); - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "metal": - lafSet = setMetalLookAndFeel(); - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "nimbus": - lafSet = setNimbusLookAndFeel(); - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "quaqua": - lafSet = setQuaquaLookAndFeel(); - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "vaqua": - lafSet = setVaquaLookAndFeel(); - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "mac": - lafSet = setMacLookAndFeel(); - if (!lafSet) - { - System.err.println("Could not set requested laf=" + laf); - } - break; - case "none": - break; - default: - System.err.println("Requested laf=" + laf + " not implemented"); - } - if (!lafSet) - { - setSystemLookAndFeel(); - if (Platform.isLinux() && !Platform.isJS()) - { - setMetalLookAndFeel(); - } - if (Platform.isAMacAndNotJS()) - { - setMacLookAndFeel(); - } - } + setLookAndFeel(); /* * configure 'full' SO model if preferences say to, else use the default (full SO) @@ -465,16 +396,22 @@ public class Jalview if (!headless) { - desktop = new Desktop(); desktop.setInBatchMode(true); // indicate we are starting up try { JalviewTaskbar.setTaskbar(this); + } catch (Exception e) + { + Cache.log.info("Cannot set Taskbar"); + Cache.log.error(e.getMessage()); + // e.printStackTrace(); } catch (Throwable t) { - System.out.println("Error setting Taskbar: " + t.getMessage()); + Cache.log.info("Cannot set Taskbar"); + Cache.log.error(t.getMessage()); + // t.printStackTrace(); } desktop.setVisible(true); @@ -892,29 +829,130 @@ public class Jalview } } - private static boolean setCrossPlatformLookAndFeel() + private static void setLookAndFeel() { - return setGenericLookAndFeel(false); + // property laf = "crossplatform", "system", "gtk", "metal", "nimbus" or + // "mac" + // If not set (or chosen laf fails), use the normal SystemLaF and if on Mac, + // try Quaqua/Vaqua. + String lafProp = System.getProperty("laf"); + String lafSetting = Cache.getDefault("PREFERRED_LAF", null); + String laf = "none"; + if (lafProp != null) + { + laf = lafProp; + } + else if (lafSetting != null) + { + laf = lafSetting; + } + boolean lafSet = false; + switch (laf) + { + case "crossplatform": + lafSet = setCrossPlatformLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "system": + lafSet = setSystemLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "gtk": + lafSet = setGtkLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "metal": + lafSet = setMetalLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "nimbus": + lafSet = setNimbusLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "quaqua": + lafSet = setQuaquaLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "vaqua": + lafSet = setVaquaLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "mac": + lafSet = setMacLookAndFeel(); + if (!lafSet) + { + Cache.log.error("Could not set requested laf=" + laf); + } + break; + case "none": + break; + default: + Cache.log.error("Requested laf=" + laf + " not implemented"); + } + if (!lafSet) + { + setSystemLookAndFeel(); + if (Platform.isLinux()) + { + setMetalLookAndFeel(); + } + if (Platform.isMac()) + { + setMacLookAndFeel(); + } + } } - private static boolean setSystemLookAndFeel() + private static boolean setCrossPlatformLookAndFeel() { - return setGenericLookAndFeel(true); + boolean set = false; + try + { + UIManager.setLookAndFeel( + UIManager.getCrossPlatformLookAndFeelClassName()); + set = true; + } catch (Exception ex) + { + Cache.log.error("Unexpected Look and Feel Exception"); + Cache.log.error(ex.getMessage()); + Cache.log.debug(Cache.getStackTraceString(ex)); + } + return set; } - private static boolean setGenericLookAndFeel(boolean system) + private static boolean setSystemLookAndFeel() { boolean set = false; try { - UIManager.setLookAndFeel( - system ? UIManager.getSystemLookAndFeelClassName() - : UIManager.getCrossPlatformLookAndFeelClassName()); + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); set = true; } catch (Exception ex) { - System.err.println("Unexpected Look and Feel Exception"); - ex.printStackTrace(); + Cache.log.error("Unexpected Look and Feel Exception"); + Cache.log.error(ex.getMessage()); + Cache.log.debug(Cache.getStackTraceString(ex)); } return set; } @@ -940,8 +978,9 @@ public class Jalview set = true; } catch (Exception ex) { - System.err.println("Unexpected Look and Feel Exception"); - ex.printStackTrace(); + Cache.log.error("Unexpected Look and Feel Exception"); + Cache.log.error(ex.getMessage()); + Cache.log.debug(Cache.getStackTraceString(ex)); } return set; } @@ -1031,8 +1070,6 @@ public class Jalview + "-jabaws URL\tSpecify URL for Jabaws services (e.g. for a local installation).\n" + "-fetchfrom nickname\tQuery nickname for features for the alignments and display them.\n" + "-groovy FILE\tExecute groovy script in FILE, after all other arguments have been processed (if FILE is the text 'STDIN' then the file will be read from STDIN)\n" - + "-jvmmempc=PERCENT\tOnly available with standalone executable jar or jalview.bin.Launcher. Limit maximum heap size (memory) to PERCENT% of total physical memory detected. This defaults to 90 if total physical memory can be detected. See https://www.jalview.org/help/html/memory.html for more details.\n" - + "-jvmmemmax=MAXMEMORY\tOnly available with standalone executable jar or jalview.bin.Launcher. Limit maximum heap size (memory) to MAXMEMORY. MAXMEMORY can be specified in bytes, kilobytes(k), megabytes(m), gigabytes(g) or if you're lucky enough, terabytes(t). This defaults to 32g if total physical memory can be detected, or to 8g if total physical memory cannot be detected. See https://www.jalview.org/help/html/memory.html for more details.\n" + "\n~Read documentation in Application or visit http://www.jalview.org for description of Features and Annotations file~\n\n"); } -- 1.7.10.2