• 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-jetty/src/main/java/com/aspectran/jetty/daemon/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.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.jetty.server.JettyServer;
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 'jetty' to control the Jetty Server.
37
 */
38
public class JettyCommand extends AbstractCommand {
39

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

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

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

46
    public JettyCommand(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 = "jetty.server";
×
67
            }
68

69
            switch (mode) {
×
70
                case "start":
71
                    return startJettyServer(serverName);
×
72
                case "stop":
73
                    return stopJettyServer(serverName);
×
74
                case "restart":
75
                    CommandResult commandResult = stopJettyServer(serverName);
×
76
                    if (commandResult.isSuccess()) {
×
77
                        commandResult = startJettyServer(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 startJettyServer(String serverName) throws Exception {
91
        JettyServer jettyServer = null;
×
92
        try {
93
            if (hasJettyServer(serverName)) {
×
94
                jettyServer = getJettyServer(serverName);
×
95
                if (jettyServer.isRunning()) {
×
96
                    return failed(warn("Jetty server is already running"));
×
97
                } else {
98
                    jettyServer.start();
×
99
                    return success(info(getStatus(jettyServer.getState())));
×
100
                }
101
            } else {
102
                jettyServer = getJettyServer(serverName);
×
103
                if (!jettyServer.isRunning()) {
×
104
                    jettyServer.start();
×
105
                }
106
                return success(info(getStatus(jettyServer.getState())));
×
107
            }
108
        } catch (Exception e) {
×
109
            if (jettyServer != null) {
×
110
                destroyJettyServer(jettyServer);
×
111
            }
112
            Throwable cause = ExceptionUtils.getRootCause(e);
×
113
            if (cause instanceof BindException) {
×
114
                return failed("Jetty server failed to start. Cause: Port already in use", e);
×
115
            } else {
116
                return failed(e);
×
117
            }
118
        }
119
    }
120

121
    private CommandResult stopJettyServer(String serverName) {
122
        try {
123
            if (hasJettyServer(serverName)) {
×
124
                JettyServer jettyServer = getJettyServer(serverName);
×
NEW
125
                jettyServer.stop();
×
126
                destroyJettyServer(jettyServer);
×
127
                return success(info(getStatus(LifeCycle.STOPPED)));
×
128
            } else {
129
                return failed(warn("Jetty 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 (hasJettyServer(serverName)) {
×
139
                JettyServer jettyServer = getJettyServer(serverName);
×
140
                if (jettyServer.isStarted()) {
×
141
                    return success(info(getStatus(LifeCycle.RUNNING)));
×
142
                } else {
143
                    return success(info(getStatus(jettyServer.getState())));
×
144
                }
145
            } else {
146
                return success(info(getStatus(LifeCycle.STOPPED)));
×
147
            }
148
        } catch (BeanException e) {
×
149
            return failed("Jetty 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 + " - " + "Jetty " + JettyServer.getVersion();
×
158
    }
159

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

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

170
    private void destroyJettyServer(JettyServer jettyServer) throws Exception {
171
        BeanRegistry beanRegistry = getDaemonService().getActivityContext().getBeanRegistry();
×
172
        beanRegistry.destroySingleton(jettyServer);
×
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 'jetty' to control the Jetty 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