JAL-3348 prepping getdown to pass JVM command line args
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / data / Application.java
index 501407c..799b5bb 100644 (file)
@@ -23,14 +23,16 @@ import java.util.concurrent.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.zip.GZIPInputStream;
-import com.sun.management.OperatingSystemMXBean;
-import java.lang.management.ManagementFactory;
 
+import jalview.bin.MemorySetting;
 
 import com.threerings.getdown.util.*;
 // avoid ambiguity with java.util.Base64 which we can't use as it's 1.8+
 import com.threerings.getdown.util.Base64;
 
+import com.threerings.getdown.data.EnvConfig;
+import com.threerings.getdown.data.EnvConfig.Note;
+
 import static com.threerings.getdown.Log.log;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
@@ -42,6 +44,9 @@ public class Application
 {
     /** The name of our configuration file. */
     public static final String CONFIG_FILE = "getdown.txt";
+    
+    /** Dir where a backup config file might reside */ 
+    public static final String BACKUP_CONFIG_DIR = "install";
 
     /** The name of our target version file. */
     public static final String VERSION_FILE = "version.txt";
@@ -65,14 +70,20 @@ public class Application
          */
         public enum Step
         {
-            UPDATE_JAVA(10),
-            VERIFY_METADATA(15, 65, 95),
-            DOWNLOAD(40),
+            //UPDATE_JAVA(10),
+            UPDATE_JAVA(20),
+            //VERIFY_METADATA(15, 65, 95),
+            VERIFY_METADATA(15, 45, 90),
+            DOWNLOAD(60),
             PATCH(60),
-            VERIFY_RESOURCES(70, 97),
-            REDOWNLOAD_RESOURCES(90),
-            UNPACK(98),
-            LAUNCH(99);
+            //VERIFY_RESOURCES(70, 97),
+            VERIFY_RESOURCES(40, 90),
+            //REDOWNLOAD_RESOURCES(90),
+            REDOWNLOAD_RESOURCES(80),
+            //UNPACK(98),
+            UNPACK(95),
+            //LAUNCH(99);
+            LAUNCH(100);
 
             /** What is the final percent value for this step? */
             public final List<Integer> defaultPercents;
@@ -99,6 +110,9 @@ public class Application
         /** The paths (relative to the appdir) of images for the window icon. */
         public final List<String> iconImages;
 
+        /** The path (relative to the appdir) to a single background image to appear first. */
+        public final String instantBackgroundImage;
+
         /** The path (relative to the appdir) to a single background image. */
         public final String backgroundImage;
 
@@ -138,6 +152,21 @@ public class Application
         /** Whether progress text should be hidden or not. */
         public final boolean hideProgressText;
 
+        /** Whether the splash screen should update non-asynchronously before being shown. */
+        public final boolean progressSync;
+
+        /** Whether the splash screen should update non-asynchronously after being shown. */
+        public final boolean progressSyncAfterShown;
+
+        /** Whether the splash screen should retain focus. */
+        public final boolean keepOnTop;
+
+        /** Whether to display the appbase. */
+        public final boolean displayAppbase;
+
+        /** Whether to display the version. */
+        public final boolean displayVersion;
+
         /** The minimum number of seconds to display the GUI. This is to prevent the GUI from
           * flashing up on the screen and immediately disappearing, which can be confusing to the
           * user. */
@@ -151,12 +180,14 @@ public class Application
         @Override
         public String toString ()
         {
-            return "[name=" + name + ", bg=" + background + ", bg=" + backgroundImage +
+            return "[name=" + name + ", bg=" + background + ", bg=" + backgroundImage + ", instant_bg=" + instantBackgroundImage +
                 ", pi=" + progressImage + ", prect=" + progress + ", pt=" + progressText +
                 ", pb=" + progressBar + ", srect=" + status + ", st=" + statusText +
                 ", shadow=" + textShadow + ", err=" + installError + ", nrect=" + patchNotes +
                 ", notes=" + patchNotesUrl + ", stepPercentages=" + stepPercentages +
-                ", hideProgressText" + hideProgressText + ", minShow=" + minShowSeconds + "]";
+                ", hideProgressText=" + hideProgressText + ", keepOnTop=" + keepOnTop + ", progressSync=" + progressSync +
+                ", progressSyncAfterShown=" + progressSyncAfterShown + ", minShow=" + minShowSeconds +
+                ", displayAppbase=" + displayAppbase + ", displayVersion=" + displayVersion + "]";
         }
 
         public UpdateInterface (Config config)
@@ -164,7 +195,12 @@ public class Application
             this.name = config.getString("ui.name");
             this.progress = config.getRect("ui.progress", new Rectangle(5, 5, 300, 15));
             this.progressText = config.getColor("ui.progress_text", Color.BLACK);
-            this.hideProgressText =  config.getBoolean("ui.hide_progress_text");
+            this.hideProgressText = config.getBoolean("ui.hide_progress_text");
+            this.progressSync = config.getBoolean("ui.progress_sync_before_shown");
+            this.progressSyncAfterShown = config.getBoolean("ui.progress_sync_after_shown");
+            this.keepOnTop =  config.getBoolean("ui.keep_on_top");
+            this.displayAppbase =  config.getBoolean("ui.display_appbase");
+            this.displayVersion =  config.getBoolean("ui.display_version");
             this.minShowSeconds = config.getInt("ui.min_show_seconds", 5);
             this.progressBar = config.getColor("ui.progress_bar", 0x6699CC);
             this.status = config.getRect("ui.status", new Rectangle(5, 25, 500, 100));
@@ -172,6 +208,7 @@ public class Application
             this.textShadow = config.getColor("ui.text_shadow", Color.CLEAR);
             this.hideDecorations = config.getBoolean("ui.hide_decorations");
             this.backgroundImage = config.getString("ui.background_image");
+            this.instantBackgroundImage = config.getString("ui.instant_background_image");
             // default to black or white bg color, depending on the brightness of the progressText
             int defaultBackground = (0.5f < Color.brightness(this.progressText)) ?
                 Color.BLACK : Color.WHITE;
@@ -246,15 +283,16 @@ public class Application
      *
      */
     public Application (EnvConfig envc) {
-        _envc = envc;
-        _config = getLocalPath(envc.appDir, CONFIG_FILE);
+       _envc = envc;
+       _config = getLocalPath(envc.appDir, CONFIG_FILE);
+       _backupConfig = getLocalPath(envc.appDir, BACKUP_CONFIG_DIR+File.separator+CONFIG_FILE);
     }
 
     /**
      * Returns the configured application directory.
      */
     public File getAppDir () {
-        return _envc.appDir;
+       return _envc.appDir;
     }
 
     /**
@@ -263,7 +301,7 @@ public class Application
      */
     public boolean useCodeCache ()
     {
-        return _useCodeCache;
+       return _useCodeCache;
     }
 
     /**
@@ -272,7 +310,7 @@ public class Application
      */
     public int getCodeCacheRetentionDays ()
     {
-        return _codeCacheRetentionDays;
+       return _codeCacheRetentionDays;
     }
 
     /**
@@ -280,7 +318,7 @@ public class Application
      * app files from its hosting server.
      */
     public int maxConcurrentDownloads () {
-        return _maxConcDownloads;
+       return _maxConcDownloads;
     }
 
     /**
@@ -288,11 +326,11 @@ public class Application
      */
     public Resource getConfigResource ()
     {
-        try {
-            return createResource(CONFIG_FILE, Resource.NORMAL);
-        } catch (Exception e) {
-            throw new RuntimeException("Invalid appbase '" + _vappbase + "'.", e);
-        }
+       try {
+               return createResource(CONFIG_FILE, Resource.NORMAL);
+       } catch (Exception e) {
+               throw new RuntimeException("Invalid appbase '" + _vappbase + "'.", e);
+       }
     }
 
     /**
@@ -300,7 +338,7 @@ public class Application
      */
     public List<Resource> getCodeResources ()
     {
-        return _codes;
+       return _codes;
     }
 
     /**
@@ -448,7 +486,8 @@ public class Application
             return null;
         }
 
-        String vmfile = LaunchUtil.LOCAL_JAVA_DIR + ".jar";
+        String extension = (_javaLocation.endsWith(".tgz"))?".tgz":".jar";
+        String vmfile = LaunchUtil.LOCAL_JAVA_DIR + extension;
                log.info("vmfile is '"+vmfile+"'");
                System.out.println("vmfile is '"+vmfile+"'");
         try {
@@ -564,7 +603,28 @@ public class Application
                 log.info("Found no getdown.txt file", "appdir", getAppDir());
             }
         } catch (Exception e) {
-            log.warning("Failure reading config file", "file", config, e);
+            log.warning("Failure reading config file", "file", _config, e);
+        }
+        if (config == null || config.getString("appbase") == null || config.getString("appbase").isEmpty()) {
+               try {
+                       Config backupConfig = Config.parseConfig(_backupConfig, opts);
+                       config = backupConfig;
+                       log.warning("Using backup config file", "appdir", getAppDir(), "backupConfig", _backupConfig.getAbsoluteFile());
+               } catch (Exception e) {
+                       log.warning("Failure reading backup config file", "file", _backupConfig, e);
+               }
+        }
+        
+        // see if there's an override config from locator file
+        Config locatorConfig = createLocatorConfig(opts);
+        
+        // merge the locator file config into config (or replace config with)
+        if (locatorConfig != null) {
+          if (config == null || locatorConfig.getBoolean(LOCATOR_FILE_EXTENSION+"_replace")) {
+            config = locatorConfig;
+          } else {
+            config.mergeConfig(locatorConfig, locatorConfig.getBoolean(LOCATOR_FILE_EXTENSION+"_merge"));
+          }
         }
 
         // if we failed to read our config file, check for an appbase specified via a system
@@ -580,6 +640,12 @@ public class Application
         // first determine our application base, this way if anything goes wrong later in the
         // process, our caller can use the appbase to download a new configuration file
         _appbase = config.getString("appbase");
+        
+        // see if locatorConfig override
+        if (locatorConfig != null && !StringUtil.isBlank(locatorConfig.getString("appbase"))) {
+          _appbase = locatorConfig.getString("appbase");
+        }
+        
         if (_appbase == null) {
             throw new RuntimeException("m.missing_appbase");
         }
@@ -731,28 +797,34 @@ public class Application
             jvmmempc = config.getInt(appPrefix + "jvmmempc", jvmmempc);
         }
         if (0 <= jvmmempc && jvmmempc <= 100) {
-            final Object o = ManagementFactory.getOperatingSystemMXBean();
-
-            try {
-                if (o instanceof OperatingSystemMXBean) {
-                    final OperatingSystemMXBean osb = (OperatingSystemMXBean) o;
-                    long physicalMem = osb.getTotalPhysicalMemorySize();
-                    long requestedMem = physicalMem*jvmmempc/100;
-                    String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(requestedMem)};
-                    // remove other max heap size arg
-                    ARG: for (int i = 0; i < _jvmargs.size(); i++) {
-                           if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
-                                _jvmargs.remove(i);
-                           }
-                    }
-                    addAll(maxMemHeapArg, _jvmargs);
-
-                }
-            }
-            catch (NoClassDefFoundError e) {
-                // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
-                System.out.println("No com.sun.management.OperatingSystemMXBean. Cannot use 'jvmmempc'.");
+          
+          long maxMemLong = -1;
+
+          try
+          {
+            maxMemLong = MemorySetting.memPercent(jvmmempc);
+          } catch (Exception e)
+          {
+            e.printStackTrace();
+          } catch (Throwable t)
+          {
+            t.printStackTrace();
+          }
+
+          if (maxMemLong > 0)
+          {
+            
+            String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(maxMemLong)};
+            // remove other max heap size arg
+            ARG: for (int i = 0; i < _jvmargs.size(); i++) {
+              if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
+                _jvmargs.remove(i);
+              }
             }
+            addAll(maxMemHeapArg, _jvmargs);
+            
+          }
+
         } else if (jvmmempc != -1) {
           System.out.println("'jvmmempc' value must be in range 0 to 100 (read as '"+Integer.toString(jvmmempc)+"')");
         }
@@ -766,7 +838,7 @@ public class Application
 
         // add the launch specific application arguments
         _appargs.addAll(_envc.appArgs);
-
+        
         // look for custom arguments
         fillAssignmentListFromPairs("extra.txt", _txtJvmArgs);
 
@@ -1042,11 +1114,32 @@ public class Application
             args.add(_class);
         }
 
+        // almost finally check the startup file arguments
+        for (File f : _startupFiles) {
+          _appargs.add(f.getAbsolutePath());
+          break; // Only add one file to open
+        }
+        
+        // check if one arg with recognised extension
+        if ( _appargs.size() == 1 && _appargs.get(0) != null ) {
+          String filename = _appargs.get(0);
+          String ext = null;
+          int j = filename.lastIndexOf('.');
+          if (j > -1) {
+            ext = filename.substring(j+1);
+          }
+          if (ext != null && LOCATOR_FILE_EXTENSION.equals(ext.toLowerCase())) {
+            // this file extension should have been dealt with in Getdown class
+          } else {
+            _appargs.add(0, "-open");
+          }
+        }
+
         // finally add the application arguments
         for (String string : _appargs) {
             args.add(processArg(string));
         }
-
+        
         String[] envp = createEnvironment();
         String[] sargs = args.toArray(new String[args.size()]);
         log.info("Running " + StringUtil.join(sargs, "\n  "));
@@ -1760,8 +1853,99 @@ public class Application
         return new File(appdir, path);
     }
 
+    public static void setStartupFilesFromParameterString(String p) {
+      // multiple files *might* be passed in as space separated quoted filenames
+      String q = "\"";
+      if (!StringUtil.isBlank(p)) {
+        String[] filenames;
+        // split quoted params or treat as single string array
+        if (p.startsWith(q) && p.endsWith(q)) {
+          // this fails if, e.g.
+          // p=q("stupidfilename\" " "otherfilename")
+          // let's hope no-one ever ends a filename with '" '
+          filenames = p.substring(q.length(),p.length()-q.length()).split(q+" "+q);
+        } else {
+          // single unquoted filename
+          filenames = new String[]{p};
+        }
+
+        // check for locator file.  Only allow one locator file to be double clicked (if multiple files opened, ignore locator files)
+        String locatorFilename = filenames.length >= 1 ? filenames[0] : null;
+        if (
+                !StringUtil.isBlank(locatorFilename)
+                && locatorFilename.toLowerCase().endsWith("."+Application.LOCATOR_FILE_EXTENSION)
+                ) {
+          setLocatorFile(locatorFilename);
+          // remove the locator filename from the filenames array
+          String[] otherFilenames = new String[filenames.length - 1];
+          System.arraycopy(filenames, 1, otherFilenames, 0, otherFilenames.length);
+          filenames = otherFilenames;
+        }
+
+        for (int i = 0; i < filenames.length; i++) {
+          String filename = filenames[i];
+          // skip any other locator files in a multiple file list
+          if (! filename.toLowerCase().endsWith("."+Application.LOCATOR_FILE_EXTENSION)) {
+            addStartupFile(filename);
+          }
+        }
+      }
+    }
+    
+    public static void setLocatorFile(String filename) {
+      _locatorFile = new File(filename);
+    }
+    
+    public static void addStartupFile(String filename) {
+      _startupFiles.add(new File(filename));
+    }
+    
+    private Config createLocatorConfig(Config.ParseOpts opts) {
+      if (_locatorFile == null) {
+        return null;
+      }
+      
+      Config locatorConfig = null;
+      
+      try {
+        Config tmpConfig = null;
+        Map<String, Object> tmpData = new HashMap<>();
+        if (_locatorFile.exists()) {
+          tmpConfig = Config.parseConfig(_locatorFile,  opts);
+          // appbase is sanitised in HostWhitelist
+          Map<String, Object> tmpConfigData = tmpConfig.getData();
+          if (tmpConfig != null) {
+            for (Map.Entry<String, Object> entry : tmpConfigData.entrySet()) {
+              String key = entry.getKey();
+              Object value = entry.getValue();
+              String mkey = key.indexOf('.') > -1 ? key.substring(key.indexOf('.') + 1) : key;
+              if (Config.allowedReplaceKeys.contains(mkey) || Config.allowedMergeKeys.contains(mkey)) {
+                tmpData.put(key, value);
+              }
+            }
+          } else {
+            log.warning("Error occurred reading config file", "file", _locatorFile);
+          }
+        } else {
+          log.warning("Given locator file does not exist", "file", _locatorFile);
+        }
+        
+        locatorConfig = new Config(tmpData);
+        
+      } catch (Exception e) {
+        log.warning("Failure reading locator file",  "file", _locatorFile, e);
+      }
+      
+      return locatorConfig;
+    }
+    
+    public String getAppbase() {
+       return _appbase;
+    }
+    
     protected final EnvConfig _envc;
     protected File _config;
+    protected File _backupConfig;
     protected Digest _digest;
 
     protected long _version = -1;
@@ -1823,4 +2007,8 @@ public class Application
 
     protected static final String ENV_VAR_PREFIX = "%ENV.";
     protected static final Pattern ENV_VAR_PATTERN = Pattern.compile("%ENV\\.(.*?)%");
+    protected static File _locatorFile;
+    protected static List<File> _startupFiles = new ArrayList<>();
+    public static final String LOCATOR_FILE_EXTENSION = "jvl";
 }