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

Commit

Permalink
Throw interrupted exceptions from stop().
Browse files Browse the repository at this point in the history
Make the arquillian container use the SwarmExector/SwarmProcess bits.
  • Loading branch information
bobmcwhirter committed Nov 20, 2015
1 parent 9bf6f0e commit cc3df2b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 70 deletions.
Expand Up @@ -51,6 +51,8 @@
import org.jboss.shrinkwrap.resolver.api.maven.repository.MavenRemoteRepository;
import org.jboss.shrinkwrap.resolver.api.maven.repository.MavenUpdatePolicy;
import org.wildfly.swarm.tools.BuildTool;
import org.wildfly.swarm.tools.exec.SwarmExecutor;
import org.wildfly.swarm.tools.exec.SwarmProcess;

/**
* @author Bob McWhirter
Expand All @@ -61,10 +63,8 @@ public class WildFlySwarmContainer implements DeployableContainer<WildFlySwarmCo

private List<String> requestedMavenArtifacts;

private Process process;
private SwarmProcess process;

private IOBridge stdout;
private IOBridge stderr;

@Override
public Class<WildFlySwarmContainerConfiguration> getConfigurationClass() {
Expand Down Expand Up @@ -165,12 +165,10 @@ public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
}
}

try {
SwarmExecutor executor = new SwarmExecutor();
executor.withDefaultSystemProperties();

Path java = findJava();
if (java == null) {
throw new DeploymentException("Unable to locate `java` binary");
}
try {

Archive<?> wrapped = tool.build();

Expand All @@ -186,52 +184,29 @@ public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
wrapped.as(ZipExporter.class).exportTo(executable, true);
executable.deleteOnExit();

List<String> cli = new ArrayList<>();

cli.add(java.toString());

cli.add("-Djava.net.preferIPv4Stack=true");

Enumeration<?> names = System.getProperties().propertyNames();

while (names.hasMoreElements()) {
String key = (String) names.nextElement();
if (key.startsWith("jboss") || key.startsWith("swarm") || key.startsWith("wildfly") || key.startsWith( "maven" )) {
String value = System.getProperty(key);
cli.add("-D" + key + "=" + value);
}
}

cli.add("-jar");
cli.add(executable.getAbsolutePath());
executor.withProperty( "java.net.preferIPv4Stack", "true" );
executor.withExecutableJar( executable.toPath() );


File workingDirectory = Files.createTempDirectory("arquillian").toFile();
workingDirectory.deleteOnExit();
this.process = Runtime.getRuntime().exec(cli.toArray(new String[cli.size()]), new String[]{}, workingDirectory);

CountDownLatch latch = new CountDownLatch(1);
this.stdout = new IOBridge("out", latch, process.getInputStream(), System.out);
this.stderr = new IOBridge("err", latch, process.getErrorStream(), System.err);
executor.withWorkingDirectory( workingDirectory.toPath() );

this.process = executor.execute();
this.process.getOutputStream().close();

new Thread(stdout).start();
new Thread(stderr).start();

ProtocolMetaData metaData = new ProtocolMetaData();
HTTPContext context = new HTTPContext("localhost", 8080);
context.add(new Servlet(ServletMethodExecutor.ARQUILLIAN_SERVLET_NAME, "/"));
metaData.addContext(context);
latch.await(2, TimeUnit.MINUTES);

this.process.awaitDeploy( 2, TimeUnit.MINUTES );
if ( ! this.process.isAlive() ) {
throw new DeploymentException( "Process failed to start" );
}
if ( this.stdout.getError() != null ) {
throw new DeploymentException( "Error starting process", this.stdout.getError() );
}
if ( this.stderr.getError() != null ) {
throw new DeploymentException( "Error starting process", this.stderr.getError() );
if ( this.process.getError() != null ) {
throw new DeploymentException( "Error starting process", this.process.getError() );
}
return metaData;
} catch (Exception e) {
Expand All @@ -242,24 +217,9 @@ public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
@Override
public void undeploy(Archive<?> archive) throws DeploymentException {
try {
try {
this.stdout.close();
} catch (IOException e) {
// ignore
}
try {
this.stderr.close();
} catch (IOException e) {
// ignore
}

this.process.destroy();
this.process.waitFor(10, TimeUnit.SECONDS);
this.process.destroyForcibly();
this.process.waitFor(10, TimeUnit.SECONDS);

this.process.stop();
} catch (InterruptedException e) {
e.printStackTrace();
throw new DeploymentException( "Unable to stop process", e );
}
}

Expand Down
Expand Up @@ -33,7 +33,11 @@ public class StopMojo extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
SwarmProcess process = (SwarmProcess) getPluginContext().get("swarm-process");
if(process != null) {
process.stop( 10, TimeUnit.SECONDS );
try {
process.stop( 10, TimeUnit.SECONDS );
} catch (InterruptedException e) {
throw new MojoFailureException( "unable to stop process", e);
}
}

}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import java.io.PrintStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -41,9 +42,12 @@ public class SwarmExecutor {

private Executable executable;

private Path workingDirectory;

public SwarmExecutor() {
this.stdout = System.out;
this.stderr = System.err;
this.workingDirectory = Paths.get( System.getProperty( "user.dir" ) );
}

public SwarmExecutor withStdoutFile(Path stdoutFile) {
Expand Down Expand Up @@ -177,6 +181,11 @@ public SwarmExecutor withArgument(String arg) {
return this;
}

public SwarmExecutor withWorkingDirectory(Path workingDirectory) {
this.workingDirectory = workingDirectory;
return this;
}

public SwarmProcess execute() throws IOException {
if (this.executable == null) {
throw new RuntimeException("An executable jar or a main-class must be specified");
Expand All @@ -199,7 +208,7 @@ public SwarmProcess execute() throws IOException {
cli.addAll(this.executable.toArguments());
cli.addAll(this.arguments);

Process process = Runtime.getRuntime().exec(cli.toArray(new String[0]), toStringArray(environment));
Process process = Runtime.getRuntime().exec(cli.toArray(new String[0]), toStringArray(environment), this.workingDirectory.toFile());

return new SwarmProcess(
process,
Expand Down
20 changes: 8 additions & 12 deletions tools/src/main/java/org/wildfly/swarm/tools/exec/SwarmProcess.java
Expand Up @@ -30,10 +30,10 @@ public SwarmProcess(Process process, OutputStream stdout, Path stdoutFile, Outpu
}

public Exception getError() {
if ( this.stdout.getError() != null ) {
if (this.stdout.getError() != null) {
return this.stdout.getError();
}
if ( this.stderr.getError() != null ) {
if (this.stderr.getError() != null) {
return this.stderr.getError();
}
return null;
Expand Down Expand Up @@ -71,18 +71,14 @@ public void awaitDeploy(long timeout, TimeUnit timeUnit) throws InterruptedExcep
this.latch.await(timeout, timeUnit);
}

public int stop() {
return stop( 10, TimeUnit.SECONDS );
public int stop() throws InterruptedException {
return stop(10, TimeUnit.SECONDS);
}

public int stop(long timeout, TimeUnit timeUnit) {
public int stop(long timeout, TimeUnit timeUnit) throws InterruptedException {
this.process.destroy();
try {
if (!this.process.waitFor(timeout, timeUnit)) {
process.destroyForcibly();
}
} catch (InterruptedException e) {
throw new RuntimeException("Unable to destroy process", e);
if (!this.process.waitFor(timeout, timeUnit)) {
process.destroyForcibly();
}
try {
this.stdout.close();
Expand All @@ -96,7 +92,7 @@ public int stop(long timeout, TimeUnit timeUnit) {
// ignore
}

if ( ! process.isAlive() ) {
if (!process.isAlive()) {
return process.exitValue();
}

Expand Down

0 comments on commit cc3df2b

Please sign in to comment.