Skip to content

Commit c137f02

Browse files
committed
[194] Create a ServerManagerListener implementation which takes a snapshot of the server configuration after the server is started. Before the server is shutdown, the snapshot configuration is restored.
Signed-off-by: James R. Perkins <[email protected]>
1 parent 86f5b9a commit c137f02

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

src/main/java/org/wildfly/plugin/tools/server/AbstractServerManager.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,15 @@ public boolean isClosed() {
365365

366366
@Override
367367
public void close() {
368-
internalClose(configuration.shutdownOnClose(), true);
368+
lock.lock();
369+
try {
370+
if (configuration.shutdownOnClose()) {
371+
beforeShutdown();
372+
}
373+
internalClose(configuration.shutdownOnClose(), true);
374+
} finally {
375+
lock.unlock();
376+
}
369377
}
370378

371379
@Override
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright The WildFly Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.wildfly.plugin.tools.server;
7+
8+
import java.io.IOException;
9+
import java.util.concurrent.TimeUnit;
10+
11+
import org.jboss.as.controller.client.helpers.Operations;
12+
import org.jboss.dmr.ModelNode;
13+
import org.jboss.logging.Logger;
14+
15+
/**
16+
* A listener which takes a {@linkplain ServerManager#takeSnapshot() snapshot} of the server configuration after the
17+
* server has started and restores that configuration before the server is shutdown.
18+
*
19+
* @author <a href="mailto:[email protected]">James R. Perkins</a>
20+
*/
21+
public class RestoreConfigListener implements ServerManagerListener {
22+
private static final Logger LOGGER = Logger.getLogger(RestoreConfigListener.class);
23+
private volatile String configPath;
24+
25+
@Override
26+
public void afterStart(final ServerManager serverManager) {
27+
try {
28+
configPath = serverManager.takeSnapshot();
29+
} catch (IOException e) {
30+
throw new ServerManagerException(e, "Failed to take a snapshot of the servers configuration.");
31+
}
32+
}
33+
34+
@Override
35+
public void beforeShutdown(final ServerManager serverManager) {
36+
final String configPath = this.configPath;
37+
if (configPath != null) {
38+
try {
39+
final String attributeName;
40+
final ModelNode address;
41+
if (serverManager instanceof DomainManager domainManager) {
42+
attributeName = "domain-config";
43+
address = domainManager.determineHostAddress();
44+
} else {
45+
attributeName = "server-config";
46+
address = new ModelNode().setEmptyList();
47+
}
48+
final ModelNode op = Operations.createOperation("reload", address);
49+
op.get(attributeName).set(configPath);
50+
serverManager.executeOperation(op);
51+
serverManager.waitFor(ServerManager.TIMEOUT, TimeUnit.SECONDS);
52+
final ModelNode result = serverManager.client()
53+
.execute(Operations.createOperation("write-config", address));
54+
if (!Operations.isSuccessfulOutcome(result)) {
55+
throw new ServerManagerException(
56+
"Failed to write config after restoring from snapshot %s", Operations.getFailureDescription(result)
57+
.asString());
58+
}
59+
} catch (IOException e) {
60+
throw new ServerManagerException(e, "Failed to restore the previous configuration.");
61+
} catch (InterruptedException e) {
62+
throw new ServerManagerException(e, "Interrupted while attempting to reload the server");
63+
}
64+
} else {
65+
LOGGER.warn("No configuration was restored. The snapshot configuration file was never created.");
66+
}
67+
}
68+
}

src/test/java/org/wildfly/plugin/tools/Environment.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.wildfly.plugin.tools.server.Configuration;
2525
import org.wildfly.plugin.tools.server.DomainConfiguration;
2626
import org.wildfly.plugin.tools.server.DomainManager;
27+
import org.wildfly.plugin.tools.server.RestoreConfigListener;
2728
import org.wildfly.plugin.tools.server.ServerManager;
2829
import org.wildfly.plugin.tools.server.StandaloneConfiguration;
2930
import org.wildfly.plugin.tools.server.StandaloneManager;
@@ -150,12 +151,15 @@ public static Collection<String> getJvmArgs() {
150151
}
151152

152153
public static StandaloneManager launchStandalone() {
153-
return launchStandalone(true);
154+
return launchStandalone(true, false);
154155
}
155156

156-
public static StandaloneManager launchStandalone(final boolean shutdownOnClose) {
157+
public static StandaloneManager launchStandalone(final boolean shutdownOnClose, final boolean addRestorerListener) {
157158
final StandaloneManager serverManager = ServerManager
158159
.of(standaloneConfiguration().shutdownOnClose(shutdownOnClose));
160+
if (addRestorerListener) {
161+
serverManager.addServerManagerListener(new RestoreConfigListener());
162+
}
159163
try {
160164
if (!serverManager.start(TIMEOUT, TimeUnit.SECONDS).isRunning()) {
161165
serverManager.kill();
@@ -167,9 +171,12 @@ public static StandaloneManager launchStandalone(final boolean shutdownOnClose)
167171
return serverManager;
168172
}
169173

170-
public static DomainManager launchDomain() {
174+
public static DomainManager launchDomain(final boolean addRestorerListener) {
171175
final DomainManager serverManager = ServerManager
172176
.of(domainConfiguration());
177+
if (addRestorerListener) {
178+
serverManager.addServerManagerListener(new RestoreConfigListener());
179+
}
173180
try {
174181
if (!serverManager.start(TIMEOUT, TimeUnit.SECONDS).isRunning()) {
175182
serverManager.kill();

src/test/java/org/wildfly/plugin/tools/server/ServerManagerIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void checkStandaloneServerState() throws Exception {
5353

5454
@Test
5555
public void checkStandaloneReloadIfRequired() throws Exception {
56-
try (StandaloneManager serverManager = Environment.launchStandalone()) {
56+
try (StandaloneManager serverManager = Environment.launchStandalone(true, true)) {
5757
// Execute a command which will put the server in a state of requiring a reload
5858
final ModelNode address = Operations.createAddress("subsystem", "remoting");
5959
ModelNode result = executeCommand(serverManager.client(),
@@ -71,7 +71,7 @@ public void checkStandaloneReloadIfRequired() throws Exception {
7171

7272
@Test
7373
public void checkDomainReloadIfRequired() throws Exception {
74-
try (DomainManager serverManager = Environment.launchDomain()) {
74+
try (DomainManager serverManager = Environment.launchDomain(true)) {
7575
// Execute a command which will put the server in a state of requiring a reload
7676
final ModelNode address = Operations.createAddress("profile", "full", "subsystem", "remoting");
7777
ModelNode result = executeCommand(serverManager.client(),
@@ -93,7 +93,7 @@ public void checkDomainReloadIfRequired() throws Exception {
9393
public void checkStandaloneServerManagerClosed() throws Exception {
9494
ServerManager checker;
9595
ProcessHandle process;
96-
try (StandaloneManager serverManager = Environment.launchStandalone(false)) {
96+
try (StandaloneManager serverManager = Environment.launchStandalone(false, false)) {
9797
checker = serverManager;
9898
process = serverManager.process().orElseThrow(() -> new AssertionError("Server process is null"));
9999
}
@@ -170,7 +170,7 @@ public void checkManagedStandalone() {
170170

171171
@Test
172172
public void checkManagedDomain() {
173-
try (ServerManager serverManager = Environment.launchDomain()) {
173+
try (ServerManager serverManager = Environment.launchDomain(false)) {
174174
final ServerManager managedServerManager = serverManager.asManaged();
175175
Assertions.assertThrows(UnsupportedOperationException.class, managedServerManager::kill);
176176
Assertions.assertThrows(UnsupportedOperationException.class, managedServerManager::shutdown);

0 commit comments

Comments
 (0)