• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #4014

25 Jan 2025 04:30PM CUT coverage: 35.455% (+0.008%) from 35.447%
#4014

push

github

topframe
Update

24 of 52 new or added lines in 13 files covered. (46.15%)

2 existing lines in 2 files now uncovered.

14289 of 40302 relevant lines covered (35.45%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/with-undertow/src/main/java/com/aspectran/undertow/daemon/command/UndertowCommand.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.undertow.daemon.command;
17

18
import com.aspectran.core.activity.request.ParameterMap;
19
import com.aspectran.core.component.bean.BeanException;
20
import com.aspectran.core.component.bean.BeanRegistry;
21
import com.aspectran.core.context.asel.item.ItemEvaluator;
22
import com.aspectran.core.context.rule.ItemRuleMap;
23
import com.aspectran.daemon.command.AbstractCommand;
24
import com.aspectran.daemon.command.CommandParameters;
25
import com.aspectran.daemon.command.CommandRegistry;
26
import com.aspectran.daemon.command.CommandResult;
27
import com.aspectran.undertow.server.TowServer;
28
import com.aspectran.utils.ExceptionUtils;
29
import com.aspectran.utils.StringUtils;
30
import com.aspectran.utils.annotation.jsr305.NonNull;
31
import com.aspectran.utils.lifecycle.LifeCycle;
32

33
import java.net.BindException;
34

35
/**
36
 * Use the command 'undertow' to control the Undertow Server.
37
 */
38
public class UndertowCommand extends AbstractCommand {
39

40
    private static final String NAMESPACE = "builtins";
41

42
    private static final String COMMAND_NAME = "undertow";
43

44
    private final CommandDescriptor descriptor = new CommandDescriptor();
×
45

46
    public UndertowCommand(CommandRegistry registry) {
47
        super(registry);
×
48
    }
×
49

50
    @Override
51
    public CommandResult execute(CommandParameters parameters) {
52
        try {
53
            String mode = null;
×
54
            String serverName = null;
×
55
            ItemRuleMap parameterItemRuleMap = parameters.getParameterItemRuleMap();
×
56
            if ((parameterItemRuleMap != null && !parameterItemRuleMap.isEmpty())) {
×
57
                ItemEvaluator evaluator = getDaemonService().getDefaultActivity().getItemEvaluator();
×
58
                ParameterMap parameterMap = evaluator.evaluateAsParameterMap(parameterItemRuleMap);
×
59
                mode = parameterMap.getParameter("mode");
×
60
                serverName = parameterMap.getParameter("server");
×
61
            }
62
            if (mode == null) {
×
63
                return failed("'mode' parameter is not specified");
×
64
            }
65
            if (!StringUtils.hasLength(serverName)) {
×
66
                serverName = "tow.server";
×
67
            }
68

69
            switch (mode) {
×
70
                case "start":
71
                    return startTowServer(serverName);
×
72
                case "stop":
73
                    return stopTowServer(serverName);
×
74
                case "restart":
75
                    CommandResult commandResult = stopTowServer(serverName);
×
76
                    if (commandResult.isSuccess()) {
×
77
                        commandResult = startTowServer(serverName);
×
78
                    }
79
                    return commandResult;
×
80
                case "status":
81
                    return printServerStatus(serverName);
×
82
                default:
83
                    return failed(error("Unknown mode '" + mode + "'"));
×
84
            }
85
        } catch (Exception e) {
×
86
            return failed(e);
×
87
        }
88
    }
89

90
    private CommandResult startTowServer(String serverName) throws Exception {
91
        TowServer towServer = null;
×
92
        try {
93
            if (hasTowServer(serverName)) {
×
94
                towServer = getTowServer(serverName);
×
95
                if (towServer.isRunning()) {
×
96
                    return failed(warn("Undertow server is already running"));
×
97
                } else {
98
                    towServer.start();
×
99
                    return success(info(getStatus(towServer.getState())));
×
100
                }
101
            } else {
102
                towServer = getTowServer(serverName);
×
103
                if (!towServer.isRunning()) {
×
104
                    towServer.start();
×
105
                }
106
                return success(info(getStatus(towServer.getState())));
×
107
            }
108
        } catch (Exception e) {
×
109
            if (towServer != null) {
×
110
                destroyTowServer(towServer);
×
111
            }
112
            Throwable cause = ExceptionUtils.getRootCause(e);
×
113
            if (cause instanceof BindException) {
×
114
                return failed("Undertow server failed to start. Cause: Port already in use", e);
×
115
            } else {
116
                return failed(e);
×
117
            }
118
        }
119
    }
120

121
    private CommandResult stopTowServer(String serverName) {
122
        try {
123
            if (hasTowServer(serverName)) {
×
124
                TowServer towServer = getTowServer(serverName);
×
NEW
125
                towServer.stop();
×
126
                destroyTowServer(towServer);
×
127
                return success(info(getStatus(LifeCycle.STOPPED)));
×
128
            } else {
129
                return failed(warn("Undertow server is not running"));
×
130
            }
131
        } catch (Exception e) {
×
132
            return failed(e);
×
133
        }
134
    }
135

136
    private CommandResult printServerStatus(String serverName) {
137
        try {
138
            if (hasTowServer(serverName)) {
×
139
                TowServer towServer = getTowServer(serverName);
×
140
                if (towServer.isStarted()) {
×
141
                    return success(info(getStatus(LifeCycle.RUNNING)));
×
142
                } else {
143
                    return success(info(getStatus(towServer.getState())));
×
144
                }
145
            } else {
146
                return success(info(getStatus(LifeCycle.STOPPED)));
×
147
            }
148
        } catch (BeanException e) {
×
149
            return failed("Undertow server is not available", e);
×
150
        } catch (Exception e) {
×
151
            return failed(e);
×
152
        }
153
    }
154

155
    @NonNull
156
    private String getStatus(String status) {
157
        return status + " - " + "Undertow " + TowServer.getVersion();
×
158
    }
159

160
    private TowServer getTowServer(String serverName) {
161
        BeanRegistry beanRegistry = getDaemonService().getActivityContext().getBeanRegistry();
×
162
        return beanRegistry.getBean(TowServer.class, serverName);
×
163
    }
164

165
    private boolean hasTowServer(String serverName) {
166
        BeanRegistry beanRegistry = getDaemonService().getActivityContext().getBeanRegistry();
×
167
        return beanRegistry.hasSingleton(TowServer.class, serverName);
×
168
    }
169

170
    private void destroyTowServer(TowServer towServer) throws Exception {
171
        BeanRegistry beanRegistry = getDaemonService().getActivityContext().getBeanRegistry();
×
172
        beanRegistry.destroySingleton(towServer);
×
173
    }
×
174

175
    @Override
176
    public Descriptor getDescriptor() {
177
        return descriptor;
×
178
    }
179

180
    private static class CommandDescriptor implements Descriptor {
181

182
        @Override
183
        public String getNamespace() {
184
            return NAMESPACE;
×
185
        }
186

187
        @Override
188
        public String getName() {
189
            return COMMAND_NAME;
×
190
        }
191

192
        @Override
193
        @NonNull
194
        public String getDescription() {
195
            return "Use the command 'undertow' to control the Undertow server";
×
196
        }
197

198
    }
199

200
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc