Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Added proper usage instructions for new argument parsing policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mordechaim committed Nov 12, 2018
1 parent 08c9971 commit c0381b5
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/main/java/org/update4j/service/DefaultBootstrap.java
Expand Up @@ -21,6 +21,7 @@
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -34,7 +35,6 @@
import org.update4j.Configuration;
import org.update4j.SingleInstanceManager;
import org.update4j.Update;
import org.update4j.util.StringUtils;

public class DefaultBootstrap implements Delegate {

Expand Down Expand Up @@ -151,7 +151,7 @@ private void parseArgs(List<String> bootArgs) {
} else if (arg.equals("--delegate")) {
i++;
validateHasValue("delegate", bootArgs, i);
continue; // ignore
continue; // just ignore
}

Matcher m = Pattern.compile("--remote" + PATTERN).matcher(arg);
Expand Down Expand Up @@ -192,8 +192,8 @@ private void validateNotSet(String val, String command) {
throw new IllegalArgumentException("Duplicate --" + command + " command.");
}

private void validateHasValue(String argName, List<?> list, int index) {
if (index == list.size()) {
private void validateHasValue(String argName, List<String> list, int index) {
if (index >= list.size() || list.get(index).startsWith("--")) {
throw new IllegalArgumentException("Missing value for \"--" + argName + "\".");
}
}
Expand All @@ -203,7 +203,7 @@ private void updateFirst(List<String> args) throws Throwable {
Configuration localConfig = null;

if (remote != null) {
try (Reader in = new InputStreamReader(new URL(remote).openStream())) {
try (Reader in = openConnection(new URL(remote))) {
remoteConfig = getConfig(in);
}
}
Expand Down Expand Up @@ -264,7 +264,7 @@ private void launchFirst(List<String> args) throws Throwable {

Configuration remoteConfig = null;
if (remote != null) {
try (Reader in = new InputStreamReader(new URL(remote).openStream())) {
try (Reader in = openConnection(new URL(remote))) {
remoteConfig = getConfig(in);
}
}
Expand Down Expand Up @@ -300,6 +300,20 @@ private void launchFirst(List<String> args) throws Throwable {
}

}

private Reader openConnection(URL url) throws IOException {

URLConnection connection = url.openConnection();

// Some downloads may fail with HTTP/403, this may solve it
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
// Set a connection timeout of 10 seconds
connection.setConnectTimeout(10 * 1000);
// Set a read timeout of 10 seconds
connection.setReadTimeout(10 * 1000);

return new InputStreamReader(connection.getInputStream());
}

private Configuration getConfig(Reader in) {
try {
Expand All @@ -321,7 +335,7 @@ private Configuration getConfig(Reader in) {
private static void welcome() {

System.out.println(getLogo() + "\tWelcome to the update4j framework.\n\n"
+ "\tYou started the framework with its default settings, which does\n"
+ "\tYou started the framework with its default bootstrap, which does\n"
+ "\tthe update and launch logic for you without complex setup. All you need is to\n"
+ "\tspecify some settings via command line arguments.\n\n"
+ "\tBefore you start, you first need to create a \"configuration\" file that contains\n"
Expand All @@ -332,17 +346,18 @@ private static void welcome() {
+ "\thttp://docs.update4j.org/javadoc/update4j/org/update4j/Configuration.html\n\n"
+ "\tWhile the default bootstrap works perfectly for a majority of cases, you might\n"
+ "\tfurther customize the update and launch life-cycle to the last detail by\n"
+ "\timplementing a custom bootstrap and update/launch your business application\n"
+ "\tusing the Configuration.update() and Configuration.launch() methods.\n\n"
+ "\tIf you choose to implement your own bootstrap, there are 2 ways to do it:\n\n"
+ "\t\t- Standard Mode: Start the bootstrap application using your own main method.\n"
+ "\t\t You will not be able to update the bootstrap application (as code cannot update itself),\n"
+ "\t\t only the business application will be updatable.\n\n"
+ "\t\t- Delegate Mode: Move your main method into an implementation of Delegate\n"
+ "\t\t and start the framework just as you did now, i.e. calling \"update4j's\" main method.\n"
+ "\t\t and start the framework just as you did now, i.e. calling *update4j's* main method.\n"
+ "\t\t This allows you to update the bootstrap application by releasing a newer version\n"
+ "\t\t with a higher version() number and make it visible to the JVM boot classpath\n"
+ "\t\t or modulepath by placing it in the right directory.\n"
+ "\t\t It it recommended not to use this feature before you can get everything\n"
+ "\t\t It is recommended not to use this feature before you can get everything\n"
+ "\t\t to run smoothly in Standard Mode, as this adds an extra layer of complexity.\n\n"
+ "\tFor more details about implementing the bootstrap, please refer to the Github wiki:\n"
+ "\thttps://github.com/update4j/update4j/wiki/Documentation#lifecycle\n"
Expand All @@ -356,8 +371,11 @@ private static String getLogo() {

return

"\n" + "\t _ _ ___ _ \n" + "\t | | | | / (_)\n"
+ "\t _ _ _ __ __| | __ _| |_ ___ / /| |_ \n" + "\t| | | | '_ \\ / _` |/ _` | __/ _ \\/ /_| | |\n"
"\n"
+ "\t _ _ ___ _ \n"
+ "\t | | | | / (_)\n"
+ "\t _ _ _ __ __| | __ _| |_ ___ / /| |_ \n"
+ "\t| | | | '_ \\ / _` |/ _` | __/ _ \\/ /_| | |\n"
+ "\t| |_| | |_) | (_| | (_| | || __/\\___ | |\n"
+ "\t \\__,_| .__/ \\__,_|\\__,_|\\__\\___| |_/ |\n"
+ "\t | | _/ |\n"
Expand All @@ -369,20 +387,25 @@ private static String getLogo() {

private static void usage() {

System.err.println("To start in modulepath:\n\n" + "\tjava -p update4j-" + Bootstrap.VERSION
+ ".jar -m org.update4j [commands...]\n" + "\tjava -p . -m org.update4j [commands...]\n\n"
+ "To start in classpath:\n\n" + "\tjava -jar update4j-" + Bootstrap.VERSION + ".jar [commands...]\n"
+ "\tjava -cp update4j-" + Bootstrap.VERSION + ".jar org.update4j.Bootstrap [commands...]\n"
+ "\tjava -cp * org.update4j.Bootstrap [commands...]\n\n" + "Available commands:\n\n"
+ "\t--remote=[url] - The remote (or if using file:/// scheme - local) location of the\n"
System.err.println("To start in modulepath:\n\n"
+ "\tjava -p update4j-" + Bootstrap.VERSION
+ ".jar -m org.update4j [commands...] [-- business-args...]\n"
+ "\tjava -p . -m org.update4j [commands...] [-- business-args...]\n\n"
+ "To start in classpath:\n\n" + "\tjava -jar update4j-"
+ Bootstrap.VERSION + ".jar [commands...] [-- business-args...]\n"
+ "\tjava -cp update4j-" + Bootstrap.VERSION
+ ".jar org.update4j.Bootstrap [commands...] [-- business-args...]\n"
+ "\tjava -cp * org.update4j.Bootstrap [commands...] [-- business-args...]\n\n"
+ "Available commands:\n\n"
+ "\t--remote [url] - The remote (or if using file:/// scheme - local) location of the\n"
+ "\t\tconfiguration file. If it fails to download or command is missing, it will\n"
+ "\t\tfall back to local.\n\n"
+ "\t--local=[path] - The path of a local configuration to use if the remote failed to download\n"
+ "\t--local [path] - The path of a local configuration to use if the remote failed to download\n"
+ "\t\tor was not passed. If both remote and local fail, startup fails.\n\n"
+ "\t--syncLocal - Sync the local configuration with the remote if it downloaded successfully.\n"
+ "\t\tUseful to still allow launching without Internet connection. Default will not sync unless\n"
+ "\t\t--launchFirst was specified.\n\n"
+ "\t--cert=[path] - A path to an X.509 certificate file to use to verify signatures. If missing,\n"
+ "\t--syncLocal - Sync the local configuration with the remote if it downloaded, loaded and\n"
+ "\t\tupdated files successfully. Useful to still allow launching without Internet connection.\n"
+ "\t\tDefault will not sync unless --launchFirst was specified.\n\n"
+ "\t--cert [path] - A path to an X.509 certificate file to use to verify signatures. If missing,\n"
+ "\t\tno signature verification will be performed.\n\n"
+ "\t--launchFirst - If specified, it will first launch the local application then silently\n"
+ "\t\tdownload the update; the update will be available only on next restart. It will still\n"
Expand All @@ -395,7 +418,8 @@ private static void usage() {
+ "\t\tand failed.\n\n"
+ "\t--singleInstance - Run the application as a single instance. Any subsequent attempts\n"
+ "\t\tto run will just exit. You can better control this feature by directly using the\n"
+ "\t\tSingleInstanceManager class.\n");
+ "\t\tSingleInstanceManager class.\n\n\n"
+ "To pass arguments to the business application, separate them with '--' (w/o quotes).");

}
}

0 comments on commit c0381b5

Please sign in to comment.