• 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

20.35
/with-jetty/src/main/java/com/aspectran/jetty/shell/command/JettyCommand.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.jetty.shell.command;
17

18
import com.aspectran.core.component.bean.BeanException;
19
import com.aspectran.core.component.bean.BeanRegistry;
20
import com.aspectran.jetty.server.JettyServer;
21
import com.aspectran.shell.command.AbstractCommand;
22
import com.aspectran.shell.command.CommandRegistry;
23
import com.aspectran.shell.command.option.Arguments;
24
import com.aspectran.shell.command.option.Option;
25
import com.aspectran.shell.command.option.ParsedOptions;
26
import com.aspectran.shell.console.ShellConsole;
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 'jetty' to control the Jetty Server.
36
 */
37
public class JettyCommand extends AbstractCommand {
38

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

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

43
    private final CommandDescriptor descriptor = new CommandDescriptor();
1✔
44

45
    public JettyCommand(CommandRegistry registry) {
46
        super(registry);
1✔
47

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

58
        Arguments arguments = touchArguments();
1✔
59
        arguments.setTitle("Available commands:");
1✔
60
        arguments.put("start", "Start Jetty server");
1✔
61
        arguments.put("stop", "Stop Jetty server");
1✔
62
        arguments.put("restart", "Restart Jetty server");
1✔
63
        arguments.put("status", "Display a brief status report");
1✔
64
        arguments.setRequired(true);
1✔
65
    }
1✔
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", "jetty.server");
×
83

84
        if (command != null) {
×
85
            switch (command) {
×
86
                case "start":
87
                    startJettyServer(serverName, console);
×
88
                    break;
×
89
                case "stop":
90
                    stopJettyServer(serverName, console);
×
91
                    break;
×
92
                case "restart":
93
                    if (stopJettyServer(serverName, console)) {
×
94
                        startJettyServer(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 startJettyServer(String serverName, ShellConsole console) throws Exception {
111
        JettyServer jettyServer = null;
×
112
        try {
113
            if (hasJettyServer(serverName)) {
×
114
                jettyServer = getJettyServer(serverName);
×
115
                if (jettyServer.isRunning()) {
×
116
                    console.writeError("Jetty server is already running");
×
117
                } else {
118
                    jettyServer.start();
×
119
                    printStatus(jettyServer.getState(), console);
×
120
                }
121
            } else {
122
                jettyServer = getJettyServer(serverName);
×
123
                if (!jettyServer.isRunning()) {
×
124
                    jettyServer.start();
×
125
                }
126
                printStatus(jettyServer.getState(), console);
×
127
            }
128
        } catch (BeanException e) {
×
129
            console.writeError("Jetty server is not available. Cause: " + e);
×
130
        } catch (Exception e) {
×
131
            if (jettyServer != null) {
×
132
                destroyJettyServer(jettyServer);
×
133
            }
134
            Throwable cause = ExceptionUtils.getRootCause(e);
×
135
            if (cause instanceof BindException) {
×
136
                console.writeError("Jetty server failed to start. Cause: Port already in use");
×
137
            } else {
138
                console.writeError(e.toString());
×
139
            }
140
        }
×
141
    }
×
142

143
    private boolean stopJettyServer(String serverName, ShellConsole console) {
144
        boolean success = false;
×
145
        try {
146
            if (hasJettyServer(serverName)) {
×
147
                JettyServer jettyServer = getJettyServer(serverName);
×
NEW
148
                jettyServer.stop();
×
149
                destroyJettyServer(jettyServer);
×
150
                printStatus(LifeCycle.STOPPED, console);
×
151
                success = true;
×
152
            } else {
×
153
                console.writeError("Jetty server is not running");
×
154
            }
155
        } catch (BeanException e) {
×
156
            console.writeError("Jetty 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 (hasJettyServer(serverName)) {
×
166
                JettyServer jettyServer = getJettyServer(serverName);
×
167
                if (jettyServer.isStarted()) {
×
168
                    printStatus(LifeCycle.RUNNING, console);
×
169
                } else {
170
                    printStatus(jettyServer.getState(), console);
×
171
                }
172
            } else {
×
173
                printStatus(LifeCycle.STOPPED, console);
×
174
            }
175
        } catch (BeanException e) {
×
176
            console.writeError("Jetty server is not available. Cause: " + e);
×
177
        } catch (Exception e) {
×
178
            console.writeError(e.toString());
×
179
        }
×
180
    }
×
181

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

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

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

201
    private void destroyJettyServer(JettyServer jettyServer) throws Exception {
202
        BeanRegistry beanRegistry = getActiveShellService().getActivityContext().getBeanRegistry();
×
203
        beanRegistry.destroySingleton(jettyServer);
×
204
    }
×
205

206
    @Override
207
    public Descriptor getDescriptor() {
208
        return descriptor;
1✔
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;
1✔
221
        }
222

223
        @Override
224
        @NonNull
225
        public String getDescription() {
226
            return "Use the command 'jetty' to control the Jetty server";
1✔
227
        }
228

229
        @Override
230
        @Nullable
231
        public String getUsage() {
232
            return null;
1✔
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