JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / data / Application.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.data;
7
8 import java.io.*;
9 import java.lang.reflect.Method;
10 import java.net.MalformedURLException;
11 import java.net.Proxy;
12 import java.net.URL;
13 import java.net.URLClassLoader;
14 import java.net.URLConnection;
15 import java.net.URLEncoder;
16 import java.nio.channels.FileChannel;
17 import java.nio.channels.FileLock;
18 import java.nio.channels.OverlappingFileLockException;
19 import java.security.*;
20 import java.security.cert.Certificate;
21 import java.util.*;
22 import java.util.concurrent.*;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import java.util.zip.GZIPInputStream;
26 import com.sun.management.OperatingSystemMXBean;
27 import java.lang.management.ManagementFactory;
28
29
30 import com.threerings.getdown.util.*;
31 // avoid ambiguity with java.util.Base64 which we can't use as it's 1.8+
32 import com.threerings.getdown.util.Base64;
33
34 import static com.threerings.getdown.Log.log;
35 import static java.nio.charset.StandardCharsets.UTF_8;
36
37 /**
38  * Parses and provide access to the information contained in the <code>getdown.txt</code>
39  * configuration file.
40  */
41 public class Application
42 {
43     /** The name of our configuration file. */
44     public static final String CONFIG_FILE = "getdown.txt";
45
46     /** The name of our target version file. */
47     public static final String VERSION_FILE = "version.txt";
48
49     /** System properties that are prefixed with this string will be passed through to our
50      * application (minus this prefix). */
51     public static final String PROP_PASSTHROUGH_PREFIX = "app.";
52
53     /** Suffix used for control file signatures. */
54     public static final String SIGNATURE_SUFFIX = ".sig";
55
56     /** A special classname that means 'use -jar code.jar' instead of a classname. */
57     public static final String MANIFEST_CLASS = "manifest";
58
59     /** Used to communicate information about the UI displayed when updating the application. */
60     public static final class UpdateInterface
61     {
62         /**
63          * The major steps involved in updating, along with some arbitrary percentages
64          * assigned to them, to mark global progress.
65          */
66         public enum Step
67         {
68             UPDATE_JAVA(10),
69             VERIFY_METADATA(15, 65, 95),
70             DOWNLOAD(40),
71             PATCH(60),
72             VERIFY_RESOURCES(70, 97),
73             REDOWNLOAD_RESOURCES(90),
74             UNPACK(98),
75             LAUNCH(99);
76
77             /** What is the final percent value for this step? */
78             public final List<Integer> defaultPercents;
79
80             /** Enum constructor. */
81             Step (int... percents)
82             {
83                 this.defaultPercents = intsToList(percents);
84             }
85         }
86
87         /** The human readable name of this application. */
88         public final String name;
89
90         /** A background color, just in case. */
91         public final int background;
92
93         /** Background image specifiers for `RotatingBackgrounds`. */
94         public final List<String> rotatingBackgrounds;
95
96         /** The error background image for `RotatingBackgrounds`. */
97         public final String errorBackground;
98
99         /** The paths (relative to the appdir) of images for the window icon. */
100         public final List<String> iconImages;
101
102         /** The path (relative to the appdir) to a single background image. */
103         public final String backgroundImage;
104
105         /** The path (relative to the appdir) to the progress bar image. */
106         public final String progressImage;
107
108         /** The dimensions of the progress bar. */
109         public final Rectangle progress;
110
111         /** The color of the progress text. */
112         public final int progressText;
113
114         /** The color of the progress bar. */
115         public final int progressBar;
116
117         /** The dimensions of the status display. */
118         public final Rectangle status;
119
120         /** The color of the status text. */
121         public final int statusText;
122
123         /** The color of the text shadow. */
124         public final int textShadow;
125
126         /** Where to point the user for help with install errors. */
127         public final String installError;
128
129         /** The dimensions of the patch notes button. */
130         public final Rectangle patchNotes;
131
132         /** The patch notes URL. */
133         public final String patchNotesUrl;
134
135         /** Whether window decorations are hidden for the UI. */
136         public final boolean hideDecorations;
137
138         /** Whether progress text should be hidden or not. */
139         public final boolean hideProgressText;
140
141         /** The minimum number of seconds to display the GUI. This is to prevent the GUI from
142           * flashing up on the screen and immediately disappearing, which can be confusing to the
143           * user. */
144         public final int minShowSeconds;
145
146         /** The global percentages for each step. A step may have more than one, and
147          * the lowest reasonable one is used if a step is revisited. */
148         public final Map<Step, List<Integer>> stepPercentages;
149
150         /** Generates a string representation of this instance. */
151         @Override
152         public String toString ()
153         {
154             return "[name=" + name + ", bg=" + background + ", bg=" + backgroundImage +
155                 ", pi=" + progressImage + ", prect=" + progress + ", pt=" + progressText +
156                 ", pb=" + progressBar + ", srect=" + status + ", st=" + statusText +
157                 ", shadow=" + textShadow + ", err=" + installError + ", nrect=" + patchNotes +
158                 ", notes=" + patchNotesUrl + ", stepPercentages=" + stepPercentages +
159                 ", hideProgressText" + hideProgressText + ", minShow=" + minShowSeconds + "]";
160         }
161
162         public UpdateInterface (Config config)
163         {
164             this.name = config.getString("ui.name");
165             this.progress = config.getRect("ui.progress", new Rectangle(5, 5, 300, 15));
166             this.progressText = config.getColor("ui.progress_text", Color.BLACK);
167             this.hideProgressText =  config.getBoolean("ui.hide_progress_text");
168             this.minShowSeconds = config.getInt("ui.min_show_seconds", 5);
169             this.progressBar = config.getColor("ui.progress_bar", 0x6699CC);
170             this.status = config.getRect("ui.status", new Rectangle(5, 25, 500, 100));
171             this.statusText = config.getColor("ui.status_text", Color.BLACK);
172             this.textShadow = config.getColor("ui.text_shadow", Color.CLEAR);
173             this.hideDecorations = config.getBoolean("ui.hide_decorations");
174             this.backgroundImage = config.getString("ui.background_image");
175             // default to black or white bg color, depending on the brightness of the progressText
176             int defaultBackground = (0.5f < Color.brightness(this.progressText)) ?
177                 Color.BLACK : Color.WHITE;
178             this.background = config.getColor("ui.background", defaultBackground);
179             this.progressImage = config.getString("ui.progress_image");
180             this.rotatingBackgrounds = stringsToList(
181                 config.getMultiValue("ui.rotating_background"));
182             this.iconImages = stringsToList(config.getMultiValue("ui.icon"));
183             this.errorBackground = config.getString("ui.error_background");
184
185             // On an installation error, where do we point the user.
186             String installError = config.getUrl("ui.install_error", null);
187             this.installError = (installError == null) ?
188                 "m.default_install_error" : MessageUtil.taint(installError);
189
190             // the patch notes bits
191             this.patchNotes = config.getRect("ui.patch_notes", new Rectangle(5, 50, 112, 26));
192             this.patchNotesUrl = config.getUrl("ui.patch_notes_url", null);
193
194             // step progress percentage (defaults and then customized values)
195             EnumMap<Step, List<Integer>> stepPercentages = new EnumMap<>(Step.class);
196             for (Step step : Step.values()) {
197                 stepPercentages.put(step, step.defaultPercents);
198             }
199             for (UpdateInterface.Step step : UpdateInterface.Step.values()) {
200                 String spec = config.getString("ui.percents." + step.name());
201                 if (spec != null) {
202                     try {
203                         stepPercentages.put(step, intsToList(StringUtil.parseIntArray(spec)));
204                     } catch (Exception e) {
205                         log.warning("Failed to parse percentages for " + step + ": " + spec);
206                     }
207                 }
208             }
209             this.stepPercentages = Collections.unmodifiableMap(stepPercentages);
210         }
211     }
212
213     /**
214      * Used by {@link #verifyMetadata} to communicate status in circumstances where it needs to
215      * take network actions.
216      */
217     public static interface StatusDisplay
218     {
219         /** Requests that the specified status message be displayed. */
220         public void updateStatus (String message);
221     }
222
223     /**
224      * Contains metadata for an auxiliary resource group.
225      */
226     public static class AuxGroup {
227         public final String name;
228         public final List<Resource> codes;
229         public final List<Resource> rsrcs;
230
231         public AuxGroup (String name, List<Resource> codes, List<Resource> rsrcs) {
232             this.name = name;
233             this.codes = Collections.unmodifiableList(codes);
234             this.rsrcs = Collections.unmodifiableList(rsrcs);
235         }
236     }
237
238     /** The proxy that should be used to do HTTP downloads. This must be configured prior to using
239       * the application instance. Yes this is a public mutable field, no I'm not going to create a
240       * getter and setter just to pretend like that's not the case. */
241     public Proxy proxy = Proxy.NO_PROXY;
242
243     /**
244      * Creates an application instance which records the location of the <code>getdown.txt</code>
245      * configuration file from the supplied application directory.
246      *
247      */
248     public Application (EnvConfig envc) {
249         _envc = envc;
250         _config = getLocalPath(envc.appDir, CONFIG_FILE);
251     }
252
253     /**
254      * Returns the configured application directory.
255      */
256     public File getAppDir () {
257         return _envc.appDir;
258     }
259
260     /**
261      * Returns whether the application should cache code resources prior to launching the
262      * application.
263      */
264     public boolean useCodeCache ()
265     {
266         return _useCodeCache;
267     }
268
269     /**
270      * Returns the number of days a cached code resource is allowed to stay unused before it
271      * becomes eligible for deletion.
272      */
273     public int getCodeCacheRetentionDays ()
274     {
275         return _codeCacheRetentionDays;
276     }
277
278     /**
279      * Returns the configured maximum concurrent downloads. Used to cap simultaneous downloads of
280      * app files from its hosting server.
281      */
282     public int maxConcurrentDownloads () {
283         return _maxConcDownloads;
284     }
285
286     /**
287      * Returns a resource that refers to the application configuration file itself.
288      */
289     public Resource getConfigResource ()
290     {
291         try {
292             return createResource(CONFIG_FILE, Resource.NORMAL);
293         } catch (Exception e) {
294             throw new RuntimeException("Invalid appbase '" + _vappbase + "'.", e);
295         }
296     }
297
298     /**
299      * Returns a list of the code {@link Resource} objects used by this application.
300      */
301     public List<Resource> getCodeResources ()
302     {
303         return _codes;
304     }
305
306     /**
307      * Returns a list of the non-code {@link Resource} objects used by this application.
308      */
309     public List<Resource> getResources ()
310     {
311         return _resources;
312     }
313
314     /**
315      * Returns the digest of the given {@code resource}.
316      */
317     public String getDigest (Resource resource)
318     {
319         return _digest.getDigest(resource);
320     }
321
322     /**
323      * Returns a list of all the active {@link Resource} objects used by this application (code and
324      * non-code).
325      */
326     public List<Resource> getAllActiveResources ()
327     {
328         List<Resource> allResources = new ArrayList<>();
329         allResources.addAll(getActiveCodeResources());
330         allResources.addAll(getActiveResources());
331         return allResources;
332     }
333
334     /**
335      * Returns the auxiliary resource group with the specified name, or null.
336      */
337     public AuxGroup getAuxGroup (String name)
338     {
339         return _auxgroups.get(name);
340     }
341
342     /**
343      * Returns the set of all auxiliary resource groups defined by the application. An auxiliary
344      * resource group is a collection of resource files that are not downloaded unless a group
345      * token file is present in the application directory.
346      */
347     public Iterable<AuxGroup> getAuxGroups ()
348     {
349         return _auxgroups.values();
350     }
351
352     /**
353      * Returns true if the specified auxgroup has been "activated", false if not. Non-activated
354      * groups should be ignored, activated groups should be downloaded and patched along with the
355      * main resources.
356      */
357     public boolean isAuxGroupActive (String auxgroup)
358     {
359         Boolean active = _auxactive.get(auxgroup);
360         if (active == null) {
361             // TODO: compare the contents with the MD5 hash of the auxgroup name and the client's
362             // machine ident
363             active = getLocalPath(auxgroup + ".dat").exists();
364             _auxactive.put(auxgroup, active);
365         }
366         return active;
367     }
368
369     /**
370      * Returns all main code resources and all code resources from active auxiliary resource groups.
371      */
372     public List<Resource> getActiveCodeResources ()
373     {
374         ArrayList<Resource> codes = new ArrayList<>();
375         codes.addAll(getCodeResources());
376         for (AuxGroup aux : getAuxGroups()) {
377             if (isAuxGroupActive(aux.name)) {
378                 codes.addAll(aux.codes);
379             }
380         }
381         return codes;
382     }
383
384     /**
385      * Returns all resources indicated to contain native library files (.dll, .so, etc.).
386      */
387     public List<Resource> getNativeResources ()
388     {
389         List<Resource> natives = new ArrayList<>();
390         for (Resource resource: _resources) {
391             if (resource.isNative()) {
392                 natives.add(resource);
393             }
394         }
395         return natives;
396     }
397
398     /**
399      * Returns all non-code resources and all resources from active auxiliary resource groups.
400      */
401     public List<Resource> getActiveResources ()
402     {
403         ArrayList<Resource> rsrcs = new ArrayList<>();
404         rsrcs.addAll(getResources());
405         for (AuxGroup aux : getAuxGroups()) {
406             if (isAuxGroupActive(aux.name)) {
407                 rsrcs.addAll(aux.rsrcs);
408             }
409         }
410         return rsrcs;
411     }
412
413     /**
414      * Returns a resource that can be used to download a patch file that will bring this
415      * application from its current version to the target version.
416      *
417      * @param auxgroup the auxiliary resource group for which a patch resource is desired or null
418      * for the main application patch resource.
419      */
420     public Resource getPatchResource (String auxgroup)
421     {
422         if (_targetVersion <= _version) {
423             log.warning("Requested patch resource for up-to-date or non-versioned application",
424                 "cvers", _version, "tvers", _targetVersion);
425             return null;
426         }
427
428         String infix = (auxgroup == null) ? "" : ("-" + auxgroup);
429         String pfile = "patch" + infix + _version + ".dat";
430         try {
431             URL remote = new URL(createVAppBase(_targetVersion), encodePath(pfile));
432             return new Resource(pfile, remote, getLocalPath(pfile), Resource.NORMAL);
433         } catch (Exception e) {
434             log.warning("Failed to create patch resource path",
435                 "pfile", pfile, "appbase", _appbase, "tvers", _targetVersion, "error", e);
436             return null;
437         }
438     }
439
440     /**
441      * Returns a resource for a zip file containing a Java VM that can be downloaded to use in
442      * place of the installed VM (in the case where the VM that launched Getdown does not meet the
443      * application's version requirements) or null if no VM is available for this platform.
444      */
445     public Resource getJavaVMResource ()
446     {
447         if (StringUtil.isBlank(_javaLocation)) {
448             return null;
449         }
450
451         String vmfile = LaunchUtil.LOCAL_JAVA_DIR + ".jar";
452                 log.info("vmfile is '"+vmfile+"'");
453                 System.out.println("vmfile is '"+vmfile+"'");
454         try {
455             URL remote = new URL(createVAppBase(_targetVersion), encodePath(_javaLocation));
456             log.info("Attempting to fetch jvm at "+remote.toString());
457             System.out.println("Attempting to fetch jvm at "+remote.toString());
458             return new Resource(vmfile, remote, getLocalPath(vmfile),
459                                 EnumSet.of(Resource.Attr.UNPACK, Resource.Attr.CLEAN));
460         } catch (Exception e) {
461             log.warning("Failed to create VM resource", "vmfile", vmfile, "appbase", _appbase,
462                 "tvers", _targetVersion, "javaloc", _javaLocation, "error", e);
463             System.out.println("Failed to create VM resource: vmfile="+vmfile+", appbase="+_appbase+
464                 ", tvers="+_targetVersion+", javaloc="+_javaLocation+", error="+e);
465             return null;
466         }
467     }
468
469     /**
470      * Returns a resource that can be used to download an archive containing all files belonging to
471      * the application.
472      */
473     public Resource getFullResource ()
474     {
475         String file = "full";
476         try {
477             URL remote = new URL(createVAppBase(_targetVersion), encodePath(file));
478             return new Resource(file, remote, getLocalPath(file), Resource.NORMAL);
479         } catch (Exception e) {
480             log.warning("Failed to create full resource path",
481                 "file", file, "appbase", _appbase, "tvers", _targetVersion, "error", e);
482             return null;
483         }
484     }
485
486     /**
487      * Returns the URL to use to report an initial download event. Returns null if no tracking
488      * start URL was configured for this application.
489      *
490      * @param event the event to be reported: start, jvm_start, jvm_complete, complete.
491      */
492     public URL getTrackingURL (String event)
493     {
494         try {
495             String suffix = _trackingURLSuffix == null ? "" : _trackingURLSuffix;
496             String ga = getGATrackingCode();
497             return _trackingURL == null ? null :
498                 HostWhitelist.verify(new URL(_trackingURL + encodePath(event + suffix + ga)));
499         } catch (MalformedURLException mue) {
500             log.warning("Invalid tracking URL", "path", _trackingURL, "event", event, "error", mue);
501             return null;
502         }
503     }
504
505     /**
506      * Returns the URL to request to report that we have reached the specified percentage of our
507      * initial download. Returns null if no tracking request was configured for the specified
508      * percentage.
509      */
510     public URL getTrackingProgressURL (int percent)
511     {
512         if (_trackingPcts == null || !_trackingPcts.contains(percent)) {
513             return null;
514         }
515         return getTrackingURL("pct" + percent);
516     }
517
518     /**
519      * Returns the name of our tracking cookie or null if it was not set.
520      */
521     public String getTrackingCookieName ()
522     {
523         return _trackingCookieName;
524     }
525
526     /**
527      * Returns the name of our tracking cookie system property or null if it was not set.
528      */
529     public String getTrackingCookieProperty ()
530     {
531         return _trackingCookieProperty;
532     }
533
534     /**
535      * Instructs the application to parse its {@code getdown.txt} configuration and prepare itself
536      * for operation. The application base URL will be parsed first so that if there are errors
537      * discovered later, the caller can use the application base to download a new {@code
538      * getdown.txt} file and try again.
539      *
540      * @return a {@code Config} instance that contains information from the config file.
541      *
542      * @exception IOException thrown if there is an error reading the file or an error encountered
543      * during its parsing.
544      */
545     public Config init (boolean checkPlatform)
546         throws IOException
547     {
548         Config config = null;
549         File cfgfile = _config;
550         Config.ParseOpts opts = Config.createOpts(checkPlatform);
551         try {
552             // if we have a configuration file, read the data from it
553             if (cfgfile.exists()) {
554                 config = Config.parseConfig(_config, opts);
555             }
556             // otherwise, try reading data from our backup config file; thanks to funny windows
557             // bullshit, we have to do this backup file fiddling in case we got screwed while
558             // updating getdown.txt during normal operation
559             else if ((cfgfile = getLocalPath(CONFIG_FILE + "_old")).exists()) {
560                 config = Config.parseConfig(cfgfile, opts);
561             }
562             // otherwise, issue a warning that we found no getdown file
563             else {
564                 log.info("Found no getdown.txt file", "appdir", getAppDir());
565             }
566         } catch (Exception e) {
567             log.warning("Failure reading config file", "file", config, e);
568         }
569
570         // if we failed to read our config file, check for an appbase specified via a system
571         // property; we can use that to bootstrap ourselves back into operation
572         if (config == null) {
573             String appbase = _envc.appBase;
574             log.info("Using 'appbase' from bootstrap config", "appbase", appbase);
575             Map<String, Object> cdata = new HashMap<>();
576             cdata.put("appbase", appbase);
577             config = new Config(cdata);
578         }
579
580         // first determine our application base, this way if anything goes wrong later in the
581         // process, our caller can use the appbase to download a new configuration file
582         _appbase = config.getString("appbase");
583         if (_appbase == null) {
584             throw new RuntimeException("m.missing_appbase");
585         }
586
587         // check if we're overriding the domain in the appbase
588         _appbase = SysProps.overrideAppbase(_appbase);
589
590         // make sure there's a trailing slash
591         if (!_appbase.endsWith("/")) {
592             _appbase = _appbase + "/";
593         }
594
595         // extract our version information
596         _version = config.getLong("version", -1L);
597
598         // if we are a versioned deployment, create a versioned appbase
599         try {
600             _vappbase = createVAppBase(_version);
601         } catch (MalformedURLException mue) {
602             String err = MessageUtil.tcompose("m.invalid_appbase", _appbase);
603             throw (IOException) new IOException(err).initCause(mue);
604         }
605
606         // check for a latest config URL
607         String latest = config.getString("latest");
608         if (latest != null) {
609             if (latest.startsWith(_appbase)) {
610                 latest = _appbase + latest.substring(_appbase.length());
611             } else {
612                 latest = SysProps.replaceDomain(latest);
613             }
614             try {
615                 _latest = HostWhitelist.verify(new URL(latest));
616             } catch (MalformedURLException mue) {
617                 log.warning("Invalid URL for latest attribute.", mue);
618             }
619         }
620
621         String appPrefix = _envc.appId == null ? "" : (_envc.appId + ".");
622
623         // determine our application class name (use app-specific class _if_ one is provided)
624         _class = config.getString("class");
625         if (appPrefix.length() > 0) {
626             _class = config.getString(appPrefix + "class", _class);
627         }
628         if (_class == null) {
629             throw new IOException("m.missing_class");
630         }
631
632         // determine whether we want strict comments
633         _strictComments = config.getBoolean("strict_comments");
634
635         // check to see if we're using a custom java.version property and regex
636         _javaVersionProp = config.getString("java_version_prop", _javaVersionProp);
637         _javaVersionRegex = config.getString("java_version_regex", _javaVersionRegex);
638
639         // check to see if we require a particular JVM version and have a supplied JVM
640         _javaMinVersion = config.getLong("java_version", _javaMinVersion);
641         // we support java_min_version as an alias of java_version; it better expresses the check
642         // that's going on and better mirrors java_max_version
643         _javaMinVersion = config.getLong("java_min_version", _javaMinVersion);
644         // check to see if we require a particular max JVM version and have a supplied JVM
645         _javaMaxVersion = config.getLong("java_max_version", _javaMaxVersion);
646         // check to see if we require a particular JVM version and have a supplied JVM
647         _javaExactVersionRequired = config.getBoolean("java_exact_version_required");
648
649         // this is a little weird, but when we're run from the digester, we see a String[] which
650         // contains java locations for all platforms which we can't grok, but the digester doesn't
651         // need to know about that; when we're run in a real application there will be only one!
652         Object javaloc = config.getRaw("java_location");
653         if (javaloc instanceof String) {
654             _javaLocation = (String)javaloc;
655         }
656
657         // determine whether we have any tracking configuration
658         _trackingURL = config.getString("tracking_url");
659
660         // check for tracking progress percent configuration
661         String trackPcts = config.getString("tracking_percents");
662         if (!StringUtil.isBlank(trackPcts)) {
663             _trackingPcts = new HashSet<>();
664             for (int pct : StringUtil.parseIntArray(trackPcts)) {
665                 _trackingPcts.add(pct);
666             }
667         } else if (!StringUtil.isBlank(_trackingURL)) {
668             _trackingPcts = new HashSet<>();
669             _trackingPcts.add(50);
670         }
671
672         // Check for tracking cookie configuration
673         _trackingCookieName = config.getString("tracking_cookie_name");
674         _trackingCookieProperty = config.getString("tracking_cookie_property");
675
676         // Some app may need an extra suffix added to the tracking URL
677         _trackingURLSuffix = config.getString("tracking_url_suffix");
678
679         // Some app may need to generate google analytics code
680         _trackingGAHash = config.getString("tracking_ga_hash");
681
682         // clear our arrays as we may be reinitializing
683         _codes.clear();
684         _resources.clear();
685         _auxgroups.clear();
686         _jvmargs.clear();
687         _appargs.clear();
688         _txtJvmArgs.clear();
689
690         // parse our code resources
691         if (config.getMultiValue("code") == null &&
692             config.getMultiValue("ucode") == null) {
693             throw new IOException("m.missing_code");
694         }
695         parseResources(config, "code", Resource.NORMAL, _codes);
696         parseResources(config, "ucode", Resource.UNPACK, _codes);
697
698         // parse our non-code resources
699         parseResources(config, "resource", Resource.NORMAL, _resources);
700         parseResources(config, "uresource", Resource.UNPACK, _resources);
701         parseResources(config, "xresource", Resource.EXEC, _resources);
702         parseResources(config, "presource", Resource.PRELOAD, _resources);
703         parseResources(config, "nresource", Resource.NATIVE, _resources);
704
705         // parse our auxiliary resource groups
706         for (String auxgroup : config.getList("auxgroups")) {
707             ArrayList<Resource> codes = new ArrayList<>();
708             parseResources(config, auxgroup + ".code", Resource.NORMAL, codes);
709             parseResources(config, auxgroup + ".ucode", Resource.UNPACK, codes);
710             ArrayList<Resource> rsrcs = new ArrayList<>();
711             parseResources(config, auxgroup + ".resource", Resource.NORMAL, rsrcs);
712             parseResources(config, auxgroup + ".xresource", Resource.EXEC, rsrcs);
713             parseResources(config, auxgroup + ".uresource", Resource.UNPACK, rsrcs);
714             parseResources(config, auxgroup + ".presource", Resource.PRELOAD, rsrcs);
715             parseResources(config, auxgroup + ".nresource", Resource.NATIVE, rsrcs);
716             _auxgroups.put(auxgroup, new AuxGroup(auxgroup, codes, rsrcs));
717         }
718
719         // transfer our JVM arguments (we include both "global" args and app_id-prefixed args)
720         String[] jvmargs = config.getMultiValue("jvmarg");
721         addAll(jvmargs, _jvmargs);
722         if (appPrefix.length() > 0) {
723             jvmargs = config.getMultiValue(appPrefix + "jvmarg");
724             addAll(jvmargs, _jvmargs);
725         }
726
727         // see if a percentage of physical memory option exists
728         int jvmmempc = config.getInt("jvmmempc", -1);
729         // app_id prefixed setting overrides
730         if (appPrefix.length() > 0) {
731             jvmmempc = config.getInt(appPrefix + "jvmmempc", jvmmempc);
732         }
733         if (0 <= jvmmempc && jvmmempc <= 100) {
734             final Object o = ManagementFactory.getOperatingSystemMXBean();
735
736             try {
737                 if (o instanceof OperatingSystemMXBean) {
738                     final OperatingSystemMXBean osb = (OperatingSystemMXBean) o;
739                     long physicalMem = osb.getTotalPhysicalMemorySize();
740                     long requestedMem = physicalMem*jvmmempc/100;
741                     String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(requestedMem)};
742                     // remove other max heap size arg
743                     ARG: for (int i = 0; i < _jvmargs.size(); i++) {
744                            if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
745                                 _jvmargs.remove(i);
746                            }
747                     }
748                     addAll(maxMemHeapArg, _jvmargs);
749
750                 }
751             }
752             catch (NoClassDefFoundError e) {
753                 // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
754                 System.out.println("No com.sun.management.OperatingSystemMXBean. Cannot use 'jvmmempc'.");
755             }
756         } else if (jvmmempc != -1) {
757           System.out.println("'jvmmempc' value must be in range 0 to 100 (read as '"+Integer.toString(jvmmempc)+"')");
758         }
759
760         // get the set of optimum JVM arguments
761         _optimumJvmArgs = config.getMultiValue("optimum_jvmarg");
762
763         // transfer our application arguments
764         String[] appargs = config.getMultiValue(appPrefix + "apparg");
765         addAll(appargs, _appargs);
766
767         // add the launch specific application arguments
768         _appargs.addAll(_envc.appArgs);
769
770         // look for custom arguments
771         fillAssignmentListFromPairs("extra.txt", _txtJvmArgs);
772
773         // determine whether we want to allow offline operation (defaults to false)
774         _allowOffline = config.getBoolean("allow_offline");
775
776         // look for a debug.txt file which causes us to run in java.exe on Windows so that we can
777         // obtain a thread dump of the running JVM
778         _windebug = getLocalPath("debug.txt").exists();
779
780         // whether to cache code resources and launch from cache
781         _useCodeCache = config.getBoolean("use_code_cache");
782         _codeCacheRetentionDays = config.getInt("code_cache_retention_days", 7);
783
784         // maximum simultaneous downloads
785         _maxConcDownloads = Math.max(1, config.getInt("max_concurrent_downloads",
786                                                       SysProps.threadPoolSize()));
787
788         // extract some info used to configure our child process on macOS
789         _dockName = config.getString("ui.name");
790         _dockIconPath = config.getString("ui.mac_dock_icon", "../desktop.icns");
791
792         return config;
793     }
794
795     /**
796      * Adds strings of the form pair0=pair1 to collector for each pair parsed out of pairLocation.
797      */
798     protected void fillAssignmentListFromPairs (String pairLocation, List<String> collector)
799     {
800         File pairFile = getLocalPath(pairLocation);
801         if (pairFile.exists()) {
802             try {
803                 List<String[]> args = Config.parsePairs(pairFile, Config.createOpts(false));
804                 for (String[] pair : args) {
805                     if (pair[1].length() == 0) {
806                         collector.add(pair[0]);
807                     } else {
808                         collector.add(pair[0] + "=" + pair[1]);
809                     }
810                 }
811             } catch (Throwable t) {
812                 log.warning("Failed to parse '" + pairFile + "': " + t);
813             }
814         }
815     }
816
817     /**
818      * Returns a URL from which the specified path can be fetched. Our application base URL is
819      * properly versioned and combined with the supplied path.
820      */
821     public URL getRemoteURL (String path)
822         throws MalformedURLException
823     {
824         return new URL(_vappbase, encodePath(path));
825     }
826
827     /**
828      * Returns the local path to the specified resource.
829      */
830     public File getLocalPath (String path)
831     {
832         return getLocalPath(getAppDir(), path);
833     }
834
835     /**
836      * Returns true if we either have no version requirement, are running in a JVM that meets our
837      * version requirements or have what appears to be a version of the JVM that meets our
838      * requirements.
839      */
840     public boolean haveValidJavaVersion ()
841     {
842         // if we're doing no version checking, then yay!
843         if (_javaMinVersion == 0 && _javaMaxVersion == 0) return true;
844
845         try {
846             // parse the version out of the java.version (or custom) system property
847             long version = SysProps.parseJavaVersion(_javaVersionProp, _javaVersionRegex);
848
849             log.info("Checking Java version", "current", version,
850                      "wantMin", _javaMinVersion, "wantMax", _javaMaxVersion);
851
852             // if we have an unpacked VM, check the 'release' file for its version
853             Resource vmjar = getJavaVMResource();
854             if (vmjar != null && vmjar.isMarkedValid()) {
855                 File vmdir = new File(getAppDir(), LaunchUtil.LOCAL_JAVA_DIR);
856                 File relfile = new File(vmdir, "release");
857                 if (!relfile.exists()) {
858                     log.warning("Unpacked JVM missing 'release' file. Assuming valid version.");
859                     return true;
860                 }
861
862                 long vmvers = VersionUtil.readReleaseVersion(relfile, _javaVersionRegex);
863                 if (vmvers == 0L) {
864                     log.warning("Unable to read version from 'release' file. Assuming valid.");
865                     return true;
866                 }
867
868                 version = vmvers;
869                 log.info("Checking version of unpacked JVM [vers=" + version + "].");
870             }
871
872             if (_javaExactVersionRequired) {
873                 if (version == _javaMinVersion) return true;
874                 else {
875                     log.warning("An exact Java VM version is required.", "current", version,
876                                 "required", _javaMinVersion);
877                     return false;
878                 }
879             }
880
881             boolean minVersionOK = (_javaMinVersion == 0) || (version >= _javaMinVersion);
882             boolean maxVersionOK = (_javaMaxVersion == 0) || (version <= _javaMaxVersion);
883             return minVersionOK && maxVersionOK;
884
885         } catch (RuntimeException re) {
886             // if we can't parse the java version we're in weird land and should probably just try
887             // our luck with what we've got rather than try to download a new jvm
888             log.warning("Unable to parse VM version, hoping for the best",
889                         "error", re, "needed", _javaMinVersion);
890             return true;
891         }
892     }
893
894     /**
895      * Checks whether the app has a set of "optimum" JVM args that we wish to try first, detecting
896      * whether the launch is successful and, if necessary, trying again without the optimum
897      * arguments.
898      */
899     public boolean hasOptimumJvmArgs ()
900     {
901         return _optimumJvmArgs != null;
902     }
903
904     /**
905      * Returns true if the app should attempt to run even if we have no Internet connection.
906      */
907     public boolean allowOffline ()
908     {
909         return _allowOffline;
910     }
911
912     /**
913      * Attempts to redownload the <code>getdown.txt</code> file based on information parsed from a
914      * previous call to {@link #init}.
915      */
916     public void attemptRecovery (StatusDisplay status)
917         throws IOException
918     {
919         status.updateStatus("m.updating_metadata");
920         downloadConfigFile();
921     }
922
923     /**
924      * Downloads and replaces the <code>getdown.txt</code> and <code>digest.txt</code> files with
925      * those for the target version of our application.
926      */
927     public void updateMetadata ()
928         throws IOException
929     {
930         try {
931             // update our versioned application base with the target version
932             _vappbase = createVAppBase(_targetVersion);
933         } catch (MalformedURLException mue) {
934             String err = MessageUtil.tcompose("m.invalid_appbase", _appbase);
935             throw (IOException) new IOException(err).initCause(mue);
936         }
937
938         try {
939             // now re-download our control files; we download the digest first so that if it fails,
940             // our config file will still reference the old version and re-running the updater will
941             // start the whole process over again
942             downloadDigestFiles();
943             downloadConfigFile();
944
945         } catch (IOException ex) {
946             // if we are allowing offline execution, we want to allow the application to run in its
947             // current form rather than aborting the entire process; to do this, we delete the
948             // version.txt file and "trick" Getdown into thinking that it just needs to validate
949             // the application as is; next time the app runs when connected to the internet, it
950             // will have to rediscover that it needs updating and reattempt to update itself
951             if (_allowOffline) {
952                 log.warning("Failed to update digest files.  Attempting offline operaton.", ex);
953                 if (!FileUtil.deleteHarder(getLocalPath(VERSION_FILE))) {
954                     log.warning("Deleting version.txt failed.  This probably isn't going to work.");
955                 }
956             } else {
957                 throw ex;
958             }
959         }
960     }
961
962     /**
963      * Invokes the process associated with this application definition.
964      *
965      * @param optimum whether or not to include the set of optimum arguments (as opposed to falling
966      * back).
967      */
968     public Process createProcess (boolean optimum)
969         throws IOException
970     {
971         ArrayList<String> args = new ArrayList<>();
972
973         // reconstruct the path to the JVM
974         args.add(LaunchUtil.getJVMPath(getAppDir(), _windebug || optimum));
975
976         // check whether we're using -jar mode or -classpath mode
977         boolean dashJarMode = MANIFEST_CLASS.equals(_class);
978
979         // add the -classpath arguments if we're not in -jar mode
980         ClassPath classPath = PathBuilder.buildClassPath(this);
981         if (!dashJarMode) {
982             args.add("-classpath");
983             args.add(classPath.asArgumentString());
984         }
985
986         // we love our Mac users, so we do nice things to preserve our application identity
987         if (LaunchUtil.isMacOS()) {
988             args.add("-Xdock:icon=" + getLocalPath(_dockIconPath).getAbsolutePath());
989             args.add("-Xdock:name=" + _dockName);
990         }
991
992         // pass along our proxy settings
993         String proxyHost;
994         if ((proxyHost = System.getProperty("http.proxyHost")) != null) {
995             args.add("-Dhttp.proxyHost=" + proxyHost);
996             args.add("-Dhttp.proxyPort=" + System.getProperty("http.proxyPort"));
997             args.add("-Dhttps.proxyHost=" + proxyHost);
998             args.add("-Dhttps.proxyPort=" + System.getProperty("http.proxyPort"));
999         }
1000
1001         // add the marker indicating the app is running in getdown
1002         args.add("-D" + Properties.GETDOWN + "=true");
1003
1004         // set the native library path if we have native resources
1005         // @TODO optional getdown.txt parameter to set addCurrentLibraryPath to true or false?
1006         ClassPath javaLibPath = PathBuilder.buildLibsPath(this, true);
1007         if (javaLibPath != null) {
1008             args.add("-Djava.library.path=" + javaLibPath.asArgumentString());
1009         }
1010
1011         // pass along any pass-through arguments
1012         for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
1013             String key = (String)entry.getKey();
1014             if (key.startsWith(PROP_PASSTHROUGH_PREFIX)) {
1015                 key = key.substring(PROP_PASSTHROUGH_PREFIX.length());
1016                 args.add("-D" + key + "=" + entry.getValue());
1017             }
1018         }
1019
1020         // add the JVM arguments
1021         for (String string : _jvmargs) {
1022             args.add(processArg(string));
1023         }
1024
1025         // add the optimum arguments if requested and available
1026         if (optimum && _optimumJvmArgs != null) {
1027             for (String string : _optimumJvmArgs) {
1028                 args.add(processArg(string));
1029             }
1030         }
1031
1032         // add the arguments from extra.txt (after the optimum ones, in case they override them)
1033         for (String string : _txtJvmArgs) {
1034             args.add(processArg(string));
1035         }
1036
1037         // if we're in -jar mode add those arguments, otherwise add the app class name
1038         if (dashJarMode) {
1039             args.add("-jar");
1040             args.add(classPath.asArgumentString());
1041         } else {
1042             args.add(_class);
1043         }
1044
1045         // finally add the application arguments
1046         for (String string : _appargs) {
1047             args.add(processArg(string));
1048         }
1049
1050         String[] envp = createEnvironment();
1051         String[] sargs = args.toArray(new String[args.size()]);
1052         log.info("Running " + StringUtil.join(sargs, "\n  "));
1053
1054         return Runtime.getRuntime().exec(sargs, envp, getAppDir());
1055     }
1056
1057     /**
1058      * If the application provided environment variables, combine those with the current
1059      * environment and return that in a style usable for {@link Runtime#exec(String, String[])}.
1060      * If the application didn't provide any environment variables, null is returned to just use
1061      * the existing environment.
1062      */
1063     protected String[] createEnvironment ()
1064     {
1065         List<String> envvar = new ArrayList<>();
1066         fillAssignmentListFromPairs("env.txt", envvar);
1067         if (envvar.isEmpty()) {
1068             log.info("Didn't find any custom environment variables, not setting any.");
1069             return null;
1070         }
1071
1072         List<String> envAssignments = new ArrayList<>();
1073         for (String assignment : envvar) {
1074             envAssignments.add(processArg(assignment));
1075         }
1076         for (Map.Entry<String, String> environmentEntry : System.getenv().entrySet()) {
1077             envAssignments.add(environmentEntry.getKey() + "=" + environmentEntry.getValue());
1078         }
1079         String[] envp = envAssignments.toArray(new String[envAssignments.size()]);
1080         log.info("Environment " + StringUtil.join(envp, "\n "));
1081         return envp;
1082     }
1083
1084     /**
1085      * Runs this application directly in the current VM.
1086      */
1087     public void invokeDirect () throws IOException
1088     {
1089         ClassPath classPath = PathBuilder.buildClassPath(this);
1090         URL[] jarUrls = classPath.asUrls();
1091
1092         // create custom class loader
1093         URLClassLoader loader = new URLClassLoader(jarUrls, ClassLoader.getSystemClassLoader()) {
1094             @Override protected PermissionCollection getPermissions (CodeSource code) {
1095                 Permissions perms = new Permissions();
1096                 perms.add(new AllPermission());
1097                 return perms;
1098             }
1099         };
1100         Thread.currentThread().setContextClassLoader(loader);
1101
1102         log.info("Configured URL class loader:");
1103         for (URL url : jarUrls) log.info("  " + url);
1104
1105         // configure any system properties that we can
1106         for (String jvmarg : _jvmargs) {
1107             if (jvmarg.startsWith("-D")) {
1108                 jvmarg = processArg(jvmarg.substring(2));
1109                 int eqidx = jvmarg.indexOf("=");
1110                 if (eqidx == -1) {
1111                     log.warning("Bogus system property: '" + jvmarg + "'?");
1112                 } else {
1113                     System.setProperty(jvmarg.substring(0, eqidx), jvmarg.substring(eqidx+1));
1114                 }
1115             }
1116         }
1117
1118         // pass along any pass-through arguments
1119         Map<String, String> passProps = new HashMap<>();
1120         for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
1121             String key = (String)entry.getKey();
1122             if (key.startsWith(PROP_PASSTHROUGH_PREFIX)) {
1123                 key = key.substring(PROP_PASSTHROUGH_PREFIX.length());
1124                 passProps.put(key, (String)entry.getValue());
1125             }
1126         }
1127         // we can't set these in the above loop lest we get a ConcurrentModificationException
1128         for (Map.Entry<String, String> entry : passProps.entrySet()) {
1129             System.setProperty(entry.getKey(), entry.getValue());
1130         }
1131
1132         // prepare our app arguments
1133         String[] args = new String[_appargs.size()];
1134         for (int ii = 0; ii < args.length; ii++) args[ii] = processArg(_appargs.get(ii));
1135
1136         try {
1137             log.info("Loading " + _class);
1138             Class<?> appclass = loader.loadClass(_class);
1139             Method main = appclass.getMethod("main", EMPTY_STRING_ARRAY.getClass());
1140             log.info("Invoking main({" + StringUtil.join(args, ", ") + "})");
1141             main.invoke(null, new Object[] { args });
1142         } catch (Exception e) {
1143             log.warning("Failure invoking app main", e);
1144         }
1145     }
1146
1147     /** Replaces the application directory and version in any argument. */
1148     protected String processArg (String arg)
1149     {
1150         arg = arg.replace("%APPDIR%", getAppDir().getAbsolutePath());
1151         arg = arg.replace("%VERSION%", String.valueOf(_version));
1152
1153         // if this argument contains %ENV.FOO% replace those with the associated values looked up
1154         // from the environment
1155         if (arg.contains(ENV_VAR_PREFIX)) {
1156             StringBuffer sb = new StringBuffer();
1157             Matcher matcher = ENV_VAR_PATTERN.matcher(arg);
1158             while (matcher.find()) {
1159                 String varName = matcher.group(1), varValue = System.getenv(varName);
1160                 String repValue = varValue == null ? "MISSING:"+varName : varValue;
1161                 matcher.appendReplacement(sb, Matcher.quoteReplacement(repValue));
1162             }
1163             matcher.appendTail(sb);
1164             arg = sb.toString();
1165         }
1166
1167         return arg;
1168     }
1169
1170     /**
1171      * Loads the <code>digest.txt</code> file and verifies the contents of both that file and the
1172      * <code>getdown.text</code> file. Then it loads the <code>version.txt</code> and decides
1173      * whether or not the application needs to be updated or whether we can proceed to verification
1174      * and execution.
1175      *
1176      * @return true if the application needs to be updated, false if it is up to date and can be
1177      * verified and executed.
1178      *
1179      * @exception IOException thrown if we encounter an unrecoverable error while verifying the
1180      * metadata.
1181      */
1182     public boolean verifyMetadata (StatusDisplay status)
1183         throws IOException
1184     {
1185         log.info("Verifying application: " + _vappbase);
1186         log.info("Version: " + _version);
1187         log.info("Class: " + _class);
1188
1189         // this will read in the contents of the digest file and validate itself
1190         try {
1191             _digest = new Digest(getAppDir(), _strictComments);
1192         } catch (IOException ioe) {
1193             log.info("Failed to load digest: " + ioe.getMessage() + ". Attempting recovery...");
1194         }
1195
1196         // if we have no version, then we are running in unversioned mode so we need to download
1197         // our digest.txt file on every invocation
1198         if (_version == -1) {
1199             // make a note of the old meta-digest, if this changes we need to revalidate all of our
1200             // resources as one or more of them have also changed
1201             String olddig = (_digest == null) ? "" : _digest.getMetaDigest();
1202             try {
1203                 status.updateStatus("m.checking");
1204                 downloadDigestFiles();
1205                 _digest = new Digest(getAppDir(), _strictComments);
1206                 if (!olddig.equals(_digest.getMetaDigest())) {
1207                     log.info("Unversioned digest changed. Revalidating...");
1208                     status.updateStatus("m.validating");
1209                     clearValidationMarkers();
1210                 }
1211             } catch (IOException ioe) {
1212                 log.warning("Failed to refresh non-versioned digest: " +
1213                             ioe.getMessage() + ". Proceeding...");
1214             }
1215         }
1216
1217         // regardless of whether we're versioned, if we failed to read the digest from disk, try to
1218         // redownload the digest file and give it another good college try; this time we allow
1219         // exceptions to propagate up to the caller as there is nothing else we can do
1220         if (_digest == null) {
1221             status.updateStatus("m.updating_metadata");
1222             downloadDigestFiles();
1223             _digest = new Digest(getAppDir(), _strictComments);
1224         }
1225
1226         // now verify the contents of our main config file
1227         Resource crsrc = getConfigResource();
1228         if (!_digest.validateResource(crsrc, null)) {
1229             status.updateStatus("m.updating_metadata");
1230             // attempt to redownload both of our metadata files; again we pass errors up to our
1231             // caller because there's nothing we can do to automatically recover
1232             downloadConfigFile();
1233             downloadDigestFiles();
1234             _digest = new Digest(getAppDir(), _strictComments);
1235             // revalidate everything if we end up downloading new metadata
1236             clearValidationMarkers();
1237             // if the new copy validates, reinitialize ourselves; otherwise report baffling hoseage
1238             if (_digest.validateResource(crsrc, null)) {
1239                 init(true);
1240             } else {
1241                 log.warning(CONFIG_FILE + " failed to validate even after redownloading. " +
1242                             "Blindly forging onward.");
1243             }
1244         }
1245
1246         // start by assuming we are happy with our version
1247         _targetVersion = _version;
1248
1249         // if we are a versioned application, read in the contents of the version.txt file
1250         // and/or check the latest config URL for a newer version
1251         if (_version != -1) {
1252             File vfile = getLocalPath(VERSION_FILE);
1253             long fileVersion = VersionUtil.readVersion(vfile);
1254             if (fileVersion != -1) {
1255                 _targetVersion = fileVersion;
1256             }
1257
1258             if (_latest != null) {
1259                 try (InputStream in = ConnectionUtil.open(proxy, _latest, 0, 0).getInputStream();
1260                      InputStreamReader reader = new InputStreamReader(in, UTF_8);
1261                      BufferedReader bin = new BufferedReader(reader)) {
1262                     for (String[] pair : Config.parsePairs(bin, Config.createOpts(false))) {
1263                         if (pair[0].equals("version")) {
1264                             _targetVersion = Math.max(Long.parseLong(pair[1]), _targetVersion);
1265                             if (fileVersion != -1 && _targetVersion > fileVersion) {
1266                                 // replace the file with the newest version
1267                                 try (FileOutputStream fos = new FileOutputStream(vfile);
1268                                      PrintStream out = new PrintStream(fos)) {
1269                                     out.println(_targetVersion);
1270                                 }
1271                             }
1272                             break;
1273                         }
1274                     }
1275                 } catch (Exception e) {
1276                     log.warning("Unable to retrieve version from latest config file.", e);
1277                 }
1278             }
1279         }
1280
1281         // finally let the caller know if we need an update
1282         return _version != _targetVersion;
1283     }
1284
1285     /**
1286      * Verifies the code and media resources associated with this application. A list of resources
1287      * that do not exist or fail the verification process will be returned. If all resources are
1288      * ready to go, null will be returned and the application is considered ready to run.
1289      *
1290      * @param obs a progress observer that will be notified of verification progress. NOTE: this
1291      * observer may be called from arbitrary threads, so if you update a UI based on calls to it,
1292      * you have to take care to get back to your UI thread.
1293      * @param alreadyValid if non-null a 1 element array that will have the number of "already
1294      * validated" resources filled in.
1295      * @param unpacked a set to populate with unpacked resources.
1296      * @param toInstall a list into which to add resources that need to be installed.
1297      * @param toDownload a list into which to add resources that need to be downloaded.
1298      */
1299     public void verifyResources (
1300         ProgressObserver obs, int[] alreadyValid, Set<Resource> unpacked,
1301         Set<Resource> toInstall, Set<Resource> toDownload)
1302         throws InterruptedException
1303     {
1304         // resources are verified on background threads supplied by the thread pool, and progress
1305         // is reported by posting runnable actions to the actions queue which is processed by the
1306         // main (UI) thread
1307         ExecutorService exec = Executors.newFixedThreadPool(SysProps.threadPoolSize());
1308         final BlockingQueue<Runnable> actions = new LinkedBlockingQueue<Runnable>();
1309         final int[] completed = new int[1];
1310
1311         long start = System.currentTimeMillis();
1312
1313         // obtain the sizes of the resources to validate
1314         List<Resource> rsrcs = getAllActiveResources();
1315         long[] sizes = new long[rsrcs.size()];
1316         long totalSize = 0;
1317         for (int ii = 0; ii < sizes.length; ii++) {
1318             totalSize += sizes[ii] = rsrcs.get(ii).getLocal().length();
1319         }
1320         final ProgressObserver fobs = obs;
1321         // as long as we forward aggregated progress updates to the UI thread, having multiple
1322         // threads update a progress aggregator is "mostly" thread-safe
1323         final ProgressAggregator pagg = new ProgressAggregator(new ProgressObserver() {
1324             public void progress (final int percent) {
1325                 actions.add(new Runnable() {
1326                     public void run () {
1327                         fobs.progress(percent);
1328                     }
1329                 });
1330             }
1331         }, sizes);
1332
1333         final int[] fAlreadyValid = alreadyValid;
1334         final Set<Resource> toInstallAsync = new ConcurrentSkipListSet<>(toInstall);
1335         final Set<Resource> toDownloadAsync = new ConcurrentSkipListSet<>();
1336         final Set<Resource> unpackedAsync = new ConcurrentSkipListSet<>();
1337
1338         for (int ii = 0; ii < sizes.length; ii++) {
1339             final Resource rsrc = rsrcs.get(ii);
1340             final int index = ii;
1341             exec.execute(new Runnable() {
1342                 public void run () {
1343                     verifyResource(rsrc, pagg.startElement(index), fAlreadyValid,
1344                                    unpackedAsync, toInstallAsync, toDownloadAsync);
1345                     actions.add(new Runnable() {
1346                         public void run () {
1347                             completed[0] += 1;
1348                         }
1349                     });
1350                 }
1351             });
1352         }
1353
1354         while (completed[0] < rsrcs.size()) {
1355             // we should be getting progress completion updates WAY more often than one every
1356             // minute, so if things freeze up for 60 seconds, abandon ship
1357             Runnable action = actions.poll(60, TimeUnit.SECONDS);
1358             action.run();
1359         }
1360
1361         exec.shutdown();
1362
1363         toInstall.addAll(toInstallAsync);
1364         toDownload.addAll(toDownloadAsync);
1365         unpacked.addAll(unpackedAsync);
1366
1367         long complete = System.currentTimeMillis();
1368         log.info("Verified resources", "count", rsrcs.size(), "size", (totalSize/1024) + "k",
1369                  "duration", (complete-start) + "ms");
1370     }
1371
1372     private void verifyResource (Resource rsrc, ProgressObserver obs, int[] alreadyValid,
1373                                  Set<Resource> unpacked,
1374                                  Set<Resource> toInstall, Set<Resource> toDownload) {
1375         if (rsrc.isMarkedValid()) {
1376             if (alreadyValid != null) {
1377                 alreadyValid[0]++;
1378             }
1379             obs.progress(100);
1380             return;
1381         }
1382
1383         try {
1384             if (_digest.validateResource(rsrc, obs)) {
1385                 // if the resource has a _new file, add it to to-install list
1386                 if (rsrc.getLocalNew().exists()) {
1387                     toInstall.add(rsrc);
1388                     return;
1389                 }
1390                 rsrc.applyAttrs();
1391                 unpacked.add(rsrc);
1392                 rsrc.markAsValid();
1393                 return;
1394             }
1395
1396         } catch (Exception e) {
1397             log.info("Failure verifying resource. Requesting redownload...",
1398                      "rsrc", rsrc, "error", e);
1399
1400         } finally {
1401             obs.progress(100);
1402         }
1403         toDownload.add(rsrc);
1404     }
1405
1406     /**
1407      * Unpacks the resources that require it (we know that they're valid).
1408      *
1409      * @param unpacked a set of resources to skip because they're already unpacked.
1410      */
1411     public void unpackResources (ProgressObserver obs, Set<Resource> unpacked)
1412         throws InterruptedException
1413     {
1414         List<Resource> rsrcs = getActiveResources();
1415
1416         // remove resources that we don't want to unpack
1417         for (Iterator<Resource> it = rsrcs.iterator(); it.hasNext(); ) {
1418             Resource rsrc = it.next();
1419             if (!rsrc.shouldUnpack() || unpacked.contains(rsrc)) {
1420                 it.remove();
1421             }
1422         }
1423
1424         // obtain the sizes of the resources to unpack
1425         long[] sizes = new long[rsrcs.size()];
1426         for (int ii = 0; ii < sizes.length; ii++) {
1427             sizes[ii] = rsrcs.get(ii).getLocal().length();
1428         }
1429
1430         ProgressAggregator pagg = new ProgressAggregator(obs, sizes);
1431         for (int ii = 0; ii < sizes.length; ii++) {
1432             Resource rsrc = rsrcs.get(ii);
1433             ProgressObserver pobs = pagg.startElement(ii);
1434             try {
1435                 rsrc.unpack();
1436             } catch (IOException ioe) {
1437                 log.warning("Failure unpacking resource", "rsrc", rsrc, ioe);
1438             }
1439             pobs.progress(100);
1440         }
1441     }
1442
1443     /**
1444      * Clears all validation marker files.
1445      */
1446     public void clearValidationMarkers ()
1447     {
1448         clearValidationMarkers(getAllActiveResources().iterator());
1449     }
1450
1451     /**
1452      * Returns the version number for the application.  Should only be called after successful
1453      * return of verifyMetadata.
1454      */
1455     public long getVersion ()
1456     {
1457         return _version;
1458     }
1459
1460     /**
1461      * Creates a versioned application base URL for the specified version.
1462      */
1463     protected URL createVAppBase (long version)
1464         throws MalformedURLException
1465     {
1466         String url = version < 0 ? _appbase : _appbase.replace("%VERSION%", "" + version);
1467         return HostWhitelist.verify(new URL(url));
1468     }
1469
1470     /**
1471      * Clears all validation marker files for the resources in the supplied iterator.
1472      */
1473     protected void clearValidationMarkers (Iterator<Resource> iter)
1474     {
1475         while (iter.hasNext()) {
1476             iter.next().clearMarker();
1477         }
1478     }
1479
1480     /**
1481      * Downloads a new copy of CONFIG_FILE.
1482      */
1483     protected void downloadConfigFile ()
1484         throws IOException
1485     {
1486         downloadControlFile(CONFIG_FILE, 0);
1487     }
1488
1489     /**
1490      * @return true if gettingdown.lock was unlocked, already locked by this application or if
1491      * we're not locking at all.
1492      */
1493     public synchronized boolean lockForUpdates ()
1494     {
1495         if (_lock != null && _lock.isValid()) {
1496             return true;
1497         }
1498         try {
1499             _lockChannel = new RandomAccessFile(getLocalPath("gettingdown.lock"), "rw").getChannel();
1500         } catch (FileNotFoundException e) {
1501             log.warning("Unable to create lock file", "message", e.getMessage(), e);
1502             return false;
1503         }
1504         try {
1505             _lock = _lockChannel.tryLock();
1506         } catch (IOException e) {
1507             log.warning("Unable to create lock", "message", e.getMessage(), e);
1508             return false;
1509         } catch (OverlappingFileLockException e) {
1510             log.warning("The lock is held elsewhere in this JVM", e);
1511             return false;
1512         }
1513         log.info("Able to lock for updates: " + (_lock != null));
1514         return _lock != null;
1515     }
1516
1517     /**
1518      * Release gettingdown.lock
1519      */
1520     public synchronized void releaseLock ()
1521     {
1522         if (_lock != null) {
1523             log.info("Releasing lock");
1524             try {
1525                 _lock.release();
1526             } catch (IOException e) {
1527                 log.warning("Unable to release lock", "message", e.getMessage(), e);
1528             }
1529             try {
1530                 _lockChannel.close();
1531             } catch (IOException e) {
1532                 log.warning("Unable to close lock channel", "message", e.getMessage(), e);
1533             }
1534             _lockChannel = null;
1535             _lock = null;
1536         }
1537     }
1538
1539     /**
1540      * Downloads the digest files and validates their signature.
1541      * @throws IOException
1542      */
1543     protected void downloadDigestFiles ()
1544         throws IOException
1545     {
1546         for (int version = 1; version <= Digest.VERSION; version++) {
1547             downloadControlFile(Digest.digestFile(version), version);
1548         }
1549     }
1550
1551     /**
1552      * Downloads a new copy of the specified control file, optionally validating its signature.
1553      * If the download is successful, moves it over the old file on the filesystem.
1554      *
1555      * <p> TODO: Switch to PKCS #7 or CMS.
1556      *
1557      * @param sigVersion if {@code 0} no validation will be performed, if {@code > 0} then this
1558      * should indicate the version of the digest file being validated which indicates which
1559      * algorithm to use to verify the signature. See {@link Digest#VERSION}.
1560      */
1561     protected void downloadControlFile (String path, int sigVersion)
1562         throws IOException
1563     {
1564         File target = downloadFile(path);
1565
1566         if (sigVersion > 0) {
1567             if (_envc.certs.isEmpty()) {
1568                 log.info("No signing certs, not verifying digest.txt", "path", path);
1569
1570             } else {
1571                 File signatureFile = downloadFile(path + SIGNATURE_SUFFIX);
1572                 byte[] signature = null;
1573                 try (FileInputStream signatureStream = new FileInputStream(signatureFile)) {
1574                     signature = StreamUtil.toByteArray(signatureStream);
1575                 } finally {
1576                     FileUtil.deleteHarder(signatureFile); // delete the file regardless
1577                 }
1578
1579                 byte[] buffer = new byte[8192];
1580                 int length, validated = 0;
1581                 for (Certificate cert : _envc.certs) {
1582                     try (FileInputStream dataInput = new FileInputStream(target)) {
1583                         Signature sig = Signature.getInstance(Digest.sigAlgorithm(sigVersion));
1584                         sig.initVerify(cert);
1585                         while ((length = dataInput.read(buffer)) != -1) {
1586                             sig.update(buffer, 0, length);
1587                         }
1588
1589                         if (!sig.verify(Base64.decode(signature, Base64.DEFAULT))) {
1590                             log.info("Signature does not match", "cert", cert.getPublicKey());
1591                             continue;
1592                         } else {
1593                             log.info("Signature matches", "cert", cert.getPublicKey());
1594                             validated++;
1595                         }
1596
1597                     } catch (IOException ioe) {
1598                         log.warning("Failure validating signature of " + target + ": " + ioe);
1599
1600                     } catch (GeneralSecurityException gse) {
1601                         // no problem!
1602
1603                     }
1604                 }
1605
1606                 // if we couldn't find a key that validates our digest, we are the hosed!
1607                 if (validated == 0) {
1608                     // delete the temporary digest file as we know it is invalid
1609                     FileUtil.deleteHarder(target);
1610                     throw new IOException("m.corrupt_digest_signature_error");
1611                 }
1612             }
1613         }
1614
1615         // now move the temporary file over the original
1616         File original = getLocalPath(path);
1617         if (!FileUtil.renameTo(target, original)) {
1618             throw new IOException("Failed to rename(" + target + ", " + original + ")");
1619         }
1620     }
1621
1622     /**
1623      * Download a path to a temporary file, returning a {@link File} instance with the path
1624      * contents.
1625      */
1626     protected File downloadFile (String path)
1627         throws IOException
1628     {
1629         File target = getLocalPath(path + "_new");
1630
1631         URL targetURL = null;
1632         try {
1633             targetURL = getRemoteURL(path);
1634         } catch (Exception e) {
1635             log.warning("Requested to download invalid control file",
1636                 "appbase", _vappbase, "path", path, "error", e);
1637             throw (IOException) new IOException("Invalid path '" + path + "'.").initCause(e);
1638         }
1639
1640         log.info("Attempting to refetch '" + path + "' from '" + targetURL + "'.");
1641
1642         // stream the URL into our temporary file
1643         URLConnection uconn = ConnectionUtil.open(proxy, targetURL, 0, 0);
1644         // we have to tell Java not to use caches here, otherwise it will cache any request for
1645         // same URL for the lifetime of this JVM (based on the URL string, not the URL object);
1646         // if the getdown.txt file, for example, changes in the meanwhile, we would never hear
1647         // about it; turning off caches is not a performance concern, because when Getdown asks
1648         // to download a file, it expects it to come over the wire, not from a cache
1649         uconn.setUseCaches(false);
1650         uconn.setRequestProperty("Accept-Encoding", "gzip");
1651         try (InputStream fin = uconn.getInputStream()) {
1652             String encoding = uconn.getContentEncoding();
1653             boolean gzip = "gzip".equalsIgnoreCase(encoding);
1654             try (InputStream fin2 = (gzip ? new GZIPInputStream(fin) : fin)) {
1655                 try (FileOutputStream fout = new FileOutputStream(target)) {
1656                     StreamUtil.copy(fin2, fout);
1657                 }
1658             }
1659         }
1660
1661         return target;
1662     }
1663
1664     /** Helper function for creating {@link Resource} instances. */
1665     protected Resource createResource (String path, EnumSet<Resource.Attr> attrs)
1666         throws MalformedURLException
1667     {
1668         return new Resource(path, getRemoteURL(path), getLocalPath(path), attrs);
1669     }
1670
1671     /** Helper function to add all values in {@code values} (if non-null) to {@code target}. */
1672     protected static void addAll (String[] values, List<String> target) {
1673         if (values != null) {
1674             for (String value : values) {
1675                 target.add(value);
1676             }
1677         }
1678     }
1679
1680     /**
1681      * Make an immutable List from the specified int array.
1682      */
1683     public static List<Integer> intsToList (int[] values)
1684     {
1685         List<Integer> list = new ArrayList<>(values.length);
1686         for (int val : values) {
1687             list.add(val);
1688         }
1689         return Collections.unmodifiableList(list);
1690     }
1691
1692     /**
1693      * Make an immutable List from the specified String array.
1694      */
1695     public static List<String> stringsToList (String[] values)
1696     {
1697         return values == null ? null : Collections.unmodifiableList(Arrays.asList(values));
1698     }
1699
1700     /** Used to parse resources with the specified name. */
1701     protected void parseResources (Config config, String name, EnumSet<Resource.Attr> attrs,
1702                                    List<Resource> list)
1703     {
1704         String[] rsrcs = config.getMultiValue(name);
1705         if (rsrcs == null) {
1706             return;
1707         }
1708         for (String rsrc : rsrcs) {
1709             try {
1710                 list.add(createResource(rsrc, attrs));
1711             } catch (Exception e) {
1712                 log.warning("Invalid resource '" + rsrc + "'. " + e);
1713             }
1714         }
1715     }
1716
1717     /** Possibly generates and returns a google analytics tracking cookie. */
1718     protected String getGATrackingCode ()
1719     {
1720         if (_trackingGAHash == null) {
1721             return "";
1722         }
1723         long time = System.currentTimeMillis() / 1000;
1724         if (_trackingStart == 0) {
1725             _trackingStart = time;
1726         }
1727         if (_trackingId == 0) {
1728             int low = 100000000, high = 1000000000;
1729             _trackingId = low + _rando.nextInt(high-low);
1730         }
1731         StringBuilder cookie = new StringBuilder("&utmcc=__utma%3D").append(_trackingGAHash);
1732         cookie.append(".").append(_trackingId);
1733         cookie.append(".").append(_trackingStart).append(".").append(_trackingStart);
1734         cookie.append(".").append(time).append(".1%3B%2B");
1735         cookie.append("__utmz%3D").append(_trackingGAHash).append(".");
1736         cookie.append(_trackingStart).append(".1.1.");
1737         cookie.append("utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B");
1738         int low = 1000000000, high = 2000000000;
1739         cookie.append("&utmn=").append(_rando.nextInt(high-low));
1740         return cookie.toString();
1741     }
1742
1743     /**
1744      * Encodes a path for use in a URL.
1745      */
1746     protected static String encodePath (String path)
1747     {
1748         try {
1749             // we want to keep slashes because we're encoding an entire path; also we need to turn
1750             // + into %20 because web servers don't like + in paths or file names, blah
1751             return URLEncoder.encode(path, "UTF-8").replace("%2F", "/").replace("+", "%20");
1752         } catch (UnsupportedEncodingException ue) {
1753             log.warning("Failed to URL encode " + path + ": " + ue);
1754             return path;
1755         }
1756     }
1757
1758     protected File getLocalPath (File appdir, String path)
1759     {
1760         return new File(appdir, path);
1761     }
1762
1763     protected final EnvConfig _envc;
1764     protected File _config;
1765     protected Digest _digest;
1766
1767     protected long _version = -1;
1768     protected long _targetVersion = -1;
1769     protected String _appbase;
1770     protected URL _vappbase;
1771     protected URL _latest;
1772     protected String _class;
1773     protected String _dockName;
1774     protected String _dockIconPath;
1775     protected boolean _strictComments;
1776     protected boolean _windebug;
1777     protected boolean _allowOffline;
1778     protected int _maxConcDownloads;
1779
1780     protected String _trackingURL;
1781     protected Set<Integer> _trackingPcts;
1782     protected String _trackingCookieName;
1783     protected String _trackingCookieProperty;
1784     protected String _trackingURLSuffix;
1785     protected String _trackingGAHash;
1786     protected long _trackingStart;
1787     protected int _trackingId;
1788
1789     protected String _javaVersionProp = "java.version";
1790     protected String _javaVersionRegex = "(\\d+)(?:\\.(\\d+)(?:\\.(\\d+)(_\\d+)?)?)?";
1791     protected long _javaMinVersion, _javaMaxVersion;
1792     protected boolean _javaExactVersionRequired;
1793     protected String _javaLocation;
1794
1795     protected List<Resource> _codes = new ArrayList<>();
1796     protected List<Resource> _resources = new ArrayList<>();
1797
1798     protected boolean _useCodeCache;
1799     protected int _codeCacheRetentionDays;
1800
1801     protected Map<String,AuxGroup> _auxgroups = new HashMap<>();
1802     protected Map<String,Boolean> _auxactive = new HashMap<>();
1803
1804     protected List<String> _jvmargs = new ArrayList<>();
1805     protected List<String> _appargs = new ArrayList<>();
1806
1807     protected String[] _optimumJvmArgs;
1808
1809     protected List<String> _txtJvmArgs = new ArrayList<>();
1810
1811     /** If a warning has been issued about not being able to set modtimes. */
1812     protected boolean _warnedAboutSetLastModified;
1813
1814     /** Locks gettingdown.lock in the app dir. Held the entire time updating is going on.*/
1815     protected FileLock _lock;
1816
1817     /** Channel to the file underlying _lock.  Kept around solely so the lock doesn't close. */
1818     protected FileChannel _lockChannel;
1819
1820     protected Random _rando = new Random();
1821
1822     protected static final String[] EMPTY_STRING_ARRAY = new String[0];
1823
1824     protected static final String ENV_VAR_PREFIX = "%ENV.";
1825     protected static final Pattern ENV_VAR_PATTERN = Pattern.compile("%ENV\\.(.*?)%");
1826 }