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

aspectran / aspectran / #3971

08 Jan 2025 12:17PM CUT coverage: 35.017% (-0.009%) from 35.026%
#3971

push

github

topframe
Update

8 of 27 new or added lines in 6 files covered. (29.63%)

7 existing lines in 5 files now uncovered.

14188 of 40517 relevant lines covered (35.02%)

0.35 hits per line

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

43.56
/with-undertow/src/main/java/com/aspectran/undertow/server/AbstractTowServer.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.server;
17

18
import com.aspectran.core.component.session.SessionHandler;
19
import com.aspectran.undertow.server.handler.RequestHandlerFactory;
20
import com.aspectran.undertow.server.session.TowSessionManager;
21
import com.aspectran.utils.Assert;
22
import com.aspectran.utils.annotation.jsr305.NonNull;
23
import com.aspectran.utils.annotation.jsr305.Nullable;
24
import com.aspectran.utils.lifecycle.AbstractLifeCycle;
25
import com.aspectran.utils.logging.Logger;
26
import com.aspectran.utils.logging.LoggerFactory;
27
import io.undertow.Undertow;
28
import io.undertow.server.HttpHandler;
29
import io.undertow.server.handlers.GracefulShutdownHandler;
30
import io.undertow.server.session.SessionManager;
31
import io.undertow.servlet.api.Deployment;
32
import io.undertow.servlet.api.DeploymentManager;
33
import org.xnio.Option;
34
import org.xnio.OptionMap;
35

36
import java.io.IOException;
37

38
/**
39
 * <p>Created: 11/25/23</p>
40
 */
