• 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/shell/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.shell.command;
17

18
import com.aspectran.core.component.bean.BeanException;
19
import com.aspectran.core.component.bean.BeanRegistry;
20
import com.aspectran.shell.command.AbstractCommand;
21
import com.aspectran.shell.command.CommandRegistry;
22
import com.aspectran.shell.command.option.Arguments;
23
import com.aspectran.shell.command.option.Option;
24
import com.aspectran.shell.command.option.ParsedOptions;
25
import com.aspectran.shell.console.ShellConsole;
26
import com.aspectran.undertow.server.TowServer;
27
import com.aspectran.utils.ExceptionUtils;
28
import com.aspectran.utils.annotation.jsr305.NonNull;
29
import com.aspectran.utils.annotation.jsr305.Nullable;
30
import com.aspectran.utils.lifecycle.LifeCycle;
31

32
import java.net.BindException;
33

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

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

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

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

45
    public UndertowCommand(CommandRegistry registry) {
46
        super(registry);
×
47

48
        addOption(Option.builder("server")
×
49
                .valueName("name")
×
50
                .withEqualSign()
×
51
                .desc("ID of bean that defined Undertow server")
×
52
                .build());
×
53
        addOption(Option.builder("h")
×
54
                .longName("help")
×
55
                .desc("Display help for this command")
×
56
                .build());
×
57

58
        Arguments arguments = touchArguments();
×
59
        arguments.setTitle("Available commands:");
×
60
        arguments.put("start", "Start Undertow server");
×
61
        arguments.put("stop", "Stop Undertow server");
×
62
        arguments.put("restart", "Restart Undertow server");
×
63
        arguments.put("status", "Display a brief status report");
×
64
        arguments.setRequired(true);
×
65
    }
×
66

67
    @Override
68
    public void execute(@NonNull ParsedOptions options, ShellConsole console) throws Exception {
69
        if (!options.hasOptions() && !options.hasArgs()) {
×
70
            printQuickHelp(console);
×
71
            return;
×
72
        }
73
        if (options.hasOption("help")) {
×
74
            printHelp(console);
×
75
            return;
×
76
        }
77

78
        String command = null;
×
79
        if (options.hasArgs()) {
×
80
            command = options.getFirstArg();
×
81
        }
82
        String serverName = options.getValue("server", "tow.server");
×
83

84
        if (command != null) {
×
85
            switch (command) {
×
86
                case "start":
87
                    startTowServer(serverName, console);
×
88
                    break;
×
89
                case "stop":
90
                    stopTowServer(serverName, console);
×
91
                    break;
×
92
                case "restart":
93
                    if (stopTowServer(serverName, console)) {
×
94
                        startTowServer(serverName, console);
×
95
                    }
96
                    break;
97
                case "status":
98
                    printServerStatus(serverName, console);
×
99
                    break;
×
100
                default:
101
                    console.writeError("Unknown command '" + String.join(" ", options.getArgs()) + "'");
×
102
                    printQuickHelp(console);
×
103
                    break;
104
            }
×
105
        } else {
106
            printQuickHelp(console);
×
107
        }
108
    }
×
109

110
    private void startTowServer(String serverName, ShellConsole console) throws Exception {
111
        TowServer towServer = null;
×
112
        try {
113
            if (hasTowServer(serverName)) {
×
114
                towServer = getTowServer(serverName);
×
115
                if (towServer.isRunning()) {
×
116
                    console.writeError("Undertow server is already running");
×
117
                } else {
118
                    towServer.start();
×
119
                    printStatus(towServer.getState(), console);
×
120
                }
121
            } else {
122
                towServer = getTowServer(serverName);
×
123
                if (!towServer.isRunning()) {
×
124
                    towServer.start();
×
125
                }
126
                printStatus(towServer.getState(), console);
×
127
            }
128
        } catch (BeanException e) {
×
129
            console.writeError("Undertow server is not available. Cause: " + e);
×
130
        } catch (Exception e) {
×
131
            if (towServer != null) {
×
132
                destroyTowServer(towServer);
×
133
            }
134
            Throwable cause = ExceptionUtils.getRootCause(e);
×
135
            if (cause instanceof BindException) {
×
136
                console.writeError("Undertow server failed to start. Cause: Port already in use");
×
137
            } else {
138
                console.writeError(e.toString());
×
139
            }
140
        }
×
141
    }
