Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[23] Deprecate the ServerHelper and move the utilities to new utiliti… #25

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ jobs:
mvn -B validate -Pformat-check -Denforcer.skip=true

build:
name: ${{ matrix.os }}-jdk${{ matrix.java }}-${{ matrix.wildfly-version }}
name: '${{ matrix.os }}-jdk${{ matrix.java }}'
needs: format-check
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ 'ubuntu-latest' , 'windows-latest' ]
os: [ 'ubuntu-latest' , 'windows-latest', 'macos-latest' ]
java: ['11', '17', '21']

steps:
Expand All @@ -49,7 +49,7 @@ jobs:
java-version: ${{ matrix.java }}
cache: 'maven'
distribution: 'temurin'
- name: Build and Test on ${{ matrix.java }} - ${{ matrix.wildfly-version }}
- name: Build and Test on ${{ matrix.os }} - ${{ matrix.java }}
run: mvn clean install '-Dorg.jboss.logmanager.nocolor=true'
- name: Upload surefire logs for failed run
uses: actions/upload-artifact@v4
Expand Down
14 changes: 9 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,28 @@ try (final ModelControllerClient client = ModelControllerClient.Factory.create(I
}
----

== Server Helper
== Server Utilities

You can also use the `org.wildfly.plugin.core.ServerHelper` to interact with a running server.
You can also use the `org.wildfly.plugin.tools.server.DomainManager`, `org.wildfly.plugin.tools.server.ServerManager`
and `org.wildfly.plugin.tools.server.StandaloneManager` utilities to interact with a running server.

[source,java]
----
final Path wildflyHome = Paths.get(System.getProperty("user.home"), "servers", "wildfly-10.0.0.Final");
final Process process = Launcher.of(StandaloneCommandBuilder.of(wildflyHome)).launch();
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
final StandaloneManager serverManager = ServerManager.builder().client(client).process(process).standalone();
// Wait at the maximum 30 seconds for the server to start
ServerHelper.waitForStandalone(client, 30);
if (!serverManager.waitFor(30, TimeUnit.SECONDS)) {
throw new RuntimeException("Server did not start within 30 seconds.");
}
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
final DeploymentManager deploymentManager = serverManager.deploymentManager();
final Deployment deployment = Deployment.of(deploymentPath);
deploymentManager.forceDeploy(deployment).assertSuccess();

// Shutdown the standalone server
ServerHelper.shutdownStandalone(client);
serverManager.shutdown();
} finally {
process.destroy();
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-plugin-tools</artifactId>
<version>1.0.1.Final-SNAPSHOT</version>
<version>1.1.0.Final-SNAPSHOT</version>
<name>WildFly Plugin Tools</name>

<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.jboss.as.controller.client.helpers.ClientConstants.DEPLOYMENT;
import static org.jboss.as.controller.client.helpers.ClientConstants.READ_CHILDREN_NAMES_OPERATION;
import static org.jboss.as.controller.client.helpers.ClientConstants.SERVER_GROUP;
import static org.wildfly.plugin.tools.DeploymentOperations.createAddress;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -231,7 +230,7 @@ public Set<DeploymentDescription> getDeployments() throws IOException {
// Get all the deployments in the deployment repository
builder.addStep(readDeployments);

final ModelNode address = createAddress(SERVER_GROUP, "*", DEPLOYMENT, "*");
final ModelNode address = Operations.createAddress(SERVER_GROUP, "*", DEPLOYMENT, "*");
// Get all the deployments that are deployed to server groups
builder.addStep(Operations.createReadResourceOperation(address));

Expand Down Expand Up @@ -324,20 +323,20 @@ public boolean hasDeployment(final String name) {

@Override
public boolean hasDeployment(final String name, final String serverGroup) {
final ModelNode address = DeploymentOperations.createAddress(SERVER_GROUP,
final ModelNode address = Operations.createAddress(SERVER_GROUP,
Assertions.requiresNotNullOrNotEmptyParameter("serverGroup", serverGroup));
return hasDeployment(address, Assertions.requiresNotNullOrNotEmptyParameter("name", name));
}

@Override
public boolean isEnabled(final String name) {
return isEnabled(
DeploymentOperations.createAddress(DEPLOYMENT, Assertions.requiresNotNullOrNotEmptyParameter("name", name)));
Operations.createAddress(DEPLOYMENT, Assertions.requiresNotNullOrNotEmptyParameter("name", name)));
}

@Override
public boolean isEnabled(final String name, final String serverGroup) {
return isEnabled(DeploymentOperations.createAddress(
return isEnabled(Operations.createAddress(
SERVER_GROUP,
Assertions.requiresNotNullOrNotEmptyParameter("serverGroup", serverGroup),
DEPLOYMENT,
Expand Down Expand Up @@ -389,7 +388,7 @@ private DeploymentResult execute(final Operation op) throws IOException {

private DeploymentDescription getServerGroupDeployment(final String name) throws IOException {
final Set<String> serverGroups = new LinkedHashSet<>();
final ModelNode address = createAddress(SERVER_GROUP, "*", DEPLOYMENT, name);
final ModelNode address = Operations.createAddress(SERVER_GROUP, "*", DEPLOYMENT, name);

final ModelNode result = client.execute(Operations.createReadResourceOperation(address));
if (Operations.isSuccessfulOutcome(result)) {
Expand Down Expand Up @@ -520,7 +519,7 @@ private ContainerDescription get() {
synchronized (this) {
if (containerDescription == null) {
try {
containerDescription = ServerHelper.getContainerDescription(client);
containerDescription = ContainerDescription.lookup(client);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
65 changes: 12 additions & 53 deletions src/main/java/org/wildfly/plugin/tools/DeploymentOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
import static org.jboss.as.controller.client.helpers.Operations.createOperation;
import static org.jboss.as.controller.client.helpers.Operations.createRemoveOperation;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.jboss.as.controller.client.Operation;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.as.controller.client.helpers.Operations.CompositeOperationBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.wildfly.common.Assert;
import org.wildfly.plugin.tools.util.Assertions;

Expand All @@ -43,7 +41,6 @@
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
@SuppressWarnings({ "unused", "StaticMethodOnlyUsedInOneClass", "WeakerAccess" })
public class DeploymentOperations {
static final String ENABLED = "enabled";
static final ModelNode EMPTY_ADDRESS = new ModelNode().setEmptyList();
Expand All @@ -52,45 +49,6 @@ public class DeploymentOperations {
EMPTY_ADDRESS.protect();
}

/**
* Creates an {@linkplain ModelNode address} that can be used as the address for an operation. The address is
* simply a {@link ModelNode} of type {@link ModelType#LIST}.
* <p>
* The string is split into key/value pairs. If the final key does not have a value an {@code *} is used to
* indicate a wildcard for the address.
* </p>
*
* @param pairs the key/value pairs to use
*
* @return an address for the key/value pairs
*/
static ModelNode createAddress(final String... pairs) {
return createAddress(Arrays.asList(pairs));
}

/**
* Creates an {@linkplain ModelNode address} that can be used as the address for an operation. The address is
* simply a {@link ModelNode} of type {@link ModelType#LIST}.
* <p>
* The string is split into key/value pairs. If the final key does not have a value an {@code *} is used to
* indicate a wildcard for the address.
* </p>
*
* @param pairs the key/value pairs to use
*
* @return an address for the key/value pairs
*/
static ModelNode createAddress(final Iterable<String> pairs) {
final ModelNode address = new ModelNode();
final Iterator<String> iterator = pairs.iterator();
while (iterator.hasNext()) {
final String key = iterator.next();
final String value = (iterator.hasNext() ? iterator.next() : "*");
address.add(key, value);
}
return address;
}

/**
* Creates an operation to add deployment content to a running server. If the deployment is set to be
* {@linkplain Deployment#isEnabled() enabled} the content will also be deployed.
Expand Down Expand Up @@ -288,7 +246,7 @@ public static Operation createUndeployOperation(final Set<UndeployDescription> u
*/
static void addDeploymentOperationStep(final CompositeOperationBuilder builder, final Deployment deployment) {
final String name = deployment.getName();
final ModelNode address = createAddress(DEPLOYMENT, name);
final ModelNode address = Operations.createAddress(DEPLOYMENT, name);
final String runtimeName = deployment.getRuntimeName();
final ModelNode addOperation = createAddOperation(address);
if (runtimeName != null) {
Expand All @@ -302,7 +260,7 @@ static void addDeploymentOperationStep(final CompositeOperationBuilder builder,
// If the server groups are empty this is a standalone deployment
if (!serverGroups.isEmpty()) {
for (String serverGroup : serverGroups) {
final ModelNode sgAddress = createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
final ModelNode sgAddress = Operations.createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);

final ModelNode op = createAddOperation(sgAddress);
op.get(ENABLED).set(deployment.isEnabled());
Expand All @@ -326,11 +284,11 @@ static void addDeployOperationStep(final CompositeOperationBuilder builder, fina
final Set<String> serverGroups = deployment.getServerGroups();
// If the server groups are empty this is a standalone deployment
if (serverGroups.isEmpty()) {
final ModelNode address = createAddress(DEPLOYMENT, name);
final ModelNode address = Operations.createAddress(DEPLOYMENT, name);
builder.addStep(createOperation(DEPLOYMENT_DEPLOY_OPERATION, address));
} else {
for (String serverGroup : serverGroups) {
final ModelNode address = createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
final ModelNode address = Operations.createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
builder.addStep(createOperation(DEPLOYMENT_DEPLOY_OPERATION, address));
}
}
Expand Down Expand Up @@ -360,7 +318,7 @@ static void addReplaceOperationSteps(final CompositeOperationBuilder builder, fi
if (!serverGroups.isEmpty()) {
serverGroups.removeAll(currentDeployment.getServerGroups());
for (String serverGroup : serverGroups) {
final ModelNode sgAddress = createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
final ModelNode sgAddress = Operations.createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
final ModelNode addOp = createAddOperation(sgAddress);
// Explicitly set to false here as the full-replace-deployment should take care of enabling this
addOp.get(ENABLED).set(false);
Expand Down Expand Up @@ -411,11 +369,12 @@ private static void addRedeployOperationStep(final CompositeOperationBuilder bui
final String deploymentName = deployment.getName();
final Set<String> serverGroups = deployment.getServerGroups();
if (serverGroups.isEmpty()) {
builder.addStep(createOperation(DEPLOYMENT_REDEPLOY_OPERATION, createAddress(DEPLOYMENT, deploymentName)));
builder.addStep(
createOperation(DEPLOYMENT_REDEPLOY_OPERATION, Operations.createAddress(DEPLOYMENT, deploymentName)));
} else {
for (String serverGroup : serverGroups) {
builder.addStep(createOperation(DEPLOYMENT_REDEPLOY_OPERATION,
createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, deploymentName)));
Operations.createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, deploymentName)));
}
}
}
Expand All @@ -425,21 +384,21 @@ private static void addUndeployOperationStep(final CompositeOperationBuilder bui
final String name = undeployDescription.getName();
final Set<String> serverGroups = undeployDescription.getServerGroups();
if (serverGroups.isEmpty()) {
final ModelNode address = createAddress(DEPLOYMENT, name);
final ModelNode address = Operations.createAddress(DEPLOYMENT, name);
builder.addStep(createOperation(DEPLOYMENT_UNDEPLOY_OPERATION, address));
if (undeployDescription.isRemoveContent()) {
builder.addStep(createRemoveOperation(address));
}
} else {
for (String serverGroup : serverGroups) {
final ModelNode address = createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
final ModelNode address = Operations.createAddress(SERVER_GROUP, serverGroup, DEPLOYMENT, name);
builder.addStep(createOperation(DEPLOYMENT_UNDEPLOY_OPERATION, address));
if (undeployDescription.isRemoveContent()) {
builder.addStep(createRemoveOperation(address));
}
}
if (undeployDescription.isRemoveContent()) {
builder.addStep(createRemoveOperation(createAddress(DEPLOYMENT, name)));
builder.addStep(createRemoveOperation(Operations.createAddress(DEPLOYMENT, name)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ public OperationExecutionException(final String message, final ModelNode operati
this.result.protect();
}

/**
* Creates a new exception with the failure message from the result.
*
* @param message the message to prepend to the failure message
* @param cause the cause of the error
*/
public OperationExecutionException(final String message, final Throwable cause) {
super(message, cause);
result = new ModelNode(message + ": " + cause.getMessage());
}

/**
* Returns the result from the operation executed.
*
Expand Down
Loading