Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Support sysprop options in swarmtool [SWARM-5]
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias committed Dec 22, 2015
1 parent 81d7d4d commit 8acc52c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 22 deletions.
27 changes: 18 additions & 9 deletions swarmtool/src/main/java/org/wildfly/swarm/swarmtool/Build.java
Expand Up @@ -16,13 +16,6 @@

package org.wildfly.swarm.swarmtool;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.jboss.shrinkwrap.resolver.api.maven.ConfigurableMavenResolverSystem;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.repository.MavenChecksumPolicy;
Expand All @@ -32,8 +25,16 @@
import org.wildfly.swarm.arquillian.adapter.ShrinkwrapArtifactResolvingHelper;
import org.wildfly.swarm.fractionlist.FractionDescriptor;
import org.wildfly.swarm.fractionlist.FractionList;
import org.wildfly.swarm.tools.PackageAnalyzer;
import org.wildfly.swarm.tools.BuildTool;
import org.wildfly.swarm.tools.PackageAnalyzer;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

public class Build {
final static Set<String> REQUIRED_FRACTIONS = new HashSet<String>() {{
Expand Down Expand Up @@ -63,6 +64,12 @@ public Build contextPath(final String path) {
return this;
}

public Build properties(final Properties props) {
this.properties = props;

return this;
}

public Build addSwarmDependencies(final List<String> deps) {
this.swarmDependencies.addAll(deps);

Expand Down Expand Up @@ -132,7 +139,8 @@ public File run() throws Exception {
.artifactResolvingHelper(new ShrinkwrapArtifactResolvingHelper(resolver))
.projectArtifact("", baseName, "", type, this.source)
.resolveTransitiveDependencies(true)
.contextPath(this.contextPath);
.contextPath(this.contextPath)
.properties(this.properties);

if (this.autoDetectFractions) {
this.swarmDependencies.addAll(new PackageAnalyzer(this.source).detectNeededFractions());
Expand Down Expand Up @@ -164,6 +172,7 @@ public File run() throws Exception {
private String contextPath;
private String name;
private String version;
private Properties properties;
private boolean autoDetectFractions = true;

}
29 changes: 27 additions & 2 deletions swarmtool/src/main/java/org/wildfly/swarm/swarmtool/Main.java
Expand Up @@ -22,6 +22,7 @@
import joptsimple.OptionSpec;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
Expand Down Expand Up @@ -77,6 +78,19 @@ protected static File generateSwarmJar(final String [] args) throws Exception {
exit("File " + source.getAbsolutePath() + " does not exist.");
}

final Properties properties = new Properties();
if (foundOptions.has(SYSPROPS_FILE_OPT)) {
try (InputStream in = new FileInputStream(foundOptions.valueOf(SYSPROPS_FILE_OPT))) {
properties.load(in);
}
}

foundOptions.valuesOf(SYSPROPS_OPT)
.forEach(prop -> {
final String[] parts = prop.split("=");
properties.put(parts[0], parts[1]);
});

return new Build()
.source(source)
.swarmVersion(VERSION)
Expand All @@ -85,6 +99,7 @@ protected static File generateSwarmJar(final String [] args) throws Exception {
.name(foundOptions.valueOf(NAME_OPT))
.autoDetectFractions(!foundOptions.has(DISABLE_AUTO_DETECT))
.contextPath(foundOptions.valueOf(CONTEXT_PATH_OPT))
.properties(properties)
.run();
}

Expand All @@ -93,8 +108,6 @@ private static void exit(String message) {
}

private static void exit(String message, int code) {
System.err.println(message);

throw new ExitException(code, message);
}

Expand Down Expand Up @@ -142,6 +155,18 @@ private static void exit(String message, int code) {
.defaultsTo("/")
.describedAs("context");

private static final OptionSpec<String> SYSPROPS_OPT =
OPT_PARSER.accepts("D", "system property (overrides entry in --property-file)")
.withRequiredArg()
.ofType(String.class)
.describedAs("key=value");

private static final OptionSpec<File> SYSPROPS_FILE_OPT =
OPT_PARSER.accepts("property-file", "system properties")
.withRequiredArg()
.ofType(File.class)
.describedAs("system properties file");

private static final String VERSION;

static {
Expand Down
58 changes: 47 additions & 11 deletions swarmtool/src/test/java/org/wildfly/swarm/swarmtool/MainTest.java
Expand Up @@ -16,17 +16,17 @@
package org.wildfly.swarm.swarmtool;


import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;
Expand All @@ -48,7 +48,8 @@ Result runTool(String... args) throws Exception {
final String[] fullArgs = Arrays.copyOf(args, args.length + 1);
fullArgs[fullArgs.length - 1] = "--output-dir=target/test-output";

result.jarFile = Main.generateSwarmJar(fullArgs);
result.jarFile(Main.generateSwarmJar(fullArgs));

} catch (Main.ExitException e) {
result.exitStatus = e.status;
result.exitMessage = e.getMessage();
Expand All @@ -62,25 +63,60 @@ Result runTool(String... args) throws Exception {
return result;
}

String getResourcePath(String name) throws URISyntaxException {
return Paths.get(getClass().getClassLoader()
.getResource(name)
.toURI())
.toString();
}

Properties swarmProperties(Result result) throws IOException {
final Properties props = new Properties();
try (InputStream in = result.archive
.get("META-INF/wildfly-swarm.properties")
.getAsset()
.openStream()) {
props.load(in);
}

return props;
}

@Test
public void setContextPath() throws Exception {
final Path war = Paths.get(getClass().getClassLoader().getResource("simple-servlet.war").toURI());
final Result result = runTool(war.toAbsolutePath().toString(), "--context-path=/path");
final Result result = runTool(getResourcePath("simple-servlet.war"),
"--context-path=/path");

assertThat(result.exitStatus).isEqualTo(0);
assertThat(swarmProperties(result)
.get("wildfly.swarm.context.path"))
.isEqualTo("/path");
}

final WebArchive archive = ShrinkWrap.createFromZipFile(WebArchive.class, result.jarFile);
final Properties props = new Properties();
try (InputStream in = archive.get("META-INF/wildfly-swarm.properties").getAsset().openStream()) {
props.load(in);
}
@Test
public void setProperties() throws Exception {
final Result result = runTool(getResourcePath("simple-servlet.war"),
"-Dfoo=bar", "-Dham=biscuit",
"--property-file=" + getResourcePath("test.properties"));

assertThat(props.get("wildfly.swarm.context.path")).isEqualTo("/path");
assertThat(result.exitStatus).isEqualTo(0);

final Properties props = swarmProperties(result);
assertThat(props.get("cheese")).isEqualTo("biscuit");
assertThat(props.get("foo")).isEqualTo("bar");
// -D overrides properties file
assertThat(props.get("ham")).isEqualTo("biscuit");
}

class Result {
public int exitStatus = 0;
public String exitMessage = null;
public File jarFile = null;
public WebArchive archive = null;

public void jarFile(File f) {
this.jarFile = f;
this.archive = ShrinkWrap.createFromZipFile(WebArchive.class, f);
}
}
}
2 changes: 2 additions & 0 deletions swarmtool/src/test/resources/test.properties
@@ -0,0 +1,2 @@
cheese=biscuit
ham=sandwich

0 comments on commit 8acc52c

Please sign in to comment.