×
142

143
    private boolean stopTowServer(String serverName, ShellConsole console) {
144
        boolean success = false;
×
145
        try {
146
            if (hasTowServer(serverName)) {
×
147
                TowServer towServer = getTowServer(serverName);
×
NEW
148
                towServer.stop();
×
149
                destroyTowServer(towServer);
×
150
                printStatus(LifeCycle.STOPPED, console);
×
151
                success = true;
×
152
            } else {
×
153
                console.writeError("Undertow server is not running");
×
154
            }
155
        } catch (BeanException e) {
×
156
            console.writeError("Undertow server is not available. Cause: " + e);
×
157
        } catch (Exception e) {
×
158
            console.writeError(e.toString());
×
159
        }
×
160
        return success;
×
161
    }
162

163
    private void printServerStatus(String serverName, ShellConsole console) {
164
        try {
165
            if (hasTowServer(serverName)) {
×
166
                TowServer towServer = getTowServer(serverName);
×
167
                if (towServer.isStarted()) {
×
168
                    printStatus(LifeCycle.RUNNING, console);
×
169
                } else {
170
                    printStatus(towServer.getState(), console);
×
171
                }
172
            } else {
×
173
                printStatus(LifeCycle.STOPPED, console);
×
174
            }
175
        } catch (BeanException e) {
×
176
            console.writeError("Undertow server is not available. Cause: " + e);
×
177
        } catch (Exception e) {
×
178
            console.writeError(e.toString());
×
179
        }
×
180
    }
×
181

182
    private void printStatus(String status, @NonNull ShellConsole console) {
183
        console.writeLine("----------------------------------------------------------------------------");
×
184
        console.setStyle("YELLOW");
×
185
        console.write(status);
×
186
        console.resetStyle();
×
187
        console.writeLine(" - Undertow " + TowServer.getVersion());
×
188
        console.writeLine("----------------------------------------------------------------------------");
×
189
    }
×
190

191
    private TowServer getTowServer(String serverName) {
192
        BeanRegistry beanRegistry = getActiveShellService().getActivityContext().getBeanRegistry();
×
193
        return beanRegistry.getBean(TowServer.class, serverName);
×
194
    }
195

196
    private boolean hasTowServer(String serverName) {
197
        BeanRegistry beanRegistry = getActiveShellService().getActivityContext().getBeanRegistry();
×
198
        return beanRegistry.hasSingleton(TowServer.class, serverName);
×
199
    }
200

201
    private void destroyTowServer(TowServer towServer) throws Exception {
202
        BeanRegistry beanRegistry = getActiveShellService().getActivityContext().getBeanRegistry();
×
203
        beanRegistry.destroySingleton(towServer);
×
204
    }
×
205

206
    @Override
207
    public Descriptor getDescriptor() {
208
        return descriptor;
×
209
    }
210

211
    private static class CommandDescriptor implements Descriptor {
212

213
        @Override
214
        public String getNamespace() {
215
            return NAMESPACE;
×
216
        }
217

218
        @Override
219
        public String getName() {
220
            return COMMAND_NAME;
×
221
        }
222

223
        @Override
224
        @NonNull
225
        public String getDescription() {
226
            return "Use the command 'undertow' to control the Undertow server";
×
227
        }
228

229
        @Override
230
        @Nullable
231
        public String getUsage() {
232
            return null;
×
233
        }
234

235
    }
236

237
}
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