Skip to content

Commit

Permalink
Merge pull request #513 from jamezp/plugin-tools-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jamezp committed Apr 12, 2024
2 parents 11bc5a7 + 31ccc38 commit 285bdbd
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 93 deletions.
17 changes: 13 additions & 4 deletions plugin/src/main/java/org/wildfly/plugin/cli/CommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.inject.Named;
import javax.inject.Singleton;
Expand All @@ -23,7 +26,7 @@
import org.wildfly.core.launcher.CliCommandBuilder;
import org.wildfly.plugin.common.MavenModelControllerClientConfiguration;
import org.wildfly.plugin.common.StandardOutput;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* A command executor for executing CLI commands.
Expand All @@ -48,7 +51,7 @@ public void execute(final CommandConfiguration config, MavenRepoManager artifact
throws MojoFailureException, MojoExecutionException {
if (config.isOffline()) {
// The jbossHome is required for offline CLI
if (!ServerHelper.isValidHomeDirectory(config.getJBossHome())) {
if (!ServerManager.isValidHomeDirectory(config.getJBossHome())) {
throw new MojoFailureException("Invalid JBoss Home directory is not valid: " + config.getJBossHome());
}
executeInNewProcess(config);
Expand All @@ -65,7 +68,13 @@ public void execute(final CommandConfiguration config, MavenRepoManager artifact
}
if (config.isAutoReload()) {
// Reload the server if required
ServerHelper.reloadIfRequired(config.getClient(), config.getTimeout());
try {
ServerManager.builder().client(config.getClient()).build().get(config.getTimeout(), TimeUnit.SECONDS)
.reloadIfRequired(config.getTimeout(),
TimeUnit.SECONDS);
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
throw new MojoExecutionException("Failed to reload server", e);
}
}
}

Expand All @@ -89,7 +98,7 @@ protected int executeInNewProcess(final CommandConfiguration config, final Path
private void executeInProcess(final CommandConfiguration config, MavenRepoManager artifactResolver) throws Exception {
// The jbossHome is not required, but if defined should be valid
final Path jbossHome = config.getJBossHome();
if (jbossHome != null && !ServerHelper.isValidHomeDirectory(jbossHome)) {
if (jbossHome != null && !ServerManager.isValidHomeDirectory(jbossHome)) {
throw new MojoFailureException("Invalid JBoss Home directory is not valid: " + jbossHome);
}
final Properties currentSystemProperties = System.getProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.inject.Inject;

Expand All @@ -35,7 +38,7 @@
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.common.Utils;
import org.wildfly.plugin.core.MavenRepositoriesEnricher;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* Execute commands to the running WildFly Application Server.
Expand Down Expand Up @@ -246,13 +249,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// Check the server state if we're not in offline mode
if (!offline) {
try (ModelControllerClient client = createClient()) {
final String serverState = ServerHelper.serverState(client);
final String serverState = ServerManager.builder().client(client).build().get(timeout, TimeUnit.SECONDS)
.serverState();
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) {
} catch (IOException | InterruptedException | ExecutionException | TimeoutException 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.core.launcher.CliCommandBuilder;
import org.wildfly.plugin.common.StandardOutput;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* A command executor for executing offline CLI commands.
Expand All @@ -39,7 +39,7 @@ public class OfflineCommandExecutor extends AbstractCommandExecutor<BaseCommandC
@Override
public void execute(final BaseCommandConfiguration config, MavenRepoManager artifactResolver)
throws MojoFailureException, MojoExecutionException {
if (!ServerHelper.isValidHomeDirectory(config.getJBossHome())) {
if (!ServerManager.isValidHomeDirectory(config.getJBossHome())) {
throw new MojoFailureException("Invalid JBoss Home directory is not valid: " + config.getJBossHome());
}
executeInNewProcess(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.wildfly.plugin.common.AbstractServerConnection;
import org.wildfly.plugin.common.MavenModelControllerClientConfiguration;
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.tools.ContainerDescription;
import org.wildfly.plugin.tools.Deployment;
import org.wildfly.plugin.tools.DeploymentManager;
import org.wildfly.plugin.tools.DeploymentResult;
import org.wildfly.plugin.tools.ServerHelper;

/**
* The default implementation for executing build plans on the server.
Expand Down Expand Up @@ -87,7 +87,7 @@ public final void execute() throws MojoExecutionException, MojoFailureException
try (
ModelControllerClient client = createClient();
MavenModelControllerClientConfiguration configuration = getClientConfiguration()) {
final boolean isDomain = ServerHelper.getContainerDescription(client).isDomain();
final boolean isDomain = ContainerDescription.lookup(client).isDomain();
validate(isDomain);
// Deploy the deployment
getLog().debug("Executing deployment");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import org.wildfly.plugin.common.MavenModelControllerClientConfiguration;
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.common.ServerOperations;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.ContainerDescription;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* Adds a resource
Expand Down Expand Up @@ -94,7 +95,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}
// Validate the home directory if required
if (jbossHome != null && !ServerHelper.isValidHomeDirectory(jbossHome)) {
if (jbossHome != null && !ServerManager.isValidHomeDirectory(jbossHome)) {
throw new MojoFailureException("Invalid JBoss Home directory is not valid: " + jbossHome);
}
try (
Expand All @@ -113,7 +114,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
private void processResources(final ModelControllerClient client,
final MavenModelControllerClientConfiguration configuration, final Resource... resources) throws IOException {
final Collection<String> profiles = getProfiles();
final boolean isDomain = ServerHelper.getContainerDescription(client).isDomain();
final boolean isDomain = ContainerDescription.lookup(client).isDomain();
for (Resource resource : resources) {
if (isDomain && profiles.isEmpty()) {
throw new IllegalStateException("Cannot add resources when no profiles were defined.");
Expand Down
16 changes: 9 additions & 7 deletions plugin/src/main/java/org/wildfly/plugin/dev/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@
import org.wildfly.plugin.tools.DeploymentResult;
import org.wildfly.plugin.tools.GalleonUtils;
import org.wildfly.plugin.tools.PluginProgressTracker;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.UndeployDescription;
import org.wildfly.plugin.tools.VersionComparator;
import org.wildfly.plugin.tools.server.ServerManager;
import org.wildfly.plugin.tools.server.StandaloneManager;

/**
* Starts a standalone instance of WildFly and deploys the application to the server. The deployment type must be a WAR.
Expand Down Expand Up @@ -450,11 +451,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
registerDir(watcher, resolveWebAppSourceDir(), new WebAppResourceHandler(webExtensions));
try (ModelControllerClient client = createClient()) {
if (!ServerHelper.isStandaloneRunning(client)) {
final StandaloneManager serverManager = ServerManager.builder().client(client).standalone();
if (!serverManager.isRunning()) {
throw new MojoExecutionException("No standalone server appears to be running.");
}
if (remote) {
final ContainerDescription description = ServerHelper.getContainerDescription(client);
final ContainerDescription description = ContainerDescription.lookup(client);
getLog().info(String.format("Deploying to remote %s container.", description));
}
// Execute commands before the deployment is done
Expand All @@ -478,20 +480,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {
context = actOnServerState(client, context);
}

final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final DeploymentManager deploymentManager = serverManager.deploymentManager();
final Deployment deployment = getDeploymentContent();
try {
final DeploymentResult result = deploymentManager.forceDeploy(deployment);
if (!result.successful()) {
throw new MojoExecutionException("Failed to deploy content: " + result.getFailureMessage());
}
if (remote) {
getLog().info(String.format("Deployed %s", deployment.toString()));
getLog().info(String.format("Deployed %s", deployment));
}
watch(watcher, deploymentManager, deployment);
} finally {
deploymentManager.undeploy(UndeployDescription.of(deployment));
ServerHelper.shutdownStandalone(client);
serverManager.shutdown();
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -594,7 +596,7 @@ boolean reprovisionAndStart()
}
debug("Changes in layers detected, must re-provision the server");
try (ModelControllerClient client = createClient()) {
ServerHelper.shutdownStandalone(client);
ServerManager.builder().client(client).standalone().shutdown();
debug("Deleting existing installation " + installDir);
IoUtils.recursiveDelete(installDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.common.Utils;
import org.wildfly.plugin.tools.GalleonUtils;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
Expand Down Expand Up @@ -95,7 +95,7 @@ public abstract class AbstractServerStartMojo extends AbstractStartMojo {
protected Path getServerHome() throws MojoExecutionException, MojoFailureException {
// Validate the environment
final Path jbossHome = provisionIfRequired(targetDir.toPath().resolve(provisioningDir));
if (!ServerHelper.isValidHomeDirectory(jbossHome)) {
if (!ServerManager.isValidHomeDirectory(jbossHome)) {
throw new MojoExecutionException(String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome));
}
return jbossHome;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.maven.execution.MavenSession;
Expand All @@ -25,7 +25,6 @@
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.maven.plugin.util.MavenArtifactRepositoryManager;
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.wildfly.core.launcher.CommandBuilder;
Expand All @@ -34,7 +33,7 @@
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.common.StandardOutput;
import org.wildfly.plugin.core.MavenRepositoriesEnricher;
import org.wildfly.plugin.tools.ServerHelper;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
Expand Down Expand Up @@ -133,6 +132,7 @@ public abstract class AbstractStartMojo extends AbstractServerConnection {

private final AtomicBoolean initialized = new AtomicBoolean();

protected ServerManager serverManager;
protected MavenRepoManager mavenRepoManager;

protected void init() throws MojoExecutionException {
Expand Down Expand Up @@ -161,7 +161,7 @@ protected ServerContext startServer(final ServerType serverType) throws MojoExec
// Create the server and close the client after the start. The process will continue running even after
// the maven process may have been finished
try (ModelControllerClient client = createClient()) {
if (ServerHelper.isStandaloneRunning(client) || ServerHelper.isDomainRunning(client)) {
if (ServerManager.isRunning(client)) {
throw new MojoExecutionException(String.format("%s server is already running?", serverType));
}
final CommandBuilder commandBuilder = createCommandBuilder(server);
Expand All @@ -174,13 +174,16 @@ protected ServerContext startServer(final ServerType serverType) throws MojoExec
out.getRedirect().ifPresent(launcher::redirectOutput);

final Process process = launcher.launch();
if (serverType == ServerType.DOMAIN) {
serverManager = ServerManager.builder().process(process).client(client).domain();
} else {
serverManager = ServerManager.builder().process(process).client(client).standalone();
}
// 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 (!serverManager.waitFor(startupTimeout, TimeUnit.SECONDS)) {
throw new MojoExecutionException(String.format("Server failed to start in %s seconds.", startupTimeout));
}
if (!process.isAlive()) {
throw new MojoExecutionException("The process has been terminated before the start goal has completed.");
Expand Down Expand Up @@ -245,17 +248,19 @@ public void setJavaOpts(final String value) {
*/
protected ServerContext actOnServerState(final ModelControllerClient client, final ServerContext context)
throws IOException, MojoExecutionException, MojoFailureException {
final String serverState = ServerHelper.serverState(client);
final String serverState = serverManager.serverState();
if (ClientConstants.CONTROLLER_PROCESS_STATE_RESTART_REQUIRED.equals(serverState)) {
// Shutdown the server
ServerHelper.shutdownStandalone(client, timeout);
serverManager.shutdown(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"));
serverManager.executeReload(Operations.createOperation("reload"));
try {
ServerHelper.waitForStandalone(context.process(), client, timeout);
} catch (InterruptedException | TimeoutException e) {
if (!serverManager.waitFor(startupTimeout, TimeUnit.SECONDS)) {
throw new MojoExecutionException(String.format("Server failed to start in %s seconds.", startupTimeout));
}
} catch (InterruptedException e) {
throw new MojoExecutionException("Failed to wait for standalone server after a reload.", e);
}
} else if (!ClientConstants.CONTROLLER_PROCESS_STATE_RUNNING.equals(serverState)) {
Expand Down
Loading

0 comments on commit 285bdbd

Please sign in to comment.