/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.example.runner;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.dmr.ModelNode;
import org.wildfly.core.launcher.Launcher;
import org.wildfly.core.launcher.ProcessHelper;
import org.wildfly.core.launcher.StandaloneCommandBuilder;
/**
* @author James R. Perkins
*/
public class Main {
private static final String DOWNLOAD_URL = "http://download.jboss.org/wildfly/8.1.0.Final/wildfly-8.1.0.Final.zip";
private static final Path DIST_PATH = Paths.get(System.getProperty("user.home"),
".m2", "repository", "org", "wildfly", "wildfly-dist", "8.1.0.Final", "wildfly-dist-8.1.0.Final.zip");
private static final Path TEMP = Paths.get(System.getProperty("java.io.tmpdir"), "wildfly");
private static final Path WILDFLY_HOME = TEMP.resolve("wildfly-8.1.0.Final");
public static void main(final String[] args) throws Exception {
long timeout = 5L;
int port = 9990;
// Parse the parameters
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; i++) {
final String arg = args[i];
if ("-t".equals(arg)) {
timeout = Long.parseLong(args[++i]);
}
if ("-p".equals(arg)) {
port = Integer.parseInt(args[++i]);
}
}
}
final Path zipDistPath;
// First just check the ~/.m2 directory to see if we already have a dist
if (Files.exists(DIST_PATH)) {
zipDistPath = DIST_PATH;
} else {
final Path toFile = TEMP.resolve("wildfly-dist-8.1.0.Final.zip");
if (Files.notExists(toFile)) {
// Download WildFly
final HttpURLConnection connection = (HttpURLConnection) new URL(DOWNLOAD_URL).openConnection();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.printf("Error: %d - %s%n", connection.getResponseCode(), connection.getResponseMessage());
System.exit(1);
}
try {
System.out.printf("Downloading from %s%n", DOWNLOAD_URL);
Files.createDirectories(toFile.getParent());
Files.copy(connection.getInputStream(), toFile);
zipDistPath = toFile;
} finally {
connection.disconnect();
}
} else {
zipDistPath = toFile;
}
}
unzip(zipDistPath);
// Create the WildFly home
final StandaloneCommandBuilder commandBuilder = StandaloneCommandBuilder.of(WILDFLY_HOME)
.setBindAddressHint("0.0.0.0")
.setBindAddressHint("management", "0.0.0.0")
.addServerArgument("-Djboss.management.http.port=" + port);
final Launcher launcher = Launcher.of(commandBuilder).inherit();
final Process process = launcher.launch();
ProcessHelper.addShutdownHook(process);
TimeUnit.SECONDS.sleep(timeout);
try (final ModelControllerClient client = ModelControllerClient.Factory.create("http-remoting", InetAddress.getLocalHost(), port)) {
final ModelNode op = Operations.createReadResourceOperation(new ModelNode().addEmptyList().add("subsystem", "security"));
op.get("recursive").set(true);
final ModelNode result = client.execute(op);
if (Operations.isSuccessfulOutcome(result)) {
System.out.println(Operations.readResult(result));
} else {
System.out.println(Operations.getFailureDescription(result));
}
}
ProcessHelper.destroyProcess(process);
TimeUnit.SECONDS.sleep(timeout);
}
public static void unzip(final Path zipFile) throws IOException {
if (Files.exists(WILDFLY_HOME)) {
deleteDir(WILDFLY_HOME);
}
Files.createDirectories(TEMP);
final ZipFile file = new ZipFile(zipFile.toFile());
try (final ZipInputStream zis = new ZipInputStream(Files.newInputStream(zipFile))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
final Path target = TEMP.resolve(entry.getName());
if (entry.isDirectory()) {
Files.createDirectories(target);
} else {
final Path parent = target.getParent();
Files.createDirectories(parent);
Files.copy(zis, target);
}
}
}
}
static void deleteDir(final Path dir) throws IOException {
Files.walkFileTree(dir, new SimpleFileVisitor() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}