diff options
Diffstat (limited to 'jetty-start/src/main/java/org/eclipse/jetty/start/Main.java')
-rw-r--r-- | jetty-start/src/main/java/org/eclipse/jetty/start/Main.java | 440 |
1 files changed, 7 insertions, 433 deletions
diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java b/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java index 33ee63035b..87887ff8b5 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/Main.java @@ -21,37 +21,23 @@ package org.eclipse.jetty.start; import static org.eclipse.jetty.start.UsageException.*; import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; -import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.ConnectException; import java.net.InetAddress; import java.net.Socket; import java.net.SocketTimeoutException; -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardOpenOption; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Locale; -import java.util.Set; -import java.util.regex.Pattern; import org.eclipse.jetty.start.config.CommandLineConfigSource; -import org.eclipse.jetty.start.fileinits.MavenLocalRepoFileInitializer; -import org.eclipse.jetty.start.fileinits.TestFileInitializer; -import org.eclipse.jetty.start.fileinits.UriFileInitializer; /** * Main start class. @@ -82,7 +68,6 @@ import org.eclipse.jetty.start.fileinits.UriFileInitializer; */ public class Main { - private static final String EXITING_LICENSE_NOT_ACKNOWLEDGED = "Exiting: license not acknowledged!"; private static final int EXIT_USAGE = 1; public static void main(String[] args) @@ -273,222 +258,6 @@ public class Main } /** - * Build out INI file. - * <p> - * This applies equally for either <code>${jetty.base}/start.ini</code> or - * <code>${jetty.base}/start.d/${name}.ini</code> - * - * @param fileInitializers the configured initializers - * @param args the arguments of what modules are enabled - * @param name the name of the module to based the build of the ini - * @param topLevel - * @param appendStartIni true to append to <code>${jetty.base}/start.ini</code>, - * false to create a <code>${jetty.base}/start.d/${name}.ini</code> entry instead. - * @throws IOException - */ - private void buildIni(List<FileInitializer> fileInitializers, StartArgs args, String name, boolean topLevel, boolean appendStartIni) throws IOException - { - // Find the start.d relative to the base directory only. - Path start_d = baseHome.getBasePath("start.d"); - - // Is this a module? - Modules modules = args.getAllModules(); - Module module = modules.get(name); - if (module == null) - { - StartLog.warn("ERROR: No known module for %s",name); - return; - } - - boolean transitive = module.isEnabled() && (module.getSources().size() == 0); - - // Find any named ini file and check it follows the convention - Path start_ini = baseHome.getBasePath("start.ini"); - String short_start_ini = baseHome.toShortForm(start_ini); - Path startd_ini = start_d.resolve(name + ".ini"); - String short_startd_ini = baseHome.toShortForm(startd_ini); - StartIni module_ini = null; - if (FS.exists(startd_ini)) - { - module_ini = new StartIni(startd_ini); - if (module_ini.getLineMatches(Pattern.compile("--module=(.*, *)*" + name)).size() == 0) - { - StartLog.warn("ERROR: %s is not enabled in %s!",name,short_startd_ini); - return; - } - } - - if (!args.isApproveAllLicenses()) - { - if (!module.acknowledgeLicense()) - { - StartLog.warn(EXITING_LICENSE_NOT_ACKNOWLEDGED); - System.exit(1); - } - } - - boolean buildIni=false; - if (module.isEnabled()) - { - // is it an explicit request to create an ini file? - if (topLevel && !FS.exists(startd_ini) && !appendStartIni) - { - buildIni=true; - } - // else is it transitive - else if (transitive) - { - if (module.hasDefaultConfig()) - { - buildIni = true; - StartLog.info("%-15s initialised transitively",name); - } - } - // else must be initialized explicitly - else - { - for (String source : module.getSources()) - { - StartLog.info("%-15s initialised in %s",name,baseHome.toShortForm(source)); - } - } - } - else - { - buildIni=true; - } - - String source = "<transitive>"; - - // If we need an ini - if (buildIni) - { - // File BufferedWriter - BufferedWriter writer = null; - PrintWriter out = null; - try - { - if (appendStartIni) - { - source = short_start_ini; - StartLog.info("%-15s initialised in %s (appended)",name,source); - writer = Files.newBufferedWriter(start_ini,StandardCharsets.UTF_8,StandardOpenOption.CREATE,StandardOpenOption.APPEND); - out = new PrintWriter(writer); - } - else - { - // Create the directory if needed - FS.ensureDirectoryExists(start_d); - FS.ensureDirectoryWritable(start_d); - source = short_startd_ini; - StartLog.info("%-15s initialised in %s (created)",name,source); - writer = Files.newBufferedWriter(startd_ini,StandardCharsets.UTF_8,StandardOpenOption.CREATE_NEW,StandardOpenOption.WRITE); - out = new PrintWriter(writer); - } - - if (appendStartIni) - { - out.println(); - } - out.println("# --------------------------------------- "); - out.println("# Module: " + name); - - out.println("--module=" + name); - - args.parse("--module=" + name,source); - args.parseModule(module); - - for (String line : module.getDefaultConfig()) - { - out.println(line); - } - } - finally - { - if (out != null) - { - out.close(); - } - } - } - - modules.enable(name,Collections.singletonList(source)); - - // Also list other places this module is enabled - for (String src : module.getSources()) - { - StartLog.debug("also enabled in: %s",src); - if (!short_start_ini.equals(src)) - { - StartLog.info("%-15s enabled in %s",name,baseHome.toShortForm(src)); - } - } - - // Do downloads now - List<FileArg> files = new ArrayList<FileArg>(); - for (String file : module.getFiles()) - { - files.add(new FileArg(module,file)); - } - processFileResources(fileInitializers,args,files); - - // Process dependencies - module.expandProperties(args.getProperties()); - modules.registerParentsIfMissing(module); - modules.buildGraph(); - - // process new ini modules - if (topLevel) - { - List<Module> depends = new ArrayList<>(); - for (String depend : modules.resolveParentModulesOf(name)) - { - if (!name.equals(depend)) - { - Module m = modules.get(depend); - m.setEnabled(true); - depends.add(m); - } - } - Collections.sort(depends,Collections.reverseOrder(new Module.DepthComparator())); - - Set<String> done = new HashSet<>(0); - while (true) - { - // initialize known dependencies - boolean complete=true; - for (Module m : depends) - { - if (!done.contains(m.getName())) - { - complete=false; - buildIni(fileInitializers,args,m.getName(),false,appendStartIni); - done.add(m.getName()); - } - } - - if (complete) - { - break; - } - - // look for any new ones resolved via expansion - depends.clear(); - for (String depend : modules.resolveParentModulesOf(name)) - { - if (!name.equals(depend)) - { - Module m = modules.get(depend); - m.setEnabled(true); - depends.add(m); - } - } - Collections.sort(depends,Collections.reverseOrder(new Module.DepthComparator())); - } - } - } - - /** * Convenience for <code>processCommandLine(cmdLine.toArray(new String[cmdLine.size()]))</code> */ public StartArgs processCommandLine(List<String> cmdLine) throws Exception @@ -534,7 +303,8 @@ public class Main modules.buildGraph(); args.setAllModules(modules); - List<Module> activeModules = modules.resolveEnabled(); + List<Module> activeModules = modules.getEnabled(); + modules.assertModulesValid(activeModules); // ------------------------------------------------------------ // 5) Lib & XML Expansion / Resolution @@ -552,139 +322,6 @@ public class Main return args; } - /** - * Process the {@link FileArg} for startup, assume that all licenses have - * been acknowledged at this stage. - * - * @param fileInitializers the file initializer mechanisms. - * @param args the start arguments - */ - private void processFileResources(List<FileInitializer> fileInitializers, StartArgs args) throws IOException - { - processFileResources(fileInitializers,args,args.getFiles()); - } - - /** - * Process the {@link FileArg} for startup, assume that all licenses have - * been acknowledged at this stage. - * - * @param fileInitializers the file initializer mechanisms. - * @param args the start arguments - * @param files the list of {@link FileArg}s to process - */ - private void processFileResources(List<FileInitializer> fileInitializers, StartArgs args, List<FileArg> files) throws IOException - { - List<String> failures = new ArrayList<String>(); - - for (FileArg arg : files) - { - Path file = baseHome.getBasePath(arg.location); - try - { - if (!processFileResource(fileInitializers,args,arg,file)) - { - failures.add(String.format("[GenericError] %s",file.toAbsolutePath().toString())); - } - } - catch (Throwable t) - { - StartLog.warn(t); - failures.add(String.format("[%s] %s - %s",t.getClass().getSimpleName(),t.getMessage(),file.toAbsolutePath().toString())); - } - } - - if (failures.isEmpty()) - { - return; - } - - StartLog.warn("Failed to process all file resources."); - for (String failure : failures) - { - StartLog.warn(" - %s",failure); - } - } - - private boolean processFileResource(List<FileInitializer> fileInitializers, StartArgs args, FileArg arg, Path file) throws IOException - { - if (args.isDownload() && arg.uri != null) - { - URI uri = URI.create(arg.uri); - - // Process via initializers - for (FileInitializer finit : fileInitializers) - { - if (finit.init(uri,file)) - { - // Completed successfully - return true; - } - } - - return false; - } - else - { - // Process directly - boolean isDir = arg.location.endsWith("/"); - - if (FS.exists(file)) - { - // Validate existence - if (isDir) - { - if (!Files.isDirectory(file)) - { - throw new IOException("Invalid: path should be a directory (but isn't): " + file); - } - if (!FS.canReadDirectory(file)) - { - throw new IOException("Unable to read directory: " + file); - } - } - else - { - if (!FS.canReadFile(file)) - { - throw new IOException("Unable to read file: " + file); - } - } - - return true; - } - - if (isDir) - { - // Create directory - StartLog.log("MKDIR",baseHome.toShortForm(file)); - FS.ensureDirectoryExists(file); - - return true; - } - else - { - // Warn on missing file (this has to be resolved manually by - // user) - String shortRef = baseHome.toShortForm(file); - if (args.isTestingModeEnabled()) - { - StartLog.log("TESTING MODE","Skipping required file check on: %s",shortRef); - return true; - } - - StartLog.warn("Missing Required File: %s",baseHome.toShortForm(file)); - args.setRun(false); - if (arg.uri != null) - { - StartLog.warn(" Can be downloaded From: %s",arg.uri); - StartLog.warn(" Run start.jar --create-files to download"); - } - - return false; - } - } - } - public void start(StartArgs args) throws IOException, InterruptedException { StartLog.debug("StartArgs: %s",args); @@ -740,74 +377,13 @@ public class Main doStop(args); } - // Establish FileInitializers - List<FileInitializer> fileInitializers = new ArrayList<FileInitializer>(); - if (args.isTestingModeEnabled()) + BaseBuilder baseBuilder = new BaseBuilder(baseHome,args); + if(baseBuilder.build()) { - // No downloads performed - fileInitializers.add(new TestFileInitializer()); - } - else - { - // Downloads are allowed to be performed - - // Setup Maven Local Repo - Path localRepoDir = args.getMavenLocalRepoDir(); - if (localRepoDir != null) - { - // Use provided local repo directory - fileInitializers.add(new MavenLocalRepoFileInitializer(localRepoDir)); - } - else - { - // No no local repo directory (direct downloads) - fileInitializers.add(new MavenLocalRepoFileInitializer()); - } - - // Normal URL downloads - fileInitializers.add(new UriFileInitializer()); - } - - boolean rebuildGraph = false; - - // Initialize start.ini - for (String module : args.getAddToStartIni()) - { - buildIni(fileInitializers,args,module,true,true); - rebuildGraph = true; - } - - // Initialize start.d - for (String module : args.getAddToStartdIni()) - { - buildIni(fileInitializers,args,module,true,false); - rebuildGraph = true; - } - - if (rebuildGraph) - { - args.getAllModules().clearMissing(); - args.getAllModules().buildGraph(); - } - - // If in --create-files, check licenses - if(args.isDownload()) - { - if (!args.isApproveAllLicenses()) - { - StartLog.debug("Requesting License Acknowledgement"); - for (Module module : args.getAllModules().resolveEnabled()) - { - if (!module.acknowledgeLicense()) - { - StartLog.warn(EXITING_LICENSE_NOT_ACKNOWLEDGED); - System.exit(1); - } - } - } + // base directory changed. + StartLog.info("Base directory was modified"); + return; } - - processFileResources(fileInitializers, args); // Informational command line, don't run jetty if (!args.isRun()) @@ -860,8 +436,6 @@ public class Main } } - - private void doStop(StartArgs args) { int stopPort = Integer.parseInt(args.getProperties().getString("STOP.PORT")); |