Start environments

Generate XMLs in start.jar to communicate environments
This commit is contained in:
Greg Wilkins 2022-05-04 11:09:13 +02:00
parent 428a9d568f
commit 0681210518
4 changed files with 96 additions and 49 deletions

View File

@ -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 = """
<Put name="propertyA">valueA</Put>
<Item>lib/feature/foo.jar</Item>
<Item>etc/foo.xml</Item>
""";
StringBuilder properties = new StringBuilder();
for (Props.Prop prop: coreEnvironment.getProperties())
properties.append(" <Put name=\"").append(prop.key).append("\">").append(prop.value).append("</Put>\n");
properties.append(" <!-- Environment properties -->\n");
for (Props.Prop prop: getProperties())
properties.append(" <Put name=\"").append(prop.key).append("\">").append(prop.value).append("</Put>\n");
StringBuilder classpaths = new StringBuilder();
for (File classpath : getClasspath().getElements())
classpaths.append(" <Item>").append(classpath.getAbsolutePath()).append("</Item>\n");
StringBuilder xmls = new StringBuilder();
for (Path xml : getXmlFiles())
xmls.append(" <Item>").append(xml.toAbsolutePath()).append("</Item>\n");
out.write("""
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New class="org.eclipse.jetty.xml.EnvironmentBuilder">
<Arg name="name">%s</Arg>
<Call name="putId">
<Arg>Server</Arg>
<Arg><Ref id="Server"/></Arg>
</Call>
<!-- Core Properties -->
%s
<Call name="addClassPath">
<Arg>
<Array type="String">
%s </Array>
</Arg>
</Call>
<Call name="addXml">
<Arg>
<Array type="String">
%s </Array>
</Arg>
</Call>
<Call id="%s" name="build"/>
</New>
<Call name="addEnvironment">
<Arg><Ref id="%s"/></Arg>
</Call>
</Configure>
""".formatted(getName(), properties, classpaths, xmls, getName(), getName()).getBytes(StandardCharsets.UTF_8));
}
}
@Override
public String toString()
{

View File

@ -51,7 +51,7 @@ import org.eclipse.jetty.util.ManifestUtils;
public class StartArgs
{
public static final String VERSION;
public static final Set<String> ALL_PARTS = Set.of("java", "opts", "path", "main", "args");
public static final Set<String> ALL_PARTS = Set.of("java", "opts", "path", "main", "args", "envs");
public static final Set<String> 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<Boolean, List<File>> dirsAndFiles = StreamSupport.stream(environment.getClasspath().spliterator(), false)
Map<Boolean, List<File>> dirsAndFiles = StreamSupport.stream(coreEnvironment.getClasspath().spliterator(), false)
.collect(Collectors.groupingBy(File::isDirectory));
List<File> 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;
}

View File

@ -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:
-------------------

View File

@ -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 <code>start.jar</code>.
* <pre&gt;
* &lt;Configure id="Server" class="org.eclipse.jetty.server.Server"&gt;
* &lt;New class="org.eclipse.jetty.xml.EnvironmentBuilder"&gt;
* &lt;Arg name="name"&gt;ee10&lt;/Arg&gt;
* &lt;Call name="putId"&gt;
* &lt;Arg&gt;Server&lt;/Arg&gt;
* &lt;Arg&gt;&lt;Ref id="Server"/&gt;&lt;/Arg&gt;
* &lt;/Call&gt;
* &lt;Put name="propertyA"&gt;valueA&lt;/Put&gt;
* &lt;Put name="propertyB"&gt;valueB&lt;/Put&gt;
* &lt;Put name="propertyC"&gt;valueC&lt;/Put&gt;
*
* &lt;Call name="addClassPath"&gt;
* &lt;Arg&gt;
* &lt;Array type="String"&gt;
* &lt;Item&gt;lib/feature/foo.jar&lt;/Item&gt;
* &lt;Item&gt;lib/feature/bar.jar&lt;/Item&gt;
* &lt;/Array&gt;
* &lt;/Arg&gt;
* &lt;/Call&gt;
*
* &lt;Call name="addXml"&gt;
* &lt;Arg&gt;
* &lt;Array type="String"&gt;
* &lt;Item&gt;etc/foo.xml&lt;/Item&gt;
* &lt;Item&gt;etc/bar.jar&lt;/Item&gt;
* &lt;/Array&gt;
* &lt;/Arg&gt;
* &lt;/Call&gt;
*
* &lt;Call id="EnvironmentEE10" name="build"/&gt;
* &lt;/New&gt;
* &lt;Call name="addEnvironment"&gt;
* &lt;Arg&gt;&lt;Ref id="EnvironmentEE10"/&gt;&lt;/Arg&gt;
* &lt;/Call&gt;
* &lt;/Configure&gt;
* </pre>
*
*/
public class EnvironmentBuilder extends AbstractMap<String, String>