Skip to content

Commit 1238146

Browse files
committed
Polish
1 parent 4d521e7 commit 1238146

File tree

23 files changed

+775
-517
lines changed

23 files changed

+775
-517
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.autoconfigure.web.ErrorProperties;
2222
import org.springframework.boot.web.error.ErrorAttributeOptions;
23+
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
2324
import org.springframework.boot.web.servlet.error.ErrorAttributes;
2425
import org.springframework.boot.web.servlet.error.ErrorController;
2526
import org.springframework.stereotype.Controller;
@@ -60,16 +61,16 @@ public Map<String, Object> invoke(ServletWebRequest request) {
6061
private ErrorAttributeOptions getErrorAttributeOptions(ServletWebRequest request) {
6162
ErrorAttributeOptions options = ErrorAttributeOptions.defaults();
6263
if (this.errorProperties.isIncludeException()) {
63-
options = options.including(ErrorAttributeOptions.Include.EXCEPTION);
64+
options = options.including(Include.EXCEPTION);
6465
}
6566
if (includeStackTrace(request)) {
66-
options = options.including(ErrorAttributeOptions.Include.STACK_TRACE);
67+
options = options.including(Include.STACK_TRACE);
6768
}
6869
if (includeMessage(request)) {
69-
options = options.including(ErrorAttributeOptions.Include.MESSAGE);
70+
options = options.including(Include.MESSAGE);
7071
}
7172
if (includeBindingErrors(request)) {
72-
options = options.including(ErrorAttributeOptions.Include.BINDING_ERRORS);
73+
options = options.including(Include.BINDING_ERRORS);
7374
}
7475
return options;
7576
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpointTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.web.servlet;
1818

19-
import java.util.HashMap;
19+
import java.util.Collections;
2020
import java.util.Map;
2121

2222
import org.junit.jupiter.api.BeforeEach;
@@ -30,6 +30,7 @@
3030
import org.springframework.web.context.request.WebRequest;
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.entry;
3334

3435
/**
3536
* Tests for {@link ManagementErrorEndpoint}.
@@ -108,27 +109,27 @@ void errorResponseParamsFalse() {
108109
@Test
109110
void errorResponseWithCustomErrorAttributesUsingDeprecatedApi() {
110111
ErrorAttributes attributes = new ErrorAttributes() {
112+
111113
@Override
112114
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
113-
Map<String, Object> response = new HashMap<>();
114-
response.put("message", "An error occurred");
115-
return response;
115+
return Collections.singletonMap("message", "An error occurred");
116116
}
117117

118118
@Override
119119
public Throwable getError(WebRequest webRequest) {
120120
return null;
121121
}
122+
122123
};
123124
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
124125
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
125-
assertThat(response).hasSize(1);
126-
assertThat(response).containsEntry("message", "An error occurred");
126+
assertThat(response).containsExactly(entry("message", "An error occurred"));
127127
}
128128

129129
@Test
130130
void errorResponseWithDefaultErrorAttributesSubclassUsingDeprecatedApiAndDelegation() {
131131
ErrorAttributes attributes = new DefaultErrorAttributes() {
132+
132133
@Override
133134
@SuppressWarnings("deprecation")
134135
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
@@ -138,6 +139,7 @@ public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean inc
138139
response.remove("path");
139140
return response;
140141
}
142+
141143
};
142144
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
143145
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
@@ -150,18 +152,16 @@ public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean inc
150152
@Test
151153
void errorResponseWithDefaultErrorAttributesSubclassUsingDeprecatedApiWithoutDelegation() {
152154
ErrorAttributes attributes = new DefaultErrorAttributes() {
155+
153156
@Override
154-
@SuppressWarnings("deprecation")
155157
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
156-
Map<String, Object> response = new HashMap<>();
157-
response.put("error", "custom error");
158-
return response;
158+
return Collections.singletonMap("error", "custom error");
159159
}
160+
160161
};
161162
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
162163
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
163-
assertThat(response).hasSize(1);
164-
assertThat(response).containsEntry("error", "custom error");
164+
assertThat(response).containsExactly(entry("error", "custom error"));
165165
}
166166

167167
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/transport/RemoteHttpClientTransport.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,21 @@ static RemoteHttpClientTransport createIfPossible(Environment environment) {
5656

5757
static RemoteHttpClientTransport createIfPossible(Environment environment, SslContextFactory sslContextFactory) {
5858
String host = environment.get(DOCKER_HOST);
59-
if (host == null || Files.exists(Paths.get(host))) {
59+
if (host == null || isLocalFileReference(host)) {
6060
return null;
6161
}
6262
return create(environment, sslContextFactory, HttpHost.create(host));
6363
}
6464

65+
private static boolean isLocalFileReference(String host) {
66+
try {
67+
return Files.exists(Paths.get(host));
68+
}
69+
catch (Exception ex) {
70+
return false;
71+
}
72+
}
73+
6574
private static RemoteHttpClientTransport create(Environment environment, SslContextFactory sslContextFactory,
6675
HttpHost tcpHost) {
6776
HttpClientBuilder builder = HttpClients.custom();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
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+
* https://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+
17+
package org.springframework.boot.web.embedded.jetty;
18+
19+
import java.util.concurrent.ExecutionException;
20+
import java.util.function.Supplier;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
import org.eclipse.jetty.server.Connector;
25+
import org.eclipse.jetty.server.Server;
26+
27+
import org.springframework.boot.web.server.GracefulShutdownCallback;
28+
import org.springframework.boot.web.server.GracefulShutdownResult;
29+
import org.springframework.core.log.LogMessage;
30+
31+
/**
32+
* Handles Jetty graceful shutdown.
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
final class GracefulShutdown {
37+
38+
private static final Log logger = LogFactory.getLog(JettyWebServer.class);
39+
40+
private final Server server;
41+
42+
private final Supplier<Integer> activeRequests;
43+
44+
private volatile boolean shuttingDown = false;
45+
46+
GracefulShutdown(Server server, Supplier<Integer> activeRequests) {
47+
this.server = server;
48+
this.activeRequests = activeRequests;
49+
}
50+
51+
void shutDownGracefully(GracefulShutdownCallback callback) {
52+
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
53+
for (Connector connector : this.server.getConnectors()) {
54+
shutdown(connector);
55+
}
56+
this.shuttingDown = true;
57+
new Thread(() -> awaitShutdown(callback), "jetty-shutdown").start();
58+
59+
}
60+
61+
private void shutdown(Connector connector) {
62+
try {
63+
connector.shutdown().get();
64+
}
65+
catch (InterruptedException ex) {
66+
Thread.currentThread().interrupt();
67+
}
68+
catch (ExecutionException ex) {
69+
// Continue
70+
}
71+
}
72+
73+
private void awaitShutdown(GracefulShutdownCallback callback) {
74+
while (this.shuttingDown && this.activeRequests.get() > 0) {
75+
sleep(100);
76+
}
77+
this.shuttingDown = false;
78+
long activeRequests = this.activeRequests.get();
79+
if (activeRequests == 0) {
80+
logger.info("Graceful shutdown complete");
81+
callback.shutdownComplete(GracefulShutdownResult.IDLE);
82+
}
83+
else {
84+
logger.info(LogMessage.format("Graceful shutdown aborted with %d request(s) still active", activeRequests));
85+
callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE);
86+
}
87+
}
88+
89+
private void sleep(long millis) {
90+
try {
91+
Thread.sleep(millis);
92+
}
93+
catch (InterruptedException ex) {
94+
Thread.currentThread().interrupt();
95+
}
96+
}
97+
98+
void abort() {
99+
this.shuttingDown = false;
100+
}
101+
102+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222
import java.util.Objects;
23-
import java.util.concurrent.ExecutionException;
24-
import java.util.function.Supplier;
2523
import java.util.stream.Collectors;
2624

2725
import org.apache.commons.logging.Log;
@@ -286,12 +284,11 @@ public int getPort() {
286284

287285
@Override
288286
public void shutDownGracefully(GracefulShutdownCallback callback) {
289-
if (this.gracefulShutdown != null) {
290-
this.gracefulShutdown.shutDownGracefully(callback);
291-
}
292-
else {
287+
if (this.gracefulShutdown == null) {
293288
callback.shutdownComplete(GracefulShutdownResult.IMMEDIATE);
289+
return;
294290
}
291+
this.gracefulShutdown.shutDownGracefully(callback);
295292
}
296293

297294
/**
@@ -302,68 +299,4 @@ public Server getServer() {
302299
return this.server;
303300
}
304301

305-
static final class GracefulShutdown {
306-
307-
private static final Log logger = LogFactory.getLog(GracefulShutdown.class);
308-
309-
private final Server server;
310-
311-
private final Supplier<Integer> activeRequests;
312-
313-
private volatile boolean shuttingDown = false;
314-
315-
private GracefulShutdown(Server server, Supplier<Integer> activeRequests) {
316-
this.server = server;
317-
this.activeRequests = activeRequests;
318-
}
319-
320-
private void shutDownGracefully(GracefulShutdownCallback callback) {
321-
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
322-
for (Connector connector : this.server.getConnectors()) {
323-
shutdown(connector);
324-
}
325-
this.shuttingDown = true;
326-
new Thread(() -> {
327-
while (this.shuttingDown && this.activeRequests.get() > 0) {
328-
try {
329-
Thread.sleep(100);
330-
}
331-
catch (InterruptedException ex) {
332-
Thread.currentThread().interrupt();
333-
}
334-
}
335-
this.shuttingDown = false;
336-
long activeRequests = this.activeRequests.get();
337-
if (activeRequests == 0) {
338-
logger.info("Graceful shutdown complete");
339-
callback.shutdownComplete(GracefulShutdownResult.IDLE);
340-
}
341-
else {
342-
if (logger.isInfoEnabled()) {
343-
logger.info("Graceful shutdown aborted with " + activeRequests + " request(s) still active");
344-
}
345-
callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE);
346-
}
347-
}, "jetty-shutdown").start();
348-
349-
}
350-
351-
private void shutdown(Connector connector) {
352-
try {
353-
connector.shutdown().get();
354-
}
355-
catch (InterruptedException ex) {
356-
Thread.currentThread().interrupt();
357-
}
358-
catch (ExecutionException ex) {
359-
// Continue
360-
}
361-
}
362-
363-
private void abort() {
364-
this.shuttingDown = false;
365-
}
366-
367-
}
368-
369302
}

0 commit comments

Comments
 (0)