use of fish.payara.arquillian.container.payara.process.ConsoleReader in project Payara by payara.
the class PayaraServerControl method executeAdminCommand.
private void executeAdminCommand(String description, String command, List<String> args, ProcessOutputConsumer consumer) throws LifecycleException {
final List<String> cmd = buildCommand(command, args);
if (config.isOutputToConsole()) {
System.out.println(description + " using command: " + cmd.toString());
}
int result;
try (CloseableProcess process = new CloseableProcess(new ProcessBuilder(cmd).redirectErrorStream(true).start());
ConsoleReader consoleReader = new ConsoleReader(process, consumer)) {
new Thread(consoleReader).start();
result = process.waitFor();
} catch (IOException | InterruptedException e) {
logger.log(SEVERE, description + (e instanceof IOException ? " failed." : " interrupted."), e);
throw new LifecycleException("Unable to execute " + cmd.toString(), e);
}
if (result != 0) {
throw new LifecycleException("Unable to execute " + cmd.toString());
}
}
use of fish.payara.arquillian.container.payara.process.ConsoleReader in project Payara by payara.
the class PayaraMicroDeployableContainer method deploy.
@Override
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
if (archive == null) {
throw new IllegalArgumentException("archive must not be null");
}
try {
// The main directory from which we'll conduct our business
Path arquillianMicroDir = Files.createTempDirectory("arquillian-payara-micro");
// Create paths for the directories we'll be using
Path deploymentDir = arquillianMicroDir.resolve("deployments/");
// Create the directories
deploymentDir.toFile().mkdir();
// Create the path for the deployment archive (e.g. the application war or ear)
File deploymentFile = deploymentDir.resolve(archive.getName()).toFile();
// Create the deployment file itself
archive.as(ZipExporter.class).exportTo(deploymentFile);
// Create the list of commands to start Payara Micro
List<String> cmd = new ArrayList<>(asList("java", "-jar", configuration.getMicroJarFile().getAbsolutePath(), "--deploy", deploymentFile.getAbsolutePath()));
// Start at a random port so multiple instances won't run in to eachother all the time
if (configuration.isRandomHttpPort()) {
cmd.addAll(asList("--port", (8080 + new SecureRandom().nextInt(1000)) + ""));
}
// Add --autoBindHttp if it's enabled
if (configuration.isAutoBindHttp()) {
cmd.addAll(asList("--autoBindHttp", "--autoBindRange", "1000"));
}
// Disable clustering if it's not explicitly enabled
if (!configuration.isClusterEnabled()) {
cmd.add("--nocluster");
}
// Enable --showServletMappings if it's supported
if (configuration.getMicroVersion().isMoreRecentThan(new PayaraVersion("5.181-SNAPSHOT"))) {
cmd.add("--showServletMappings");
}
// Start Payara Micro in debug mode if it's been enabled
if (configuration.isDebug()) {
cmd.add(1, "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5006");
}
// Add the extra cmd options to the Payara Micro instance
if (configuration.getCmdOptions() != null) {
int index = 1;
// Split on non-escaped spaces
for (String option : configuration.getCmdOptions().split("(?<!\\\\) ")) {
// Unescape any path spaces before adding the string
cmd.add(index, option.replace("\\ ", " "));
index++;
}
}
// Add the extra micro options to the Payara Micro instance
if (configuration.getExtraMicroOptions() != null) {
// Split on non-escaped spaces
for (String option : configuration.getExtraMicroOptions().split("(?<!\\\\) ")) {
// Unescape any path spaces before adding the string
cmd.add(option.replace("\\ ", " "));
}
}
logger.info("Starting Payara Micro using cmd: " + cmd);
// Allow Ctrl-C to stop the test, then start Payara Micro
registerShutdownHook();
payaraMicroProcess = new ProcessBuilder(cmd).redirectErrorStream(true).start();
// Create an executor for handling the log reading and writing
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
// Create a consumer for reading Payara Micro output
BufferingConsumer consumer = new BufferingConsumer(createProcessOutputConsumer());
ConsoleReader logReader = new ConsoleReader(payaraMicroProcess, consumer);
executor.execute(logReader);
// Check at intervals if Payara Micro has finished starting up or failed to start.
CountDownLatch payaraMicroStarted = new CountDownLatch(1);
executor.scheduleAtFixedRate(() -> {
log = consumer.getBuffer().toString();
// Check for app deployed
Matcher startupMatcher = instanceConfigPattern.matcher(log);
if (startupMatcher.find()) {
payaraMicroStarted.countDown();
}
}, 1500, 200, MILLISECONDS);
int startupTimeoutInSeconds = configuration.isDebug() ? -1 : configuration.getStartupTimeoutInSeconds();
boolean microStarted = false;
// Wait for Payara Micro to start up, or time out after the specified timeout
if (startupTimeoutInSeconds == -1) {
payaraMicroStarted.await();
microStarted = true;
} else {
microStarted = payaraMicroStarted.await(startupTimeoutInSeconds, SECONDS);
}
if (microStarted) {
// Shutdown log reading executor
executor.shutdownNow();
// Create a matcher for the 'Instance Configured' message
Matcher instanceConfigMatcher = instanceConfigPattern.matcher(log);
if (instanceConfigMatcher.find()) {
// Get the host and port that the application started on.
String host = instanceConfigMatcher.group("host").trim();
String[] ports = instanceConfigMatcher.group("ports").trim().split(" ");
int firstPort = Integer.parseInt(ports[0].trim());
logger.info("Payara Micro running on host: " + host + " port: " + firstPort);
HTTPContext httpContext = new HTTPContext(host, firstPort);
// If the instance config is in the new JSON format, parse the JSON object.
if (instanceConfigMatcher.group("jsonFormat") != null) {
processDeploymentAsJson(log.substring(instanceConfigMatcher.start()), httpContext);
} else {
// Otherwise, parse it the old way
processDeploymentOldMethod(log.substring(instanceConfigMatcher.start()), httpContext);
}
ProtocolMetaData protocolMetaData = new ProtocolMetaData();
protocolMetaData.addContext(httpContext);
return protocolMetaData;
}
}
} catch (IOException e) {
// Occurs when there was an error starting the Payara Micro thread or the ConsoleReader thread.
logger.severe("Failed in creating a thread for Payara Micro.\n" + e.getMessage());
Thread.currentThread().interrupt();
return null;
} catch (InterruptedException e) {
// Occurs when the timeout is reached in waiting for Payara Micro to start
logger.severe("Timeout reached waiting for Payara Micro to start.\n" + e.getMessage());
Thread.currentThread().interrupt();
return null;
}
throw new DeploymentException("No applications were found deployed to Payara Micro.");
}
Aggregations