From 3ad746a355e54911b00b5b2e4df08f01a384b482 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 26 Aug 2013 18:04:36 +1000 Subject: [PATCH] 415826 start.jar module --enable and --disable Implemented the options, but not yet created the individual *.ini files --- .../java/org/eclipse/jetty/start/Main.java | 100 +++++++++++++++++- .../org/eclipse/jetty/start/StartArgs.java | 81 +++++++++----- 2 files changed, 156 insertions(+), 25 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 3166768b641..8df126e0373 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 @@ -34,6 +34,7 @@ import java.net.InetAddress; import java.net.Socket; import java.net.SocketTimeoutException; import java.net.URL; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -354,7 +355,7 @@ public class Main for (String enabledModule : args.getEnabledModules()) { StartLog.debug("Enabling module: %s",enabledModule); - modules.enable(enabledModule,args.getModulesSources(enabledModule)); + modules.enable(enabledModule,args.getSources(enabledModule)); } StartLog.debug("Building Module Graph"); modules.buildGraph(); @@ -454,6 +455,17 @@ public class Main download(url); } + // Enables/Disable + for (String module : args.getDisable()) + { + disable(args,module,true); + } + for (String module : args.getEnable()) + { + enable(args,module,true); + } + + // Informational command line, don't run jetty if (!args.isRun()) { @@ -573,6 +585,92 @@ public class Main } } + private void enable(StartArgs args, String name, boolean verbose) throws IOException + { + File start_d=baseHome.getFile("start.d"); + File ini=new File(start_d,name+".ini"); + + // Is it already enabled + if (ini.exists()) + { + if (verbose) + StartLog.warn("Module %s already enabled by: %s",name,baseHome.toShortForm(ini)); + return; + } + + // Is there a disabled ini? + File disabled=new File(start_d,name+".ini.disabled"); + if (!disabled.exists() && baseHome.isBaseDifferent()) + disabled=new File(new File(baseHome.getHomeDir(),"start.d"),name+".ini.disabled"); + if (disabled.exists()) + { + // enable module by copying ini template + System.err.printf("Enabling %s in %s from %s%n",name,baseHome.toShortForm(ini),baseHome.toShortForm(disabled)); + Files.copy(disabled.toPath(),ini.toPath()); + args.parse(baseHome, new StartIni(ini)); + } + else if (args.getAllModules().resolveEnabled().contains(args.getAllModules().get(name))) + { + // No ini template and module is already enabled + List sources=args.getSources(name); + if (sources!=null && sources.size()>0) + for (String s: args.getSources(name)) + StartLog.warn("Module %s is enabled in %s",name,s); + else + StartLog.warn("Module %s is already enabled (see --list-modules)",name); + + } + else if (ini.createNewFile()) + { + System.err.printf("Enabling %s in %s%n",name,baseHome.toShortForm(ini)); + // Create an ini + try(FileOutputStream out = new FileOutputStream(ini);) + { + out.write(("--module="+name+"\n").getBytes("ISO-8859-1")); + } + args.parse(baseHome, new StartIni(ini)); + } + else + { + StartLog.warn("ERROR: Module %s cannot be enabled! ",name); + return; + } + + // Process dependencies + Modules modules = args.getAllModules(); + Module module=modules.get(name); + + for (String parent:module.getParentNames()) + enable(args,parent,false); + } + + private void disable(StartArgs args, String name, boolean verbose) throws IOException + { + File start_d=baseHome.getFile("start.d"); + File ini=new File(start_d,name+".ini"); + + // Is it enabled? + if (ini.exists()) + { + File disabled=new File(start_d,name+".ini.disabled"); + + if (disabled.exists()) + { + StartLog.warn("ERROR: Disabled ini already exists: %s",baseHome.toShortForm(disabled)); + return; + } + + StartLog.warn("Disabling %s from %s",name,baseHome.toShortForm(ini)); + Files.copy(ini.toPath(),disabled.toPath()); + + return; + } + + if (verbose) + StartLog.warn("Module %s, ini file already disabled: %s",name,baseHome.toShortForm(ini)); + + } + private void usage() { String usageResource = "org/eclipse/jetty/start/usage.txt"; diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java b/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java index 0caef39e627..f02c45d29c1 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java @@ -39,6 +39,7 @@ import java.util.Set; */ public class StartArgs { + public static final String CMD_LINE_SOURCE=""; public static final String VERSION; static @@ -67,8 +68,8 @@ public class StartArgs private static final String SERVER_MAIN = "org.eclipse.jetty.xml.XmlConfiguration"; private List commandLine = new ArrayList<>(); - private Set enabledModules = new HashSet<>(); - private Map> moduleSources = new HashMap<>(); + private Set modules = new HashSet<>(); + private Map> sources = new HashMap<>(); private List downloads = new ArrayList<>(); private Classpath classpath; private List xmlRefs = new ArrayList<>(); @@ -76,6 +77,8 @@ public class StartArgs private Properties properties = new Properties(); private Set systemPropertyKeys = new HashSet<>(); private List jvmArgs = new ArrayList<>(); + private List enable = new ArrayList<>(); + private List disable = new ArrayList<>(); private Modules allModules; // Should the server be run? @@ -323,6 +326,16 @@ public class StartArgs return this.commandLine; } + public List getEnable() + { + return enable; + } + + public List getDisable() + { + return disable; + } + public List getDownloads() { return downloads; @@ -330,7 +343,7 @@ public class StartArgs public Set getEnabledModules() { - return this.enabledModules; + return this.modules; } public List getJvmArgs() @@ -338,9 +351,9 @@ public class StartArgs return jvmArgs; } - public List getModulesSources(String enabledModule) + public List getSources(String module) { - return moduleSources.get(enabledModule); + return sources.get(module); } public CommandLineBuilder getMainArgs(BaseHome baseHome, boolean addJavaInit) throws IOException @@ -424,6 +437,22 @@ public class StartArgs return value; } + private List getValues(String arg) + { + String v=getValue(arg); + ArrayList l=new ArrayList<>(); + for (String s:v.split(",")) + { + if (s!=null) + { + s=s.trim(); + if (s.length()>0) + l.add(s); + } + } + return l; + } + public List getXmlFiles() { return xmls; @@ -488,6 +517,9 @@ public class StartArgs { if ("--help".equals(arg) || "-?".equals(arg)) { + if (!CMD_LINE_SOURCE.equals(source)) + throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source); + help = true; run = false; return; @@ -501,6 +533,8 @@ public class StartArgs if ("--stop".equals(arg)) { + if (!CMD_LINE_SOURCE.equals(source)) + throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source); stopCommand = true; run = false; // @@ -541,6 +575,8 @@ public class StartArgs if ("--dry-run".equals(arg) || "--exec-print".equals(arg)) { + if (!CMD_LINE_SOURCE.equals(source)) + throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source); dryRun = true; run = false; return; @@ -554,44 +590,41 @@ public class StartArgs if (arg.startsWith("--enable=")) { - String moduleName = getValue(arg); - // TODO: + if (!CMD_LINE_SOURCE.equals(source)) + throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source); + enable.addAll(getValues(arg)); run = false; return; } if (arg.startsWith("--disable=")) { - String moduleName = getValue(arg); - // TODO: + if (!CMD_LINE_SOURCE.equals(source)) + throw new UsageException(ERR_BAD_ARG,"%s not allowed in %s",arg,source); + disable.addAll(getValues(arg)); run = false; return; } if (arg.startsWith("--module=")) { - for (String moduleName : getValue(arg).split(",")) + for (String moduleName : getValues(arg)) { - if (moduleName == null) + modules.add(moduleName); + List list=sources.get(moduleName); + if (list==null) { - continue; // skip + list=new ArrayList(); + sources.put(moduleName,list); } - moduleName=moduleName.trim(); - enabledModules.add(moduleName); - List sources=moduleSources.get(moduleName); - if (sources==null) - { - sources=new ArrayList(); - moduleSources.put(moduleName,sources); - } - sources.add(source); + list.add(source); } return; } if (arg.startsWith("MODULE=") || arg.startsWith("MODULES=")) { - System.err.println("ERROR: Ignored deprecated arg: "+arg); + StartLog.warn("Ignored deprecated arg: {}",arg); return; } @@ -672,7 +705,7 @@ public class StartArgs { for (String line : commandLine) { - parse(line,""); + parse(line,StartArgs.CMD_LINE_SOURCE); } } @@ -703,7 +736,7 @@ public class StartArgs builder.append("StartArgs [commandLine="); builder.append(commandLine); builder.append(", enabledModules="); - builder.append(enabledModules); + builder.append(modules); builder.append(", xmlRefs="); builder.append(xmlRefs); builder.append(", properties=");