From 070cda5fcdc40af294bf8267f9c32104ce9bfd82 Mon Sep 17 00:00:00 2001 From: hansonr Date: Wed, 27 Mar 2019 06:06:38 -0400 Subject: [PATCH] consolidation of Platform isJS calls --- src/jalview/bin/Cache.java | 116 +++++++++++++-------------- src/jalview/gui/AppJmolBinding.java | 25 +++++- src/jalview/io/FileLoader.java | 9 ++- src/jalview/io/FileParse.java | 21 ++--- src/jalview/javascript/web/Client.java | 6 ++ src/jalview/javascript/web/WebResource.java | 61 +++++++------- src/jalview/project/Jalview2XML.java | 17 +--- src/jalview/util/BrowserLauncher.java | 11 +-- src/jalview/util/Platform.java | 14 +++- src/jalview/ws/utils/UrlDownloadClient.java | 10 +-- 10 files changed, 151 insertions(+), 139 deletions(-) diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index e8c3151..e20b75d 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -281,6 +281,8 @@ public class Cache private static boolean propsAreReadOnly = Platform.isJS();/// ** @j2sNative /// true || */false; + private final static String JS_PROPERTY_PREFIX = "jalview_"; + public static void initLogger() { if (log != null) @@ -332,7 +334,7 @@ public class Cache public static void loadProperties(String propsFile) { propertiesFile = propsFile; - if (propsFile == null) + if (propsFile == null && !propsAreReadOnly) { propertiesFile = System.getProperty("user.home") + File.separatorChar + ".jalview_properties"; @@ -343,38 +345,45 @@ public class Cache propsAreReadOnly = true; } - try + if (propertiesFile == null) + { // BH 2019 + Platform.readInfoProperties(JS_PROPERTY_PREFIX, + applicationProperties); + } + else { - InputStream fis; try { - fis = new java.net.URL(propertiesFile).openStream(); - System.out.println( - "Loading jalview properties from : " + propertiesFile); - System.out.println( - "Disabling Jalview writing to user's local properties file."); - propsAreReadOnly = true; + InputStream fis; + try + { + fis = new java.net.URL(propertiesFile).openStream(); + System.out.println( + "Loading jalview properties from : " + propertiesFile); + System.out.println( + "Disabling Jalview writing to user's local properties file."); + propsAreReadOnly = true; + + } catch (Exception ex) + { + fis = null; + } + if (fis == null) + { + fis = new FileInputStream(propertiesFile); + } + applicationProperties.clear(); + applicationProperties.load(fis); + + // remove any old build properties + deleteBuildProperties(); + fis.close(); } catch (Exception ex) { - fis = null; + System.out.println("Error reading properties file: " + ex); } - if (fis == null) - { - fis = new FileInputStream(propertiesFile); - } - applicationProperties.clear(); - applicationProperties.load(fis); - - // remove any old build properties - - deleteBuildProperties(); - fis.close(); - } catch (Exception ex) - { - System.out.println("Error reading properties file: " + ex); } - if (getDefault("USE_PROXY", false)) { String proxyServer = getDefault("PROXY_SERVER", ""), @@ -388,69 +397,57 @@ public class Cache } // LOAD THE AUTHORS FROM THE authors.props file - boolean ignore = Platform.isJS(); - if (!ignore) - /** - * @j2sNative - */ + String authorDetails = (Platform.isJS() ? null + : "jar:".concat(Cache.class.getProtectionDomain() + .getCodeSource().getLocation().toString() + .concat("!/authors.props"))); + + try { - try + if (authorDetails != null) { - String authorDetails = "jar:" - .concat(Cache.class.getProtectionDomain().getCodeSource() - .getLocation().toString().concat("!/authors.props")); - java.net.URL localJarFileURL = new java.net.URL(authorDetails); - InputStream in = localJarFileURL.openStream(); applicationProperties.load(in); in.close(); - - } catch (Exception ex) - { - System.out.println("Error reading author details: " + ex); - ignore = true; } + } catch (Exception ex) + { + System.out.println("Error reading author details: " + ex); + authorDetails = null; } - - if (ignore) { + if (authorDetails == null) + { applicationProperties.remove("AUTHORS"); applicationProperties.remove("AUTHORFNAMES"); applicationProperties.remove("YEAR"); } - + // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW - // BH 2019.01.25 switching to Platform.isJS() - ignore = Platform.isJS(); - if (!ignore) - /** - * @j2sNative - */ + String buildDetails = (Platform.isJS() ? null + : "jar:".concat(Cache.class.getProtectionDomain() + .getCodeSource().getLocation().toString() + .concat("!/.build_properties"))); + if (buildDetails != null) { try { - String buildDetails = "jar:".concat(Cache.class.getProtectionDomain() - .getCodeSource().getLocation().toString() - .concat("!/.build_properties")); - java.net.URL localJarFileURL = new java.net.URL(buildDetails); - InputStream in = localJarFileURL.openStream(); applicationProperties.load(in); in.close(); } catch (Exception ex) { System.out.println("Error reading build details: " + ex); - ignore = true; + buildDetails = null; } } - - if (ignore) { + if (buildDetails == null) + { applicationProperties.remove("VERSION"); } - String jnlpVersion = System.getProperty("jalview.version"); String codeVersion = getProperty("VERSION"); String codeInstallation = getProperty("INSTALLATION"); @@ -576,6 +573,7 @@ public class Cache false); } + private static void deleteBuildProperties() { applicationProperties.remove("LATEST_VERSION"); diff --git a/src/jalview/gui/AppJmolBinding.java b/src/jalview/gui/AppJmolBinding.java index eeb4888..c41608e 100644 --- a/src/jalview/gui/AppJmolBinding.java +++ b/src/jalview/gui/AppJmolBinding.java @@ -28,17 +28,20 @@ import jalview.datamodel.SequenceI; import jalview.ext.jmol.JalviewJmolBinding; import jalview.io.DataSourceType; import jalview.structure.StructureSelectionManager; +import jalview.util.Platform; import java.awt.Container; +import java.io.File; +import java.util.List; import java.util.Map; import org.jmol.api.JmolAppConsoleInterface; + import javajs.util.BS; -import org.openscience.jmol.app.jmolpanel.console.AppConsole; public class AppJmolBinding extends JalviewJmolBinding { - private AppJmol appJmolWindow; + protected AppJmol appJmolWindow; public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm, PDBEntry[] pdbentry, SequenceI[][] sequenceIs, @@ -215,4 +218,22 @@ public class AppJmolBinding extends JalviewJmolBinding return null; } + + @SuppressWarnings("unused") + public void cacheFiles(List files) + { + if (files == null) + { + return; + } + for (File f : files) + { + String path = f.getAbsolutePath(); + byte[] data = Platform.getFileBytes(f); + if (data != null) + { + Platform.cacheFileData(path, data); + } + } + } } diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index f244d14..2169715 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -39,6 +39,7 @@ import jalview.project.Jalview2XML; import jalview.schemes.ColourSchemeI; import jalview.structure.StructureSelectionManager; import jalview.util.MessageManager; +import jalview.util.Platform; import jalview.ws.utils.UrlDownloadClient; import java.io.BufferedReader; @@ -47,7 +48,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Vector; @@ -649,11 +649,14 @@ public class FileLoader implements Runnable @SuppressWarnings("unused") public static BufferedReader getBuffereReader(Object file) throws FileNotFoundException { if (file instanceof String) + { return new BufferedReader(new FileReader((String) file)); - - byte[] bytes = /** @j2sNative file._bytes || */ null; + } + byte[] bytes = Platform.getFileBytes((File) file); if (bytes != null) + { return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes))); + } return new BufferedReader(new FileReader((File) file)); } diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index 809bc8f..13c0f43 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -25,6 +25,7 @@ import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureSettingsModelI; import jalview.util.MessageManager; +import jalview.util.Platform; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -333,13 +334,8 @@ public class FileParse private void parse(File file, String fileStr, DataSourceType sourceType, boolean isFileObject) throws MalformedURLException, IOException { - /** - * @j2sNative - * - * this.bytes = file && file._bytes; - * - */ - this.dataSourceType = sourceType; + bytes = Platform.getFileBytes(file); + dataSourceType = sourceType; error = false; if (sourceType == DataSourceType.FILE) @@ -375,15 +371,8 @@ public class FileParse } else if (sourceType == DataSourceType.RELATIVE_URL) { - /** - * BH 2018 hack for no support for access-origin - * - * @j2sNative - * - * this.bytes = swingjs.JSUtil.getFileAsBytes$O(fileStr); - * - */ - + // BH 2018 hack for no support for access-origin + bytes = Platform.getFileAsBytes(fileStr); dataIn = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes))); dataName = fileStr; diff --git a/src/jalview/javascript/web/Client.java b/src/jalview/javascript/web/Client.java index d33d62c..1fe35f5 100644 --- a/src/jalview/javascript/web/Client.java +++ b/src/jalview/javascript/web/Client.java @@ -2,6 +2,12 @@ package jalview.javascript.web; import com.sun.jersey.api.client.config.ClientConfig; +/** + * A JavaScript-only proxy for com.sun.jersey.api.client.Client + * + * @author hansonr + * + */ public class Client { diff --git a/src/jalview/javascript/web/WebResource.java b/src/jalview/javascript/web/WebResource.java index 6d12932..c55e9ba 100644 --- a/src/jalview/javascript/web/WebResource.java +++ b/src/jalview/javascript/web/WebResource.java @@ -1,29 +1,28 @@ package jalview.javascript.web; +import jalview.util.Platform; + import java.net.URI; import java.net.URISyntaxException; -/* this class is a proxy for +/* + * A JavaScript-only proxy for com.sun.jersey.api.client.WebResource * */ public class WebResource { private String endpoint, params = ""; - - public WebResource(String endpoint) { + + public WebResource(String endpoint) + { this.endpoint = endpoint; } - public WebResource queryParam(String key, String value) { - params += (params == "" ? "?" : "&") + key + "="; - /** - * @j2sNative - * value = encodeURIComponent(value); - */ - params += value; + params += (params == "" ? "?" : "&") + key + "=" + + Platform.encodeURI(value); return this; } @@ -41,31 +40,33 @@ public class WebResource public Builder accept(String... encoding) { - return new Builder(getURI(), encoding); + return new Builder(getURI(), encoding); } - - - public static class Builder { - private URI uri; - private String[] encoding; - public Builder(URI uri, String... encoding) + public static class Builder + { + private URI uri; + + private String[] encoding; + + public Builder(URI uri, String... encoding) { - this.uri = uri; - this.encoding = encoding; // application/json - - // TODO Auto-generated constructor stub + this.uri = uri; + this.encoding = encoding; // application/json } - public ClientResponse get(Class c) { - String data = uri.toString(); - // c will be ClientResponse - data = /** @j2sNative swingjs.JSUtil.getFileAsString$S(data) || */ null; - return new ClientResponse(data, encoding); - } + /** + * Get the response + * + * @param c + * must be ClientResponse + * @return + */ + public ClientResponse get(Class c) + { + return new ClientResponse(Platform.getFileAsString(uri.toString()), + encoding); + } } - - - } diff --git a/src/jalview/project/Jalview2XML.java b/src/jalview/project/Jalview2XML.java index fa104a6..41dd2f9 100644 --- a/src/jalview/project/Jalview2XML.java +++ b/src/jalview/project/Jalview2XML.java @@ -214,21 +214,9 @@ public class Jalview2XML // BH 2018 we add the .jvp binary extension to J2S so that // it will declare that binary when we do the file save from the browser - private static void addJ2SBinaryType(String ext) - { - ext = "." + ext + "?"; - - /** - * @j2sNative - * - * J2S._binaryTypes.push(ext); - * - */ - } - static { - addJ2SBinaryType(".jvp?"); + Platform.addJ2SBinaryType(".jvp?"); } private static final String VIEWER_PREFIX = "viewer_"; @@ -2791,8 +2779,7 @@ public class Jalview2XML // BH 2018 allow for bytes already attached to File object try { String file = (ofile instanceof File ? ((File) ofile).getCanonicalPath() : ofile.toString()); - byte[] bytes = /** @j2sNative ofile._bytes || */ - null; + byte[] bytes = Platform.getFileBytes((File) ofile); URL url = null; errorMessage = null; uniqueSetSuffix = null; diff --git a/src/jalview/util/BrowserLauncher.java b/src/jalview/util/BrowserLauncher.java index a55a65c..f47ca23 100755 --- a/src/jalview/util/BrowserLauncher.java +++ b/src/jalview/util/BrowserLauncher.java @@ -735,14 +735,15 @@ public class BrowserLauncher public static void openURL(String url) throws IOException { + if (Platform.openURL(url)) + { + return; + } /** - * @j2sNative - * - * window.open(url); - * + * Java only * + * @j2sNative */ - { if (!loadedWithoutErrors) diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index e4ee197..118269a 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -349,14 +349,26 @@ public class Platform */ } + /** + * Encode the URI using JavaScript encodeURIComponent + * + * @param value + * @return encoded value + */ public static String encodeURI(String value) { /** - * @j2sNative return encodeURIComponent(value); + * @j2sNative value = encodeURIComponent(value); */ return value; } + /** + * Open the URL using a simple window call if this is JavaScript + * + * @param url + * @return true if window has been opened + */ public static boolean openURL(String url) { if (!isJS()) diff --git a/src/jalview/ws/utils/UrlDownloadClient.java b/src/jalview/ws/utils/UrlDownloadClient.java index 055df15..58632f2 100644 --- a/src/jalview/ws/utils/UrlDownloadClient.java +++ b/src/jalview/ws/utils/UrlDownloadClient.java @@ -23,7 +23,6 @@ package jalview.ws.utils; import jalview.util.Platform; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -116,13 +115,8 @@ public class UrlDownloadClient public static void download(String urlstring, File tempFile) throws IOException { - // BH 2019.01.25 this said "...getFileAsBytes(..." which cannot work. - // maybe we cannot get here? - if (Platform.isJS()) { - /** @j2sNative - * tempFile._bytes = swingjs.JSUtil.getFileAsBytes$O(urlstring); - */ - } else { + if (!Platform.setFileBytes(tempFile, urlstring)) + { download(urlstring, tempFile.toString()); } } -- 1.7.10.2