diff --git a/plugin/src/main/java/org/wildfly/plugin/cli/ExecuteCommandsMojo.java b/plugin/src/main/java/org/wildfly/plugin/cli/ExecuteCommandsMojo.java index 392a2434..4034abde 100644 --- a/plugin/src/main/java/org/wildfly/plugin/cli/ExecuteCommandsMojo.java +++ b/plugin/src/main/java/org/wildfly/plugin/cli/ExecuteCommandsMojo.java @@ -23,6 +23,7 @@ package org.wildfly.plugin.cli; import java.io.File; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -35,6 +36,7 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -42,12 +44,15 @@ import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.repository.RemoteRepository; +import org.jboss.as.controller.client.ModelControllerClient; +import org.jboss.as.controller.client.helpers.ClientConstants; import org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager; import org.jboss.galleon.universe.maven.repo.MavenRepoManager; import org.wildfly.plugin.common.AbstractServerConnection; import org.wildfly.plugin.common.PropertyNames; import org.wildfly.plugin.common.Utils; import org.wildfly.plugin.core.MavenRepositoriesEnricher; +import org.wildfly.plugin.core.ServerHelper; /** * Execute commands to the running WildFly Application Server. @@ -255,6 +260,25 @@ public void execute() throws MojoExecutionException, MojoFailureException { cmdConfigBuilder.setJBossHome(getInstallation(buildDir.toPath().resolve(Utils.WILDFLY_DEFAULT_DIR))); } commandExecutor.execute(cmdConfigBuilder.build(), mavenRepoManager); + // Check the server state if we're not in offline mode + if (!offline) { + try (ModelControllerClient client = createClient()) { + final String serverState = ServerHelper.serverState(client); + if (!ClientConstants.CONTROLLER_PROCESS_STATE_RUNNING.equals(serverState)) { + getLog().warn(String.format( + "The server may be in an unexpected state for further interaction. The current state is %s", + serverState)); + } + } catch (IOException e) { + final Log log = getLog(); + log.warn(String.format( + "Failed to determine the server-state. The server may be in an unexpected state. Failure: %s", + e.getMessage())); + if (log.isDebugEnabled()) { + log.debug(e); + } + } + } } /** diff --git a/plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java b/plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java index 272308f3..8932e2bb 100644 --- a/plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java +++ b/plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java @@ -430,7 +430,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { .addCommands(commands) .addScripts(scripts) .setStdout("none") - .setAutoReload(true) + .setAutoReload(false) .setTimeout(timeout); if (context == null) { builder.setOffline(false) @@ -440,6 +440,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { .setFork(true); } commandExecutor.execute(builder.build(), mavenRepoManager); + // Check the server state. We may need to restart the process, assuming we control the context + if (context != null) { + context = actOnServerState(client, context); + } + final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client); final Deployment deployment = getDeploymentContent(); try { diff --git a/plugin/src/main/java/org/wildfly/plugin/server/AbstractServerStartMojo.java b/plugin/src/main/java/org/wildfly/plugin/server/AbstractServerStartMojo.java index 6e3c9f37..181bd7dc 100644 --- a/plugin/src/main/java/org/wildfly/plugin/server/AbstractServerStartMojo.java +++ b/plugin/src/main/java/org/wildfly/plugin/server/AbstractServerStartMojo.java @@ -26,6 +26,7 @@ import java.nio.file.Paths; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.maven.execution.MavenSession; @@ -39,6 +40,8 @@ import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.repository.RemoteRepository; import org.jboss.as.controller.client.ModelControllerClient; +import org.jboss.as.controller.client.helpers.ClientConstants; +import org.jboss.as.controller.client.helpers.Operations; import org.jboss.as.controller.client.helpers.domain.DomainClient; import org.jboss.galleon.ProvisioningException; import org.jboss.galleon.maven.plugin.util.MavenArtifactRepositoryManager; @@ -435,6 +438,44 @@ protected Path provisionIfRequired(final Path installDir) throws MojoFailureExce } } + /** + * Checks the current state of the server. If the server is in a state of + * {@link ClientConstants#CONTROLLER_PROCESS_STATE_RESTART_REQUIRED}, the process is restarted and a new + * {@link ServerContext} is returned. If the server is in a stat of + * {@link ClientConstants#CONTROLLER_PROCESS_STATE_RELOAD_REQUIRED}, the server will be reloaded and wait until + * the server is running. If the server is in any other state, other than + * {@link ClientConstants#CONTROLLER_PROCESS_STATE_RUNNING}, a warning message is logged to let the user know + * the state is unknown. + * + * @param client the client used to communicate with the server + * @param context the current server context + * @return a new context if a restart was required, otherwise the same context + * @throws IOException if an error occurs communicating with the server + * @throws MojoExecutionException if a failure occurs checking the state or reloading the server + * @throws MojoFailureException if a failure occurs checking the state or reloading the server + */ + protected ServerContext actOnServerState(final ModelControllerClient client, final ServerContext context) + throws IOException, MojoExecutionException, MojoFailureException { + final String serverState = ServerHelper.serverState(client); + if (ClientConstants.CONTROLLER_PROCESS_STATE_RESTART_REQUIRED.equals(serverState)) { + // Shutdown the server + ServerHelper.shutdownStandalone(client, timeout); + // Restart the server process + return startServer(ServerType.STANDALONE); + } else if (ClientConstants.CONTROLLER_PROCESS_STATE_RELOAD_REQUIRED.equals(serverState)) { + ServerHelper.executeReload(client, Operations.createOperation("reload")); + try { + ServerHelper.waitForStandalone(context.process(), client, timeout); + } catch (InterruptedException | TimeoutException e) { + throw new MojoExecutionException("Failed to wait for standalone server after a reload.", e); + } + } else if (!ClientConstants.CONTROLLER_PROCESS_STATE_RUNNING.equals(serverState)) { + getLog().warn(String.format( + "The server may be in an unexpected state for further interaction. The current state is %s", serverState)); + } + return context; + } + private void addUsers(final Path wildflyHome, final Path javaHome) throws IOException { if (addUser != null && addUser.hasUsers()) { getLog().info("Adding users: " + addUser); diff --git a/plugin/src/main/java/org/wildfly/plugin/server/RunMojo.java b/plugin/src/main/java/org/wildfly/plugin/server/RunMojo.java index 75eed5e1..6ec041fc 100644 --- a/plugin/src/main/java/org/wildfly/plugin/server/RunMojo.java +++ b/plugin/src/main/java/org/wildfly/plugin/server/RunMojo.java @@ -132,12 +132,14 @@ public void execute() throws MojoExecutionException, MojoFailureException { .addCommands(commands) .addScripts(scripts) .setJBossHome(context.jbossHome()) - .setAutoReload(true) + .setAutoReload(false) .setFork(true) .setStdout("none") .setTimeout(timeout) .build(); commandExecutor.execute(cmdConfig, mavenRepoManager); + + process = actOnServerState(client, context).process(); // Create the deployment and deploy final Deployment deployment = Deployment.of(deploymentContent) .setName(name)