0d96ecb7155033b7333b500a9ee1a7aea00a173f
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / data / SysProps.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.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 import com.threerings.getdown.util.VersionUtil;
12
13 /**
14  * This class encapsulates all system properties that are read and processed by Getdown. Don't
15  * stick a call to {@code System.getProperty} randomly into the code, put it in here and give it an
16  * accessor so that it's easy to see all of the secret system property arguments that Getdown makes
17  * use of.
18  */
19 public class SysProps
20 {
21     /** Configures the appdir (in lieu of passing it in argv). Usage: {@code -Dappdir=foo}. */
22     public static String appDir () {
23         return System.getProperty("appdir");
24     }
25
26     /** Configures the appid (in lieu of passing it in argv). Usage: {@code -Dappid=foo}. */
27     public static String appId () {
28         return System.getProperty("appid");
29     }
30
31     /** Configures the bootstrap appbase (used in lieu of providing a skeleton getdown.txt, and as
32       * a last resort fallback). Usage: {@code -Dappbase=URL}. */
33     public static String appBase () {
34         return System.getProperty("appbase");
35     }
36
37     /** If true, disables redirection of logging into {@code launcher.log}.
38       * Usage: {@code -Dno_log_redir}. */
39     public static boolean noLogRedir () {
40         return System.getProperty("no_log_redir") != null;
41     }
42
43     /** Overrides the domain on {@code appbase}. Usage: {@code -Dappbase_domain=foo}. */
44     public static String appbaseDomain () {
45         return System.getProperty("appbase_domain");
46     }
47
48     /** Overrides enter {@code appbase}. Usage: {@code -Dappbase_override=URL}. */
49     public static String appbaseOverride () {
50         return System.getProperty("appbase_override");
51     }
52
53     /** If true, Getdown installs the app without ever bringing up a UI (except in the event of an
54       * error). NOTE: it does not launch the app. See {@link #launchInSilent}.
55       * Usage: {@code -Dsilent}. */
56     public static boolean silent () {
57         return System.getProperty("silent") != null;
58     }
59
60     /** Instructs Getdown to install/update the app without ever bringing up a UI (except in the
61       * event of an error), and then launch it.
62       * Usage: {@code -Dsilent=launch}. */
63     public static boolean launchInSilent () {
64         return "launch".equals(System.getProperty("silent"));
65     }
66
67     /**
68      * Instructs Getdown to launch the app without updating it, or ever bringing up a UI (except
69      * in the event of an error).
70      * Usage: {@code -Dsilent=noupdate}.
71      */
72     public static boolean noUpdate() {
73         return "noupdate".equals(System.getProperty("silent"));
74     }
75
76     /** If true, Getdown does not automatically install updates after downloading them. It waits
77       * for the application to call `Getdown.install`.
78       * Usage: {@code -Dno_install}. */
79     public static boolean noInstall () {
80         return System.getProperty("no_install") != null;
81     }
82
83     /** Specifies the delay (in minutes) to wait before starting the update and install process.
84       * Minimum delay is 0 minutes, or no delay (negative values are rounded up to 0 minutes).
85       * Maximum delay is 1 day, or 1440 minutes (larger values are rounded down to 1 day).
86       * Usage: {@code -Ddelay=N}. */
87     public static int startDelay () {
88         return Math.min(Math.max(Integer.getInteger("delay", 0), 0), 60 * 24);
89     }
90
91     /** If true, Getdown will not unpack {@code uresource} jars. Usage: {@code -Dno_unpack}. */
92     public static boolean noUnpack () {
93         return Boolean.getBoolean("no_unpack");
94     }
95
96     /** If true, Getdown will run the application in the same VM in which Getdown is running. If
97       * false (the default), Getdown will fork a new VM. Note that reusing the same VM prevents
98       * Getdown from configuring some launch-time-only VM parameters (like -mxN etc.).
99       * Usage: {@code -Ddirect}. */
100     public static boolean direct () {
101         return Boolean.getBoolean("direct");
102     }
103
104     /** Specifies the connection timeout (in seconds) to use when downloading control files from
105       * the server. This is chiefly useful when you are running in versionless mode and want Getdown
106       * to more quickly timeout its startup update check if the server with which it is
107       * communicating is not available. Usage: {@code -Dconnect_timeout=N}. */
108     public static int connectTimeout () {
109         return Integer.getInteger("connect_timeout", 0);
110     }
111
112     /** Specifies the read timeout (in seconds) to use when downloading all files from the server.
113       * The default is 30 seconds, meaning that if a download stalls for more than 30 seconds, the
114       * update process wil fail. Setting the timeout to zero (or a negative value) will disable it.
115       * Usage: {@code -Dread_timeout=N}. */
116     public static int readTimeout () {
117         return Integer.getInteger("read_timeout", 30);
118     }
119
120     /** Returns the number of threads used to perform digesting and verifying operations in
121       * parallel. Usage: {@code -Dthread_pool_size=N} */
122     public static int threadPoolSize () {
123         int defaultSize = Math.max(Runtime.getRuntime().availableProcessors()-1, 1);
124         return Integer.getInteger("thread_pool_size", defaultSize);
125     }
126
127     /** Parses a Java version system property using the supplied regular expression. The numbers
128       * extracted from the regexp will be placed in each consecutive hundreds position in the
129       * returned value.
130       *
131       * <p>For example, {@code java.version} takes the form {@code 1.8.0_31}, and with the regexp
132       * {@code (\d+)\.(\d+)\.(\d+)(_\d+)?} we would parse {@code 1, 8, 0, 31} and combine them into
133       * the final value {@code 1080031}.
134       *
135       * <p>Note that non-numeric characters matched by the regular expression will simply be
136       * ignored, and optional groups which do not match are treated as zero in the final version
137       * calculation.
138       *
139       * <p>One can instead parse {@code java.runtime.version} which takes the form {@code
140       * 1.8.0_31-b13}. Using regexp {@code (\d+)\.(\d+)\.(\d+)_(\d+)-b(\d+)} we would parse
141       * {@code 1, 8, 0, 31, 13} and combine them into a final value {@code 108003113}.
142       *
143       * <p>Other (or future) JVMs may provide different version properties which can be parsed as
144       * desired using this general scheme as long as the numbers appear from left to right in order
145       * of significance.
146       *
147       * @throws IllegalArgumentException if no system named {@code propName} exists, or if
148       * {@code propRegex} does not match the returned version string.
149       */
150     public static long parseJavaVersion (String propName, String propRegex) {
151         String verstr = System.getProperty(propName);
152         if (verstr == null) throw new IllegalArgumentException(
153             "No system property '" + propName + "'.");
154
155         long vers = VersionUtil.parseJavaVersion(propRegex, verstr);
156         if (vers == 0L) throw new IllegalArgumentException(
157             "Regexp '" + propRegex + "' does not match '" + verstr + "' (from " + propName + ")");
158         return vers;
159     }
160
161     /**
162      * Applies {@code appbase_override} or {@code appbase_domain} if they are set.
163      */
164     public static String overrideAppbase (String appbase) {
165         String appbaseOverride = appbaseOverride();
166         if (appbaseOverride != null) {
167             return appbaseOverride;
168         } else {
169             return replaceDomain(appbase);
170         }
171     }
172
173     /**
174      * If appbase_domain property is set, replace the domain on the provided string.
175      */
176     public static String replaceDomain (String appbase)
177     {
178         String appbaseDomain = appbaseDomain();
179         if (appbaseDomain != null) {
180             Matcher m = Pattern.compile("(https?://[^/]+)(.*)").matcher(appbase);
181             appbase = m.replaceAll(appbaseDomain + "$2");
182         }
183         return appbase;
184     }
185 }