org.update4j.Bootstrap Maven / Gradle / Ivy
/*
* Copyright 2018 Mordechai Meisels
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.update4j;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.update4j.service.Delegate;
import org.update4j.service.Service;
import org.update4j.util.StringUtils;
/**
* This class consists of convenience methods and the module's main method to
* locate and start the bootstrap application in Delegate
* Mode.
*
* @author Mordechai Meisels
*
*/
public class Bootstrap {
/**
* The version of the current build of the framework.
*/
public static final String VERSION = "1.2.2";
/**
* The main method to start the bootstrap application in Delegate Mode from
* command-line.
*
*
* Here's how you should run the application as a module:
*
*
* $ java --module-path . --module org.update4j
*
*
* Or in shorthand:
*
*
* $ java -p . -m org.update4j
*
*
* For more info consult Starting
* the Application
*
*
* By default it will try to locate the highest versioned provider of
* {@link Delegate} (specified by {@link Service#version()}) currently present
* in the classpath or modulepath. You may override this behavior by passing the
* delegate class name using the {@code --delegate} option:
*
*
* $ java --module-path . --module org.update4j --delegate=com.example.MyDelegate
*
*
* The class name should be the Canonical Class Name i.e. the String
* returned when calling {@link Class#getCanonicalName()}.
*
*
*
* If the system cannot locate the passed class it will fall back to the
* default, i.e. the highest version.
*
*/
public static void main(String[] args) throws Throwable {
String override = null;
Pattern pattern = Pattern.compile("--delegate(?:\\s*=)?\\s*(" + StringUtils.CLASS_REGEX + ")");
for (String s : args) {
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
override = matcher.group(1);
break;
}
}
start(override, List.of(args));
}
/**
* Starts the bootstrap by locating the highest versioned provider of
* {@link Delegate} (specified by {@link Service#version()}) currently present
* in the classpath or modulepath.
*
* @throws Throwable
* Any throwable thrown in the bootstrap.
*/
public static void start() throws Throwable {
start((String) null);
}
/**
* Starts the bootstrap by locating the class between the list of advertised
* providers of {@link Delegate} currently present in the classpath or
* modulepath.
*
*
* If the system cannot locate any registered provider with the given name, the
* highest versioned provider (specified by {@link Service#version()}) will be
* used instead.
*
* @throws Throwable
* Any throwable thrown in the bootstrap.
*/
public static void start(String override) throws Throwable {
start(override, List.of());
}
/**
* Starts the bootstrap running the given {@link Delegate}.
*
* @throws Throwable
* Any throwable thrown in the bootstrap.
*/
public static void start(Delegate delegate) throws Throwable {
start(delegate, List.of());
}
/**
* Starts the bootstrap by locating the highest versioned provider of
* {@link Delegate} (specified by {@link Service#version()}) currently present
* in the classpath or modulepath, with the given list as command-line
* arguments.
*
* @throws Throwable
* Any throwable thrown in the bootstrap.
*/
public static void start(List args) throws Throwable {
start((String) null, args);
}
/**
* Starts the bootstrap by locating the class between the list of advertised
* providers of {@link Delegate} currently present in the classpath or
* modulepath, with the given list as command-line arguments.
*
*
* If the system cannot locate any registered provider with the given name, the
* highest versioned provider (specified by {@link Service#version()}) will be
* used instead.
*
* @throws Throwable
* Any throwable thrown in the bootstrap.
*/
public static void start(String override, List args) throws Throwable {
start(Service.loadService(Delegate.class, override), args);
}
/**
* Starts the bootstrap running the given {@link Delegate}, with the given list
* as command-line arguments.
*
* @throws Throwable
* Any throwable thrown in the bootstrap.
*/
public static void start(Delegate delegate, List args) throws Throwable {
delegate.main(args);
}
}