use of org.wildfly.core.launcher.CommandBuilder in project wildfly-core by wildfly.
the class Server method start.
protected void start(PrintStream out) {
try {
CommandBuilder cbuilder = null;
boolean needNormalModeCheck = false;
if (isBootableJar) {
final Path bootableJarPath = Paths.get(bootableJar);
if (Files.notExists(bootableJarPath) || Files.isDirectory(bootableJarPath)) {
throw new IllegalStateException("Cannot find: " + bootableJar);
}
final BootableJarCommandBuilder commandBuilder = BootableJarCommandBuilder.of(bootableJarPath);
commandBuilder.setInstallDir(Paths.get(installDir));
cbuilder = commandBuilder;
commandBuilder.setJavaHome(legacyJavaHome == null ? javaHome : legacyJavaHome);
if (jvmArgs != null) {
commandBuilder.setJavaOptions(jvmArgs.split("\\s+"));
}
if (Boolean.getBoolean(serverDebug)) {
commandBuilder.setDebug(true, serverDebugPort);
}
// we are testing, of course we want assertions and set-up some other defaults
commandBuilder.addJavaOption("-ea").setBindAddressHint("management", managementAddress);
if (jbossArgs != null) {
String[] args = jbossArgs.split("\\s+");
for (String a : args) {
if (a.startsWith("--cli-script=")) {
needNormalModeCheck = true;
break;
}
}
commandBuilder.addServerArguments(args);
}
commandBuilder.addServerArgument("-D[Standalone]");
} else {
final Path jbossHomeDir = Paths.get(jbossHome);
if (Files.notExists(jbossHomeDir) && !Files.isDirectory(jbossHomeDir)) {
throw new IllegalStateException("Cannot find: " + jbossHomeDir);
}
final StandaloneCommandBuilder commandBuilder = StandaloneCommandBuilder.of(jbossHomeDir);
cbuilder = commandBuilder;
if (modulePath != null && !modulePath.isEmpty()) {
commandBuilder.setModuleDirs(modulePath.split(Pattern.quote(File.pathSeparator)));
}
commandBuilder.setJavaHome(legacyJavaHome == null ? javaHome : legacyJavaHome);
if (jvmArgs != null) {
commandBuilder.setJavaOptions(jvmArgs.split("\\s+"));
}
if (Boolean.getBoolean(serverDebug)) {
commandBuilder.setDebug(true, serverDebugPort);
}
if (this.startMode == StartMode.ADMIN_ONLY) {
commandBuilder.setAdminOnly();
} else if (this.startMode == StartMode.SUSPEND) {
commandBuilder.setStartSuspended();
}
if (readOnly) {
commandBuilder.setServerReadOnlyConfiguration(serverConfig);
} else {
commandBuilder.setServerConfiguration(serverConfig);
}
if (gitRepository != null) {
commandBuilder.setGitRepository(gitRepository, gitBranch, gitAuthConfiguration);
}
commandBuilder.setYamlFiles(yamlFiles);
if (configDir != null) {
commandBuilder.addJavaOption("-Djboss.server.config.dir=" + configDir.toString());
}
// we are testing, of course we want assertions and set-up some other defaults
commandBuilder.addJavaOption("-ea").setBindAddressHint("management", managementAddress);
if (jbossArgs != null) {
commandBuilder.addServerArguments(jbossArgs.split("\\s+"));
}
commandBuilder.addServerArgument("-D[Standalone]");
}
StringBuilder builder = new StringBuilder("Starting container with: ");
for (String arg : cbuilder.build()) {
builder.append(arg).append(" ");
}
log.info(builder.toString());
process = Launcher.of(cbuilder).setRedirectErrorStream(true).launch();
new Thread(new ConsoleConsumer(process.getInputStream(), out)).start();
final Process proc = process;
shutdownThread = ProcessHelper.addShutdownHook(proc);
createClient();
long timeout = startupTimeout * 1000;
boolean serverAvailable = false;
long sleep = 1000;
while (timeout > 0 && !serverAvailable) {
long before = System.currentTimeMillis();
serverAvailable = client.isServerInRunningState();
if (serverAvailable && needNormalModeCheck) {
serverAvailable = client.isServerInNormalMode();
}
timeout -= (System.currentTimeMillis() - before);
if (!serverAvailable) {
if (processHasDied(proc))
break;
Thread.sleep(sleep);
timeout -= sleep;
sleep = Math.max(sleep / 2, 100);
}
}
if (!serverAvailable) {
destroyProcess();
throw new RuntimeException("Managed server was not started within " + startupTimeout + " seconds.");
}
} catch (Exception e) {
safeCloseClient();
throw new RuntimeException("Could not start container", e);
}
}
use of org.wildfly.core.launcher.CommandBuilder in project wildfly-maven-plugin by wildfly.
the class StartMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
final Log log = getLog();
if (skip) {
log.debug("Skipping server start");
return;
}
MavenRepositoriesEnricher.enrich(mavenSession, project, repositories);
mavenRepoManager = new MavenArtifactRepositoryManager(repoSystem, session, repositories);
// Validate the environment
final Path jbossHome = provisionIfRequired(targetDir.toPath().resolve(provisioningDir));
if (!ServerHelper.isValidHomeDirectory(jbossHome)) {
throw new MojoExecutionException(String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome));
}
// Determine how stdout should be consumed
try {
final StandardOutput out = StandardOutput.parse(stdout, true);
// the maven process may have been finished
try (ModelControllerClient client = createClient()) {
if (ServerHelper.isStandaloneRunning(client) || ServerHelper.isDomainRunning(client)) {
throw new MojoExecutionException(String.format("%s server is already running?", serverType));
}
final CommandBuilder commandBuilder = createCommandBuilder(jbossHome);
log.info(String.format("%s server is starting up.", serverType));
final Launcher launcher = Launcher.of(commandBuilder).setRedirectErrorStream(true);
if (env != null) {
launcher.addEnvironmentVariables(env);
}
out.getRedirect().ifPresent(launcher::redirectOutput);
final Process process = launcher.launch();
// Note that if this thread is started and no shutdown goal is executed this stop the stdout and stderr
// from being logged any longer. The user was warned in the documentation.
out.startConsumer(process);
if (serverType == ServerType.DOMAIN) {
ServerHelper.waitForDomain(process, DomainClient.Factory.create(client), startupTimeout);
} else {
ServerHelper.waitForStandalone(process, client, startupTimeout);
}
if (!process.isAlive()) {
throw new MojoExecutionException("The process has been terminated before the start goal has completed.");
}
}
} catch (MojoExecutionException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException("The server failed to start", e);
}
}
Aggregations