-
Notifications
You must be signed in to change notification settings - Fork 97
/
StartServersHandler.java
140 lines (123 loc) · 6.34 KB
/
StartServersHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.jboss.as.host.controller.operations;
import org.jboss.as.controller.HashUtil;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.AUTO_START;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_CONFIG;
import static org.jboss.as.host.controller.HostControllerLogger.ROOT_LOGGER;
import static org.jboss.as.host.controller.HostControllerMessages.MESSAGES;
import java.util.Map;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationDefinition;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.SimpleOperationDefinitionBuilder;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.host.controller.HostControllerEnvironment;
import org.jboss.as.host.controller.HostRunningModeControl;
import org.jboss.as.host.controller.RestartMode;
import org.jboss.as.host.controller.ServerInventory;
import org.jboss.as.process.ProcessInfo;
import org.jboss.dmr.ModelNode;
/**
* Starts or reconnect all auto-start servers (at boot).
*
* @author Brian Stansberry (c) 2011 Red Hat Inc.
*/
public class StartServersHandler implements OperationStepHandler {
public static final String OPERATION_NAME = "start-servers";
//Private method does not need resources for description
public static final OperationDefinition DEFINITION = new SimpleOperationDefinitionBuilder(OPERATION_NAME, null)
.setPrivateEntry()
.build();
private final ServerInventory serverInventory;
private final HostControllerEnvironment hostControllerEnvironment;
private final HostRunningModeControl runningModeControl;
/**
* Create the ServerAddHandler
*/
public StartServersHandler(final HostControllerEnvironment hostControllerEnvironment, final ServerInventory serverInventory, HostRunningModeControl runningModeControl) {
this.hostControllerEnvironment = hostControllerEnvironment;
this.serverInventory = serverInventory;
this.runningModeControl = runningModeControl;
}
/**
* {@inheritDoc}
*/
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
if (!context.isBooting()) {
throw new OperationFailedException(new ModelNode().set(MESSAGES.invocationNotAllowedAfterBoot(operation.require(OP))));
}
if (context.getRunningMode() == RunningMode.ADMIN_ONLY) {
throw new OperationFailedException(new ModelNode(MESSAGES.cannotStartServersInvalidMode(context.getRunningMode())));
}
final ModelNode domainModel = Resource.Tools.readModel(context.readResourceFromRoot(PathAddress.EMPTY_ADDRESS, true));
context.addStep(new OperationStepHandler() {
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
// start servers
final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
final ModelNode hostModel = Resource.Tools.readModel(resource);
if(hostModel.hasDefined(SERVER_CONFIG)) {
final ModelNode servers = hostModel.get(SERVER_CONFIG).clone();
if (hostControllerEnvironment.isRestart() || runningModeControl.getRestartMode() == RestartMode.HC_ONLY){
restartedHcStartOrReconnectServers(servers, domainModel);
} else {
cleanStartServers(servers, domainModel);
}
}
context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
}, OperationContext.Stage.RUNTIME);
context.stepCompleted();
}
private void cleanStartServers(final ModelNode servers, final ModelNode domainModel){
for(final String serverName : servers.keys()) {
if(servers.get(serverName, AUTO_START).asBoolean(true)) {
try {
serverInventory.startServer(serverName, domainModel);
} catch (Exception e) {
ROOT_LOGGER.failedToStartServer(e, serverName);
}
}
}
}
private void restartedHcStartOrReconnectServers(final ModelNode servers, final ModelNode domainModel){
Map<String, ProcessInfo> processInfos = serverInventory.determineRunningProcesses();
for(final String serverName : servers.keys()) {
ProcessInfo info = processInfos.get(serverInventory.getServerProcessName(serverName));
boolean auto = servers.get(serverName, AUTO_START).asBoolean(true);
if (info == null && auto) {
try {
serverInventory.startServer(serverName, domainModel);
} catch (Exception e) {
ROOT_LOGGER.failedToStartServer(e, serverName);
}
} else if (info != null){
// Reconnect the server using the current authKey
final byte[] authKey = info.getAuthKey();
serverInventory.reconnectServer(serverName, domainModel, authKey, info.isRunning(), info.isStopping());
}
}
}
}