• 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

67.24
/with-jetty/src/main/java/com/aspectran/jetty/server/JettyServer.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.server;
17

18
import com.aspectran.core.component.bean.ablility.DisposableBean;
19
import com.aspectran.core.component.bean.ablility.InitializableBean;
20
import com.aspectran.jetty.server.servlet.JettyWebAppContext;
21
import com.aspectran.utils.StringUtils;
22
import com.aspectran.utils.annotation.jsr305.NonNull;
23
import com.aspectran.utils.logging.Logger;
24
import com.aspectran.utils.logging.LoggerFactory;
25
import org.eclipse.jetty.ee10.servlet.SessionHandler;
26
import org.eclipse.jetty.ee10.webapp.WebAppContext;
27
import org.eclipse.jetty.server.ConnectionLimit;
28
import org.eclipse.jetty.server.Connector;
29
import org.eclipse.jetty.server.Handler;
30
import org.eclipse.jetty.server.NetworkConnector;
31
import org.eclipse.jetty.server.Server;
32
import org.eclipse.jetty.server.handler.ContextHandler;
33
import org.eclipse.jetty.server.handler.StatisticsHandler;
34
import org.eclipse.jetty.util.thread.ThreadPool;
35

36
import java.util.List;
37
import java.util.Objects;
38
import java.util.stream.Collectors;
39

40
/**
41
 * The Jetty Server managed by Aspectran.
42
 *
43
 * <p>Created: 2016. 12. 22.</p>
44
 */
