-
Notifications
You must be signed in to change notification settings - Fork 97
/
ServerStartHandler.java
104 lines (88 loc) · 4.37 KB
/
ServerStartHandler.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
/*
* 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 static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.host.controller.HostControllerMessages.MESSAGES;
import java.util.Locale;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.client.helpers.domain.ServerStatus;
import org.jboss.as.controller.descriptions.DescriptionProvider;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.host.controller.ServerInventory;
import org.jboss.as.host.controller.descriptions.HostRootDescription;
import org.jboss.dmr.ModelNode;
/**
* Starts a server.
*
* @author Brian Stansberry (c) 2011 Red Hat Inc.
*/
public class ServerStartHandler implements OperationStepHandler, DescriptionProvider {
public static final String OPERATION_NAME = "start";
private final ServerInventory serverInventory;
/**
* Create the ServerAddHandler
*/
public ServerStartHandler(final ServerInventory serverInventory) {
this.serverInventory = serverInventory;
}
/**
* {@inheritDoc}
*/
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
if (context.getRunningMode() == RunningMode.ADMIN_ONLY) {
throw new OperationFailedException(new ModelNode(MESSAGES.cannotStartServersInvalidMode(context.getRunningMode())));
}
final PathAddress address = PathAddress.pathAddress(operation.require(OP_ADDR));
final PathElement element = address.getLastElement();
final String serverName = element.getValue();
final boolean blocking = operation.get("blocking").asBoolean(false);
final ModelNode model = Resource.Tools.readModel(context.readResourceFromRoot(PathAddress.EMPTY_ADDRESS, true));
context.addStep(new OperationStepHandler() {
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
final ServerStatus origStatus = serverInventory.determineServerStatus(serverName);
if (origStatus != ServerStatus.STARTED && origStatus != ServerStatus.STARTING) {
final ServerStatus status = serverInventory.startServer(serverName, model, blocking);
context.getResult().set(status.toString());
} else {
context.getResult().set(origStatus.toString());
}
context.completeStep(new OperationContext.RollbackHandler() {
@Override
public void handleRollback(OperationContext context, ModelNode operation) {
if (origStatus != ServerStatus.STARTED && origStatus != ServerStatus.STARTING) {
serverInventory.stopServer(serverName, -1);
}
}
});
}
}, OperationContext.Stage.RUNTIME);
context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
@Override
public ModelNode getModelDescription(final Locale locale) {
return HostRootDescription.getStartServerOperation(locale);
}
}