41
public abstract class AbstractTowServer extends AbstractLifeCycle implements TowServer {
1✔
42

43
    private static final Logger logger = LoggerFactory.getLogger(AbstractTowServer.class);
1✔
44

45
    private final Undertow.Builder builder = Undertow.builder();
1✔
46

47
    private boolean autoStart;
48

49
    private boolean shutdownGracefully = true;
1✔
50

51
    private int shutdownTimeoutSecs;
52

53
    private RequestHandlerFactory requestHandlerFactory;
54

55
    private HttpHandler handler;
56

57
    /**
58
     * Returns whether the server starts automatically.
59
     * @return true if the server should be started
60
     */
61
    protected boolean isAutoStart() {
62
        return autoStart;
1✔
63
    }
64

65
    /**
66
     * Specifies whether the server should start automatically.
67
     * @param autoStart if the server should be started
68
     */
69
    public void setAutoStart(boolean autoStart) {
70
        this.autoStart = autoStart;
1✔
71
    }
1✔
72

73
    protected boolean isShutdownGracefully() {
74
        return shutdownGracefully;
1✔
75
    }
76

77
    public void setShutdownGracefully(boolean shutdownGracefully) {
78
        this.shutdownGracefully = shutdownGracefully;
×
79
    }
×
80

81
    protected int getShutdownTimeoutSecs() {
82
        return shutdownTimeoutSecs;
1✔
83
    }
84

85
    public void setShutdownTimeoutSecs(int shutdownTimeoutSecs) {
86
        this.shutdownTimeoutSecs = shutdownTimeoutSecs;
×
87
    }
×
88

89
    public void setSystemProperty(String key, String value) {
90
        System.setProperty(key, value);
×
91
    }
×
92

93
    public void setHttpListeners(HttpListenerConfig... httpListenerConfigs) {
94
        Assert.notNull(httpListenerConfigs, "httpListenerConfigs must not be null");
1✔
95
        for (HttpListenerConfig listenerConfig : httpListenerConfigs) {
1✔
96
            builder.addListener(listenerConfig.getListenerBuilder());
1✔
97
        }
98
    }
1✔
99

100
    public void setHttpsListeners(HttpsListenerConfig... httpsListenerConfigs) throws IOException {
NEW
101
        Assert.notNull(httpsListenerConfigs, "httpsListenerConfigs must not be null");
×
102
        for (HttpsListenerConfig listenerConfig : httpsListenerConfigs) {
×
103
            builder.addListener(listenerConfig.getListenerBuilder());
×
104
        }
105
    }
×
106

107
    public void setAjpListeners(AjpListenerConfig... ajpListenerConfigs) {
NEW
108
        Assert.notNull(ajpListenerConfigs, "ajpListenerConfigs must not be null");
×
109
        for (AjpListenerConfig listenerConfig : ajpListenerConfigs) {
×
110
            builder.addListener(listenerConfig.getListenerBuilder());
×
111
        }
112
    }
×
113

114
    public void setBufferSize(int bufferSize) {
115
        builder.setBufferSize(bufferSize);
×
116
    }
×
117

118
    public void setIoThreads(int ioThreads) {
119
        builder.setIoThreads(ioThreads);
×
120
    }
×
121

122
    public void setWorkerThreads(int workerThreads) {
123
        builder.setWorkerThreads(workerThreads);
×
124
    }
×
125

126
    public void setDirectBuffers(final boolean directBuffers) {
127
        builder.setDirectBuffers(directBuffers);
×
128
    }
×
129

130
    public <T> void setServerOption(final Option<T> option, final T value) {
131
        builder.setServerOption(option, value);
×
132
    }
×
133

134
    public <T> void setSocketOption(final Option<T> option, final T value) {
135
        builder.setSocketOption(option, value);
×
136
    }
×
137

138
    public <T> void setWorkerOption(final Option<T> option, final T value) {
139
        builder.setWorkerOption(option, value);
×
140
    }
×
141

142
    @SuppressWarnings({"rawtypes", "unchecked"})
143
    public void setServerOptions(TowOptions options) {
144
        if (options != null) {
1✔
145
            OptionMap optionMap = options.getOptionMap();
1✔
146
            for (Option option : optionMap) {
1✔
147
                builder.setServerOption(option, optionMap.get(option));
1✔
148
            }
1✔
149
        }
150
    }
1✔
151

152
    @SuppressWarnings({"rawtypes", "unchecked"})
153
    public void setSocketOptions(TowOptions options) {
154
        if (options != null) {
×
155
            OptionMap optionMap = options.getOptionMap();
×
156
            for (Option option : optionMap) {
×
157
                builder.setSocketOption(option, optionMap.get(option));
×
158
            }
×
159
        }
160
    }
×
161

162
    @SuppressWarnings({"rawtypes", "unchecked"})
163
    public void setWorkerOptions(TowOptions options) {
164
        if (options != null) {
1✔
165
            OptionMap optionMap = options.getOptionMap();
1✔
166
            for (Option option : optionMap) {
1✔
167
                builder.setWorkerOption(option, optionMap.get(option));
1✔
168
            }
1✔
169
        }
170
    }
1✔
171

172
    protected RequestHandlerFactory getRequestHandlerFactory() {
173
        Assert.state(requestHandlerFactory != null, "requestHandlerFactory is not set");
1✔
174
        return requestHandlerFactory;
1✔
175
    }
176

177
    public void setRequestHandlerFactory(RequestHandlerFactory requestHandlerFactory) {
178
        this.requestHandlerFactory = requestHandlerFactory;
1✔
179
    }
1✔
180

181
    protected Undertow buildServer() throws Exception {
182
        HttpHandler handler = getRequestHandlerFactory().createHandler();
1✔
183
        if (isShutdownGracefully()) {
1✔
184
            handler = new GracefulShutdownHandler(handler);
1✔
185
        }
186

187
        this.handler = handler;
1✔
188

189
        builder.setHandler(handler);
1✔
190
        return builder.build();
1✔
191
    }
192

193
    protected HttpHandler getHandler() {
194
        Assert.state(handler != null, "handler is not set");
1✔
195
        return handler;
1✔
196
    }
197

198
    protected void shutdown() throws Exception {
199
        if (getHandler() instanceof GracefulShutdownHandler shutdownHandler) {
1✔
200
            shutdownHandler.shutdown();
1✔
201
            try {
202
                if (getShutdownTimeoutSecs() > 0) {
1✔
UNCOV
203
                    boolean result = shutdownHandler.awaitShutdown(getShutdownTimeoutSecs() * 1000L);
×
204
                    if (!result) {
×
205
                        logger.warn("Undertow server did not shut down gracefully within " +
×
NEW
206
                                getShutdownTimeoutSecs() + " seconds. Proceeding with forceful shutdown");
×
207
                    }
208
                } else {
×
209
                    shutdownHandler.awaitShutdown();
1✔
210
                }
211
            } catch (Exception ex) {
×
212
                logger.error("Unable to gracefully stop Undertow server");
×
213
            }
1✔
214
        }
215
        getRequestHandlerFactory().dispose();
1✔
216
    }
1✔
217

218
    @Override
219
    public DeploymentManager getDeploymentManager(String deploymentName) {
NEW
220
        Assert.notNull(deploymentName, "deploymentName must not be null");
×
221
        return getRequestHandlerFactory().getServletContainer().getDeployment(deploymentName);
×
222
    }
223

224
    @Override
225
    public DeploymentManager getDeploymentManagerByPath(String path) {
NEW
226
        Assert.notNull(path, "path must not be null");
×
227
        return getRequestHandlerFactory().getServletContainer().getDeploymentByPath(path);
×
228
    }
229

230
    @Override
231
    public SessionHandler getSessionHandler(String deploymentName) {
232
        DeploymentManager deploymentManager = getDeploymentManager(deploymentName);
×
NEW
233
        Assert.state(deploymentManager != null, "Deployment named '" + deploymentName + "' not found");
×
234
        return getSessionHandler(deploymentManager);
×
235
    }
236

237
    @Override
238
    public SessionHandler getSessionHandlerByPath(String path) {
239
        DeploymentManager deploymentManager = getDeploymentManagerByPath(path);
×
NEW
240
        Assert.state(deploymentManager != null, "Deployment with path '\" + path + \"' not found");
×
241
        return getSessionHandler(deploymentManager);
×
242
    }
243

244
    @Nullable
245
    private SessionHandler getSessionHandler(@NonNull DeploymentManager deploymentManager) {
246
        Deployment deployment = deploymentManager.getDeployment();
×
247
        if (deployment != null) {
×
248
            SessionManager sessionManager = deployment.getSessionManager();
×
249
            if (sessionManager instanceof TowSessionManager towSessionManager) {
×
250
                return towSessionManager.getSessionHandler();
×
251
            }
252
        }
253
        return null;
×
254
    }
255

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