45
public class JettyServer extends Server implements InitializableBean, DisposableBean {
46

47
    private static final Logger logger = LoggerFactory.getLogger(JettyServer.class);
1✔
48

49
    private boolean shutdownGracefully = true;
1✔
50

51
    private GracefulShutdown gracefulShutdown;
52

53
    private boolean autoStart = true;
1✔
54

55
    public JettyServer() {
56
        super();
×
57
    }
×
58

59
    public JettyServer(int port) {
60
        super(port);
×
61
    }
×
62

63
    public JettyServer(ThreadPool pool) {
64
        super(pool);
1✔
65
    }
1✔
66

67
    public boolean isAutoStart() {
68
        return autoStart;
×
69
    }
70

71
    public void setAutoStart(boolean autoStart) {
72
        this.autoStart = autoStart;
1✔
73
    }
1✔
74

75
    public void setShutdownGracefully(boolean shutdownGracefully) {
76
        this.shutdownGracefully = shutdownGracefully;
1✔
77
    }
1✔
78

79
    public void setMaxConnections(int maxConnections) {
80
        if (maxConnections > -1) {
1✔
81
            addBean(new ConnectionLimit(maxConnections, this));
1✔
82
        }
83
    }
1✔
84

85
    public void setSystemProperty(String key, String value) {
86
        System.setProperty(key, value);
×
87
    }
×
88

89
    public StatisticsHandler getStatisticsHandler() {
90
        return findStatisticsHandler(getHandler());
1✔
91
    }
92

93
    private StatisticsHandler findStatisticsHandler(Handler handler) {
94
        if (handler instanceof StatisticsHandler statisticsHandler) {
1✔
95
            return statisticsHandler;
1✔
96
        }
97
        if (handler instanceof Handler.Wrapper handlerWrapper) {
×
98
            return findStatisticsHandler(handlerWrapper.getHandler());
×
99
        }
100
        return null;
×
101
    }
102

103
    public ContextHandler getContextHandler(String contextPath) {
104
        return findContextHandler(contextPath, getHandler());
×
105
    }
106

107
    private ContextHandler findContextHandler(String contextPath, Handler handler) {
108
        if (handler instanceof ContextHandler contextHandler) {
×
109
            if (Objects.equals(contextPath, contextHandler.getContextPath())) {
×
110
                return contextHandler;
×
111
            }
112
        }
113
        if (handler instanceof Handler.Wrapper handlerWrapper) {
×
114
            return findContextHandler(contextPath, handlerWrapper.getHandler());
×
115
        }
116
        if (handler instanceof Handler.Sequence handlerSequence) {
×
117
            for (Handler child : handlerSequence.getHandlers()) {
×
118
                return findContextHandler(contextPath, child);
×
119
            }
120
        }
121
        return null;
×
122
    }
123

124
    public SessionHandler getSessionHandler(String contextPath) {
125
        ContextHandler contextHandler = getContextHandler(contextPath);
×
126
        if (contextHandler instanceof WebAppContext webAppContext) {
×
127
            return webAppContext.getSessionHandler();
×
128
        } else {
129
            return null;
×
130
        }
131
    }
132

133
    @Override
134
    public void initialize() throws Exception {
135
        setStopAtShutdown(false);
1✔
136
        if (autoStart && !isRunning()) {
1✔
137
            start();
×
138
        }
139
    }
1✔
140

141
    @Override
142
    public void destroy() {
143
        if (!isStopped() && isStopping()) {
1✔
144
            try {
NEW
145
                stop();
×
NEW
146
            } catch (Exception e) {
×
NEW
147
                logger.error("Error while stopping jetty server", e);
×
NEW
148
            }
×
149
        }
150
    }
1✔
151

152
    @Override
153
    public void doStart() throws Exception {
154
        logger.info("Starting embedded Jetty server");
1✔
155
        if (shutdownGracefully && getStatisticsHandler() != null) {
1✔
156
            gracefulShutdown = new GracefulShutdown(this);
1✔
157
        } else if (gracefulShutdown != null) {
×
158
            gracefulShutdown.abort();
×
159
            gracefulShutdown = null;
×
160
        }
161
        for (Handler handler : getHandlers()) {
1✔
162
            handleDeferredInitialize(handler);
1✔
163
        }
1✔
164
        super.doStart();
1✔
165
        logger.info("Jetty started on port(s) " + getActualPortsDescription()
1✔
166
                + " with context path '" + getContextPath() + "'");
1✔
167
    }
1✔
168

169
    @Override
170
    public void doStop() {
171
        logger.info("Stopping embedded Jetty server");
1✔
172
        if (gracefulShutdown != null) {
1✔
173
            gracefulShutdown.shutDownGracefully(result -> {
1✔
174
                shutdown();
1✔
175
            });
1✔
176
        } else {
177
            shutdown();
×
178
        }
179
    }
1✔
180

181
    private void shutdown() {
182
        try {
183
            getHandler().stop();
1✔
184
            super.doStop();
1✔
185
            super.destroy();
1✔
186
        } catch (InterruptedException e) {
×
187
            Thread.currentThread().interrupt();
×
188
        } catch (Exception e) {
×
189
            logger.error("Unable to stop embedded Jetty server", e);
×
190
        } finally {
191
            for (Handler handler : getHandlers()) {
1✔
192
                handleDeferredDispose(handler);
1✔
193
            }
1✔
194
        }
195
    }
1✔
196

197
    private void handleDeferredInitialize(Handler handler) {
198
        if (handler instanceof JettyWebAppContext jettyWebAppContext) {
1✔
199
            jettyWebAppContext.deferredInitialize(this);
1✔
200
        } else if (handler instanceof Handler.Wrapper handlerWrapper) {
1✔
201
            handleDeferredInitialize(handlerWrapper.getHandler());
1✔
202
        } else if (handler instanceof Handler.Collection handlerCollection) {
1✔
203
            handleDeferredInitialize(handlerCollection.getHandlers());
1✔
204
        }
205
    }
1✔
206

207
    private void handleDeferredInitialize(@NonNull List<Handler> handlers) {
208
        for (Handler handler : handlers) {
1✔
209
            handleDeferredInitialize(handler);
1✔
210
        }
1✔
211
    }
1✔
212

213
    private void handleDeferredDispose(Handler handler) {
214
        if (handler instanceof JettyWebAppContext jettyWebAppContext) {
1✔
215
            jettyWebAppContext.deferredDispose();
1✔
216
        } else if (handler instanceof Handler.Wrapper handlerWrapper) {
1✔
217
            handleDeferredDispose(handlerWrapper.getHandler());
1✔
218
        } else if (handler instanceof Handler.Collection handlerCollection) {
1✔
219
            handleDeferredDispose(handlerCollection.getHandlers());
1✔
220
        }
221
    }
1✔
222

223
    private void handleDeferredDispose(@NonNull List<Handler> handlers) {
224
        for (Handler handler : handlers) {
1✔
225
            handleDeferredDispose(handler);
1✔
226
        }
1✔
227
    }
1✔
228

229
    private String getContextPath() {
230
        Container handlerContainer = (Container)getHandler();
1✔
231
        return handlerContainer.getHandlers().stream()
1✔
232
                .filter(ContextHandler.class::isInstance).map(ContextHandler.class::cast)
1✔
233
                .map(ContextHandler::getContextPath).collect(Collectors.joining("', '"));
1✔
234
    }
235

236
    @NonNull
237
    private String getActualPortsDescription() {
238
        StringBuilder ports = new StringBuilder();
1✔
239
        for (Connector connector : getConnectors()) {
1✔
240
            NetworkConnector connector1 = (NetworkConnector)connector;
1✔
241
            if (!ports.isEmpty()) {
1✔
242
                ports.append(", ");
×
243
            }
244
            ports.append(connector1.getLocalPort());
1✔
245
            ports.append(" (");
1✔
246
            ports.append(StringUtils.joinCommaDelimitedList(connector.getProtocols()));
1✔
247
            ports.append(")");
1✔
248
        }
249
        return ports.toString();
1✔
250
    }
251

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