diff --git a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/Environment.java b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/Environment.java index df2692e99e4..6e9170d0ad3 100644 --- a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/Environment.java +++ b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/Environment.java @@ -13,9 +13,12 @@ package org.eclipse.jetty.start; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -79,6 +82,9 @@ public class Environment public void addUniquePropertyFile(String propertyFileRef, Path propertyFile) throws IOException { + if (!"core".equalsIgnoreCase(getName())) + throw new IllegalStateException("Property files not supported in non core environments"); + if (!FS.canReadFile(propertyFile)) { throw new IOException("Cannot read file: " + propertyFileRef); @@ -287,6 +293,67 @@ public class Environment return _propertyFiles; } + public void generateXml(Environment coreEnvironment, Path envPath) throws IOException + { + try (OutputStream out = Files.newOutputStream(envPath)) + { + String todo = """ + valueA + lib/feature/foo.jar + etc/foo.xml + """; + + StringBuilder properties = new StringBuilder(); + for (Props.Prop prop: coreEnvironment.getProperties()) + properties.append(" ").append(prop.value).append("\n"); + properties.append(" \n"); + for (Props.Prop prop: getProperties()) + properties.append(" ").append(prop.value).append("\n"); + + StringBuilder classpaths = new StringBuilder(); + for (File classpath : getClasspath().getElements()) + classpaths.append(" ").append(classpath.getAbsolutePath()).append("\n"); + + StringBuilder xmls = new StringBuilder(); + for (Path xml : getXmlFiles()) + xmls.append(" ").append(xml.toAbsolutePath()).append("\n"); + + out.write(""" + + + + + %s + + Server + + + + + %s + + + + %s + + + + + + + %s + + + + + + + + + """.formatted(getName(), properties, classpaths, xmls, getName(), getName()).getBytes(StandardCharsets.UTF_8)); + } + } + @Override public String toString() { diff --git a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java index fc78216147f..cce401605c1 100644 --- a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java +++ b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java @@ -51,7 +51,7 @@ import org.eclipse.jetty.util.ManifestUtils; public class StartArgs { public static final String VERSION; - public static final Set ALL_PARTS = Set.of("java", "opts", "path", "main", "args"); + public static final Set ALL_PARTS = Set.of("java", "opts", "path", "main", "args", "envs"); public static final Set ARG_PARTS = Set.of("args"); private static final String JETTY_VERSION_KEY = "jetty.version"; @@ -483,7 +483,6 @@ public class StartArgs parts = ALL_PARTS; CommandLineBuilder cmd = new CommandLineBuilder(); - Environment environment = getCoreEnvironment(); // Special Stop/Shutdown properties ensureSystemPropertySet("STOP.PORT"); @@ -508,11 +507,11 @@ public class StartArgs String value = assign.length == 1 ? "" : assign[1]; Prop p = processSystemProperty(key, value, null); - cmd.addRawArg("-D" + p.key + "=" + environment.getProperties().expand(p.value)); + cmd.addRawArg("-D" + p.key + "=" + coreEnvironment.getProperties().expand(p.value)); } else { - cmd.addRawArg(environment.getProperties().expand(x)); + cmd.addRawArg(coreEnvironment.getProperties().expand(x)); } } @@ -528,7 +527,7 @@ public class StartArgs { if (isJPMS()) { - Map> dirsAndFiles = StreamSupport.stream(environment.getClasspath().spliterator(), false) + Map> dirsAndFiles = StreamSupport.stream(coreEnvironment.getClasspath().spliterator(), false) .collect(Collectors.groupingBy(File::isDirectory)); List files = dirsAndFiles.get(false); if (files != null && !files.isEmpty()) @@ -554,7 +553,7 @@ public class StartArgs else { cmd.addRawArg("--class-path"); - cmd.addRawArg(environment.getClasspath().toString()); + cmd.addRawArg(coreEnvironment.getClasspath().toString()); } } @@ -565,18 +564,20 @@ public class StartArgs cmd.addRawArg(getMainClassname()); } - // pass properties as args or as a file + // do properties and xmls if (parts.contains("args")) { if (dryRun && execProperties == null) { - for (Prop p : environment.getProperties()) + // pass properties as args + for (Prop p : coreEnvironment.getProperties()) { cmd.addRawArg(CommandLineBuilder.quote(p.key) + "=" + CommandLineBuilder.quote(p.value)); } } - else if (environment.getProperties().size() > 0) + else if (coreEnvironment.getProperties().size() > 0) { + // pass properties as a temp property file Path propPath; if (execProperties == null) { @@ -588,22 +589,37 @@ public class StartArgs try (OutputStream out = Files.newOutputStream(propPath)) { - environment.getProperties().store(out, "start.jar properties"); + coreEnvironment.getProperties().store(out, "start.jar properties"); } cmd.addRawArg(propPath.toAbsolutePath().toString()); } - for (Path xml : environment.getXmlFiles()) + for (Path xml : coreEnvironment.getXmlFiles()) { cmd.addRawArg(xml.toAbsolutePath().toString()); } - for (Path propertyFile : environment.getPropertyFiles()) + for (Path propertyFile : coreEnvironment.getPropertyFiles()) { cmd.addRawArg(propertyFile.toAbsolutePath().toString()); } } + if (parts.contains("envs")) + { + for (Environment environment : getEnvironments()) + { + if (environment == coreEnvironment) + continue; + + Path envPath = Files.createTempFile("env_%s_".formatted(environment.getName()), ".xml"); + environment.generateXml(coreEnvironment, envPath); + if (!isDryRun()) + envPath.toFile().deleteOnExit(); + cmd.addRawArg(envPath.toAbsolutePath().toString()); + } + } + return cmd; } diff --git a/jetty-core/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt b/jetty-core/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt index c392c6b8ee5..93811564034 100644 --- a/jetty-core/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt +++ b/jetty-core/jetty-start/src/main/resources/org/eclipse/jetty/start/usage.txt @@ -71,6 +71,7 @@ Report Commands: o "path" - the JVM class-path and/or the JPMS module-path o "main" - the main class to run o "args" - the arguments passed to the main class + o "envs" - the generated XML files to create the environments Configure Commands: ------------------- diff --git a/jetty-core/jetty-xml/src/main/java/org/eclipse/jetty/xml/EnvironmentBuilder.java b/jetty-core/jetty-xml/src/main/java/org/eclipse/jetty/xml/EnvironmentBuilder.java index 0197944be0e..2be0bd91117 100644 --- a/jetty-core/jetty-xml/src/main/java/org/eclipse/jetty/xml/EnvironmentBuilder.java +++ b/jetty-core/jetty-xml/src/main/java/org/eclipse/jetty/xml/EnvironmentBuilder.java @@ -32,43 +32,6 @@ import org.eclipse.jetty.util.resource.Resource; /** * A Builder of {@link Environment}s intended to be used in XML * files generated by start.jar. - * * */ public class EnvironmentBuilder extends AbstractMap