From: Ben Soares Date: Wed, 15 May 2024 14:48:14 +0000 (+0100) Subject: JAL-4409 Refactoring to allow easier reuse of classes with getdown X-Git-Tag: Release_2_11_4_0~28^2~30 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=75b5edb658fc39b20f7e1f07285434a0ae38f59e;p=jalview.git JAL-4409 Refactoring to allow easier reuse of classes with getdown --- diff --git a/src/jalview/analytics/Plausible.java b/src/jalview/analytics/Plausible.java index ab2de77..544fb05 100644 --- a/src/jalview/analytics/Plausible.java +++ b/src/jalview/analytics/Plausible.java @@ -44,7 +44,7 @@ import java.util.Random; import jalview.bin.Cache; import jalview.bin.Console; import jalview.util.ChannelProperties; -import jalview.util.HttpUtils; +import jalview.util.UserAgent; public class Plausible { @@ -122,7 +122,7 @@ public class Plausible // random clientId to make User-Agent unique (to register analytic) clientId = String.format("%08x", new Random().nextInt()); - USER_AGENT = HttpUtils.getUserAgent( + USER_AGENT = UserAgent.getUserAgent( MethodHandles.lookup().lookupClass().getCanonicalName() + " " + clientId); } diff --git a/src/jalview/bin/GetMemory.java b/src/jalview/bin/GetMemory.java index 5332704..1bcc841 100644 --- a/src/jalview/bin/GetMemory.java +++ b/src/jalview/bin/GetMemory.java @@ -23,6 +23,8 @@ package jalview.bin; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; +import jalview.util.ErrorLog; + /** * Isolated class to ascertain physical memory of the system using * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize @@ -61,7 +63,7 @@ class GetMemory } catch (NoClassDefFoundError e) { // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM - jalview.bin.Console.errPrintln( + ErrorLog.errPrintln( "No com.sun.management.OperatingSystemMXBean: cannot get total physical memory size"); } diff --git a/src/jalview/bin/HiDPISetting.java b/src/jalview/bin/HiDPISetting.java index e14c032..871a6f7 100644 --- a/src/jalview/bin/HiDPISetting.java +++ b/src/jalview/bin/HiDPISetting.java @@ -23,6 +23,8 @@ package jalview.bin; import java.awt.HeadlessException; import java.util.Locale; +import jalview.util.ErrorLog; + public class HiDPISetting { private static final int hidpiThreshold = 160; @@ -116,9 +118,8 @@ public class HiDPISetting } } catch (NumberFormatException e) { - jalview.bin.Console.errPrintln(setHiDPIScalePropertyName - + " property give (" + setHiDPIScaleProperty - + ") but not parseable as integer"); + ErrorLog.errPrintln(setHiDPIScalePropertyName + " property give (" + + setHiDPIScaleProperty + ") but not parseable as integer"); } } if (setHiDPI && setHiDPIScale > 0) @@ -135,8 +136,8 @@ public class HiDPISetting try { int existingPropertyVal = Integer.parseInt(existingProperty); - jalview.bin.Console.outPrintln("Existing " + scalePropertyName - + " is " + existingPropertyVal); + ErrorLog.outPrintln("Existing " + scalePropertyName + " is " + + existingPropertyVal); if (existingPropertyVal > 1) { setHiDPIScale(existingPropertyVal); @@ -144,7 +145,7 @@ public class HiDPISetting } } catch (NumberFormatException e) { - jalview.bin.Console.outPrintln( + ErrorLog.outPrintln( "Could not convert property " + scalePropertyName + " vale '" + existingProperty + "' to number"); } @@ -162,7 +163,7 @@ public class HiDPISetting { if (isLinux) { - jalview.bin.Console.errPrintln( + ErrorLog.errPrintln( "Cannot get screen resolution: " + e.getMessage()); } } @@ -178,9 +179,8 @@ public class HiDPISetting { if (isLinux) { - jalview.bin.Console - .errPrintln("Cannot get screen size height and width:" - + e.getMessage()); + ErrorLog.errPrintln("Cannot get screen size height and width:" + + e.getMessage()); } } diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index d8e149d..6d28da1 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -104,6 +104,7 @@ import jalview.util.HttpUtils; import jalview.util.LaunchUtils; import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.util.UserAgent; import jalview.ws.jws2.Jws2Discoverer; /** @@ -640,7 +641,7 @@ public class Jalview implements JalviewObjectI { headless = true; } - System.setProperty("http.agent", HttpUtils.getUserAgent()); + System.setProperty("http.agent", UserAgent.getUserAgent()); try { diff --git a/src/jalview/bin/MemorySetting.java b/src/jalview/bin/MemorySetting.java index 738d1e7..dbef0d9 100644 --- a/src/jalview/bin/MemorySetting.java +++ b/src/jalview/bin/MemorySetting.java @@ -24,7 +24,7 @@ package jalview.bin; import java.util.Locale; -import jalview.bin.argparser.Arg; +import jalview.util.ErrorLog; /** * Methods to decide on appropriate memory setting for Jalview based on two @@ -39,11 +39,11 @@ import jalview.bin.argparser.Arg; */ public class MemorySetting { - public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = Arg.JVMMEMPC - .getName(); + // This must match the value of Arg.JVMMEMPC.getName() + public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc"; - public static final String MAX_HEAPSIZE_PROPERTY_NAME = Arg.JVMMEMMAX - .getName(); + // This must match the value of Arg.JVMMEMMAX.getName() + public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax"; private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90% @@ -358,7 +358,7 @@ public class MemorySetting else { // number too big for a Long. Limit to Long.MAX_VALUE - jalview.bin.Console.outPrintln("Memory parsing of '" + memString + ErrorLog.outPrintln("Memory parsing of '" + memString + "' produces number too big. Limiting to Long.MAX_VALUE=" + Long.MAX_VALUE); return Long.MAX_VALUE; @@ -399,7 +399,7 @@ public class MemorySetting ADJUSTMENT_MESSAGE = reason; if (!quiet) { - jalview.bin.Console.outPrintln(reason); + ErrorLog.outPrintln(reason); } } diff --git a/src/jalview/util/ChannelProperties.java b/src/jalview/util/ChannelProperties.java index a1cb1ba..ffcb6a1 100644 --- a/src/jalview/util/ChannelProperties.java +++ b/src/jalview/util/ChannelProperties.java @@ -86,8 +86,8 @@ public class ChannelProperties if (channelPropsURL == null) { // complete failure of channel_properties, set all properties to defaults - jalview.bin.Console.errPrintln("Failed to find '/" - + CHANNEL_PROPERTIES_FILENAME + "' file at '" + ErrorLog.errPrintln("Failed to find '/" + CHANNEL_PROPERTIES_FILENAME + + "' file at '" + (channelPropsURL == null ? "null" : channelPropsURL.toString()) + "'. Using class defaultProps."); @@ -102,7 +102,7 @@ public class ChannelProperties channelPropsIS.close(); } catch (IOException e) { - jalview.bin.Console.errPrintln(e.getMessage()); + ErrorLog.errPrintln(e.getMessage()); // return false; } } @@ -157,10 +157,10 @@ public class ChannelProperties channelProps.load(is); } catch (FileNotFoundException e) { - jalview.bin.Console.errPrintln(e.getMessage()); + ErrorLog.errPrintln(e.getMessage()); } catch (IOException e) { - jalview.bin.Console.errPrintln(e.getMessage()); + ErrorLog.errPrintln(e.getMessage()); } } } @@ -214,8 +214,7 @@ public class ChannelProperties } else { - jalview.bin.Console - .errPrintln("Failed to get channel property '" + key + "'"); + ErrorLog.errPrintln("Failed to get channel property '" + key + "'"); } } return null; @@ -267,7 +266,7 @@ public class ChannelProperties img = imgIcon == null ? null : imgIcon.getImage(); if (img == null) { - jalview.bin.Console.errPrintln( + ErrorLog.errPrintln( "Failed to load channel image " + key + "=" + path); if (!useClassDefaultImage) { @@ -294,7 +293,7 @@ public class ChannelProperties { return urlMap().getOrDefault(key, null); } - jalview.bin.Console.errPrintln( + ErrorLog.errPrintln( "Do not use getImageURL(key) before using getImage(key...)"); } return null; diff --git a/src/jalview/util/ErrorLog.java b/src/jalview/util/ErrorLog.java new file mode 100644 index 0000000..e94b59e --- /dev/null +++ b/src/jalview/util/ErrorLog.java @@ -0,0 +1,58 @@ +package jalview.util; + +public class ErrorLog +{ + private static boolean hasConsole = true; + + public static void outPrintln(String message) + { + println(message, false); + } + + public static void errPrintln(String message) + { + println(message, true); + } + + public static void println(String message, boolean err) + { + if (hasConsole) + { + try + { + hasConsole = jalview.bin.Console.initLogger(); + if (hasConsole) + { + if (err) + { + jalview.bin.Console.errPrintln(message); + } + else + { + jalview.bin.Console.outPrintln(message); + } + } + } catch (Exception e) + { + e.printStackTrace(); + } catch (NoClassDefFoundError t) + { + hasConsole = false; + System.err.println( + "jalview.util.ErrorLog has no jalview.bin.Console. Using System.err and System.out."); + } + } + if (!hasConsole) + { + if (err) + { + System.err.println("jalview.util.ErrorLog: " + message); + } + else + { + System.out.println("jalview.util.ErrorLog: " + message); + + } + } + } +} diff --git a/src/jalview/util/HttpUtils.java b/src/jalview/util/HttpUtils.java index 9fef54e..e600c6c 100644 --- a/src/jalview/util/HttpUtils.java +++ b/src/jalview/util/HttpUtils.java @@ -23,13 +23,12 @@ package jalview.util; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.ProtocolException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; -import javax.ws.rs.HttpMethod; - -import jalview.bin.Cache; - public class HttpUtils { @@ -91,7 +90,7 @@ public class HttpUtils // jalview.bin.Console.outPrintln(System.currentTimeMillis() + " " + url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod(HttpMethod.HEAD); + connection.setRequestMethod("HEAD"); connection.setDoInput(true); connection.setUseCaches(false); connection.setConnectTimeout(300); @@ -205,46 +204,79 @@ public class HttpUtils return is; } - public static String getUserAgent() + /** + * check if a jalview:// scheme URL is given + * + * @param String + * uri + * @return boolean + */ + public static boolean isJalviewSchemeUri(String jalviewUriString) { - return getUserAgent(null); + URI jalviewUri; + try + { + jalviewUri = new URI(jalviewUriString); + } catch (URISyntaxException e) + { + return false; + } + String scheme = jalviewUri.getScheme(); + if (scheme == null || !scheme.startsWith("jalview")) + { + return false; + } + return scheme.length() == 7 // jalview + || scheme.length() == 8 // jalviewX + || scheme.substring(7).equals("http") // jalviewhttp + || scheme.substring(7).equals("https"); // jalviewhttps } - public static String getUserAgent(String className) + /** + * convert a jalview scheme URI to its equivalent URL or path + * + * @param String + * uri + * @return String + */ + public static String equivalentJalviewUrl(String jalviewUriString) { - StringBuilder sb = new StringBuilder(); - sb.append("Jalview"); - sb.append('/'); - sb.append(Cache.getDefault("VERSION", "Unknown")); - sb.append(" ("); - sb.append(System.getProperty("os.name")); - sb.append("; "); - sb.append(System.getProperty("os.arch")); - sb.append(' '); - sb.append(System.getProperty("os.name")); - sb.append(' '); - sb.append(System.getProperty("os.version")); - sb.append("; "); - sb.append("java/"); - sb.append(System.getProperty("java.version")); - sb.append("; "); - sb.append("jalview/"); - sb.append(ChannelProperties.getProperty("channel")); - if (className != null) - { - sb.append("; "); - sb.append(className); + if (!isJalviewSchemeUri(jalviewUriString)) + { + return null; } - String installation = Cache.applicationProperties - .getProperty("INSTALLATION"); - if (installation != null) + URI jalviewUri; + try { - sb.append("; "); - sb.append(installation); + jalviewUri = new URI(jalviewUriString); + } catch (URISyntaxException e) + { + return null; } - sb.append(')'); - sb.append(" help@jalview.org"); - return sb.toString(); + String scheme = jalviewUri.getScheme(); + String host = jalviewUri.getHost(); + if (host != null && host.length() > 0) + { + URI newUri; + try + { + newUri = new URI(scheme.equals("jalviewhttp") ? "http" : "https", + jalviewUri.getUserInfo(), host, jalviewUri.getPort(), + jalviewUri.getPath(), jalviewUri.getQuery(), + jalviewUri.getFragment()); + // return a URL + return newUri.toURL().toString(); + } catch (URISyntaxException | MalformedURLException e) + { + ErrorLog.errPrintln("Trying to convert '" + jalviewUriString + + "' to URL failed"); + } + } + else + { + // return a file path (not a file URI) + return jalviewUri.getPath(); + } + return null; } - } diff --git a/src/jalview/util/LaunchUtils.java b/src/jalview/util/LaunchUtils.java index 404206b..5894fec 100644 --- a/src/jalview/util/LaunchUtils.java +++ b/src/jalview/util/LaunchUtils.java @@ -29,11 +29,8 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Properties; -import jalview.bin.Console; - public class LaunchUtils { - // setting these is LaunchUtils so don't need to import Platform public final static boolean isMac = System.getProperty("os.name") .indexOf("Mac") > -1; @@ -77,7 +74,7 @@ public class LaunchUtils return null; } catch (IOException e) { - jalview.bin.Console.errPrintln(e.getMessage()); + ErrorLog.errPrintln(e.getMessage()); return null; } } @@ -115,23 +112,21 @@ public class LaunchUtils null); if (JCV == null) { - Console.errPrintln( - "Could not obtain JAVA_COMPILE_VERSION for comparison"); + ErrorLog.errPrintln("Could not obtain JAVA_COMPILE_VERSION for comparison"); return -2; } JAVA_COMPILE_VERSION = Integer.parseInt(JCV); } catch (MalformedURLException e) { - jalview.bin.Console.errPrintln("Could not find " + buildDetails); + ErrorLog.errPrintln("Could not find " + buildDetails); return -3; } catch (IOException e) { - jalview.bin.Console.errPrintln("Could not load " + buildDetails); + ErrorLog.errPrintln("Could not load " + buildDetails); return -4; } catch (NumberFormatException e) { - jalview.bin.Console - .errPrintln("Could not parse JAVA_COMPILE_VERSION"); + ErrorLog.errPrintln("Could not parse JAVA_COMPILE_VERSION"); return -5; } @@ -155,7 +150,7 @@ public class LaunchUtils String JV = System.getProperty("java.version"); if (JV == null) { - Console.errPrintln("Could not obtain java.version for comparison"); + ErrorLog.errPrintln("Could not obtain java.version for comparison"); return -2; } if (JV.startsWith("1.")) @@ -166,7 +161,7 @@ public class LaunchUtils : Integer.parseInt(JV.substring(0, JV.indexOf("."))); } catch (NumberFormatException e) { - jalview.bin.Console.errPrintln("Could not parse java.version"); + ErrorLog.errPrintln("Could not parse java.version"); return -3; } return JAVA_VERSION; @@ -187,7 +182,7 @@ public class LaunchUtils if (java_compile_version <= 0 || java_version <= 0) { - Console.errPrintln("Could not make Java version check"); + ErrorLog.errPrintln("Could not make Java version check"); return true; } // Warn if these java.version and JAVA_COMPILE_VERSION conditions exist diff --git a/src/jalview/util/StringUtils.java b/src/jalview/util/StringUtils.java index 89bc36d..42c52a7 100644 --- a/src/jalview/util/StringUtils.java +++ b/src/jalview/util/StringUtils.java @@ -202,18 +202,18 @@ public class StringUtils jv.clear(); if (DEBUG) { - jalview.bin.Console.errPrintln("Array from '" + delimiter + ErrorLog.errPrintln("Array from '" + delimiter + "' separated List:\n" + v.length); for (int i = 0; i < v.length; i++) { - jalview.bin.Console.errPrintln("item " + i + " '" + v[i] + "'"); + ErrorLog.errPrintln("item " + i + " '" + v[i] + "'"); } } return v; } if (DEBUG) { - jalview.bin.Console.errPrintln( + ErrorLog.errPrintln( "Empty Array from '" + delimiter + "' separated List"); } return null; @@ -249,13 +249,13 @@ public class StringUtils { System.err .println("Returning '" + separator + "' separated List:\n"); - jalview.bin.Console.errPrintln(v); + ErrorLog.errPrintln(v.toString()); } return v.toString(); } if (DEBUG) { - jalview.bin.Console.errPrintln( + ErrorLog.errPrintln( "Returning empty '" + separator + "' separated List\n"); } return "" + separator; diff --git a/src/jalview/util/UserAgent.java b/src/jalview/util/UserAgent.java new file mode 100644 index 0000000..b488b00 --- /dev/null +++ b/src/jalview/util/UserAgent.java @@ -0,0 +1,50 @@ +package jalview.util; + +import jalview.bin.Cache; + +public class UserAgent +{ + + public static String getUserAgent(String className) + { + StringBuilder sb = new StringBuilder(); + sb.append("Jalview"); + sb.append('/'); + sb.append(Cache.getDefault("VERSION", "Unknown")); + sb.append(" ("); + sb.append(System.getProperty("os.name")); + sb.append("; "); + sb.append(System.getProperty("os.arch")); + sb.append(' '); + sb.append(System.getProperty("os.name")); + sb.append(' '); + sb.append(System.getProperty("os.version")); + sb.append("; "); + sb.append("java/"); + sb.append(System.getProperty("java.version")); + sb.append("; "); + sb.append("jalview/"); + sb.append(ChannelProperties.getProperty("channel")); + if (className != null) + { + sb.append("; "); + sb.append(className); + } + String installation = Cache.applicationProperties + .getProperty("INSTALLATION"); + if (installation != null) + { + sb.append("; "); + sb.append(installation); + } + sb.append(')'); + sb.append(" help@jalview.org"); + return sb.toString(); + } + + public static String getUserAgent() + { + return getUserAgent(null); + } + +}