Search in sources :

Example 1 with CommandBuilder

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);
    }
}
Also used : Path(java.nio.file.Path) BootableJarCommandBuilder(org.wildfly.core.launcher.BootableJarCommandBuilder) SocketException(java.net.SocketException) CancellationException(java.util.concurrent.CancellationException) Assert.checkNotNullParamWithNullPointerException(org.wildfly.common.Assert.checkNotNullParamWithNullPointerException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StandaloneCommandBuilder(org.wildfly.core.launcher.StandaloneCommandBuilder) BootableJarCommandBuilder(org.wildfly.core.launcher.BootableJarCommandBuilder) StandaloneCommandBuilder(org.wildfly.core.launcher.StandaloneCommandBuilder) CommandBuilder(org.wildfly.core.launcher.CommandBuilder)

Example 2 with CommandBuilder

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);
    }
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) Log(org.apache.maven.plugin.logging.Log) MavenArtifactRepositoryManager(org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager) Launcher(org.wildfly.core.launcher.Launcher) StandardOutput(org.wildfly.plugin.common.StandardOutput) DomainCommandBuilder(org.wildfly.core.launcher.DomainCommandBuilder) StandaloneCommandBuilder(org.wildfly.core.launcher.StandaloneCommandBuilder) CommandBuilder(org.wildfly.core.launcher.CommandBuilder) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ProvisioningException(org.jboss.galleon.ProvisioningException)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 CommandBuilder (org.wildfly.core.launcher.CommandBuilder)2 StandaloneCommandBuilder (org.wildfly.core.launcher.StandaloneCommandBuilder)2 SocketException (java.net.SocketException)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Log (org.apache.maven.plugin.logging.Log)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 ProvisioningException (org.jboss.galleon.ProvisioningException)1 MavenArtifactRepositoryManager (org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager)1 Assert.checkNotNullParamWithNullPointerException (org.wildfly.common.Assert.checkNotNullParamWithNullPointerException)1 BootableJarCommandBuilder (org.wildfly.core.launcher.BootableJarCommandBuilder)1 DomainCommandBuilder (org.wildfly.core.launcher.DomainCommandBuilder)1 Launcher (org.wildfly.core.launcher.Launcher)1 StandardOutput (org.wildfly.plugin.common.StandardOutput)1