Skip to content

Commit 4f029d6

Browse files
committed
Polish "Add loadOnStartup property to EndpointServlet"
Closes gh-16053
1 parent b99c053 commit 4f029d6

File tree

3 files changed

+21
-42
lines changed

3 files changed

+21
-42
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/EndpointServlet.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222

2323
import javax.servlet.Servlet;
24+
import javax.servlet.ServletRegistration;
2425

2526
import org.springframework.beans.BeanUtils;
2627
import org.springframework.util.Assert;
@@ -30,36 +31,35 @@
3031
* Contains details of a servlet that is exposed as an actuator endpoint.
3132
*
3233
* @author Phillip Webb
34+
* @author Julio José Gómez Díaz
3335
*/
3436
public final class EndpointServlet {
3537

3638
private final Servlet servlet;
3739

38-
private final int loadOnStartup;
39-
4040
private final Map<String, String> initParameters;
4141

42+
private final int loadOnStartup;
43+
4244
public EndpointServlet(Class<? extends Servlet> servlet) {
43-
Assert.notNull(servlet, "Servlet must not be null");
44-
this.servlet = BeanUtils.instantiateClass(servlet);
45-
this.initParameters = Collections.emptyMap();
46-
this.loadOnStartup = -1;
45+
this(instantiateClass(servlet));
46+
}
4747

48+
private static Servlet instantiateClass(Class<? extends Servlet> servlet) {
49+
Assert.notNull(servlet, "Servlet must not be null");
50+
return BeanUtils.instantiateClass(servlet);
4851
}
4952

5053
public EndpointServlet(Servlet servlet) {
51-
Assert.notNull(servlet, "Servlet must not be null");
52-
this.servlet = servlet;
53-
this.initParameters = Collections.emptyMap();
54-
this.loadOnStartup = -1;
54+
this(servlet, Collections.emptyMap(), -1);
5555
}
5656

5757
private EndpointServlet(Servlet servlet, Map<String, String> initParameters,
5858
int loadOnStartup) {
59+
Assert.notNull(servlet, "Servlet must not be null");
5960
this.servlet = servlet;
6061
this.initParameters = Collections.unmodifiableMap(initParameters);
6162
this.loadOnStartup = loadOnStartup;
62-
6363
}
6464

6565
public EndpointServlet withInitParameter(String name, String value) {
@@ -80,13 +80,11 @@ public EndpointServlet withInitParameters(Map<String, String> initParameters) {
8080
}
8181

8282
/**
83-
* Sets the <code>loadOnStartup</code> priority that will be set on Servlet
84-
* registration
85-
* <p>
86-
* The default value for <tt>loadOnStartup</tt> is <code>-1</code>.
83+
* Sets the {@code loadOnStartup} priority that will be set on Servlet registration.
84+
* The default value for {@code loadOnStartup} is {@code -1}.
8785
* @param loadOnStartup the initialization priority of the Servlet
8886
* @return a new instance of {@link EndpointServlet} with the provided
89-
* <tt>loadOnStartup</tt> value set
87+
* {@code loadOnStartup} value set
9088
* @since 2.2.0
9189
* @see ServletRegistration.Dynamic#setLoadOnStartup(int)
9290
*/

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/EndpointServletTests.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,12 @@
1616

1717
package org.springframework.boot.actuate.endpoint.web;
1818

19-
import java.io.IOException;
2019
import java.util.Collections;
2120
import java.util.LinkedHashMap;
2221
import java.util.Map;
2322

2423
import javax.servlet.GenericServlet;
2524
import javax.servlet.Servlet;
26-
import javax.servlet.ServletException;
2725
import javax.servlet.ServletRequest;
2826
import javax.servlet.ServletResponse;
2927

@@ -129,7 +127,6 @@ public void withInitParametersWhenHasExistingShouldMergeParameters() {
129127
extra.put("e", "f");
130128
assertThat(endpointServlet.withInitParameters(extra).getInitParameters())
131129
.containsExactly(entry("a", "b1"), entry("c", "d"), entry("e", "f"));
132-
133130
}
134131

135132
@Test
@@ -140,29 +137,15 @@ public void withLoadOnStartupNotSetShouldReturnDefaultValue() {
140137

141138
@Test
142139
public void withLoadOnStartupSetShouldReturnValue() {
143-
final int loadOnStartupTestValue = 3;
144-
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class)
145-
.withLoadOnStartup(loadOnStartupTestValue);
146-
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(loadOnStartupTestValue);
147-
}
148-
149-
@Test
150-
public void withLoadOnStartupAndInitParamsShouldReturnValue() {
151-
final int loadOnStartupTestValue = 9;
152140
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class)
153-
.withLoadOnStartup(loadOnStartupTestValue).withInitParameter("a", "b")
154-
.withInitParameter("c", "d");
155-
Map<String, String> extra = new LinkedHashMap<>();
156-
extra.put("a", "b1");
157-
extra.put("e", "f");
158-
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(loadOnStartupTestValue);
141+
.withLoadOnStartup(3);
142+
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(3);
159143
}
160144

161145
private static class TestServlet extends GenericServlet {
162146

163147
@Override
164-
public void service(ServletRequest req, ServletResponse res)
165-
throws ServletException, IOException {
148+
public void service(ServletRequest req, ServletResponse res) {
166149
}
167150

168151
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrarTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,14 +123,12 @@ public void onStartupWhenHasInitParametersShouldRegisterInitParameters()
123123
@Test
124124
public void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup()
125125
throws Exception {
126-
final int loadOnStartupTestValue = 7;
127126
ExposableServletEndpoint endpoint = mockEndpoint(
128-
new EndpointServlet(TestServlet.class)
129-
.withLoadOnStartup(loadOnStartupTestValue));
127+
new EndpointServlet(TestServlet.class).withLoadOnStartup(7));
130128
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
131129
Collections.singleton(endpoint));
132130
registrar.onStartup(this.servletContext);
133-
verify(this.dynamic).setLoadOnStartup(loadOnStartupTestValue);
131+
verify(this.dynamic).setLoadOnStartup(7);
134132
}
135133

136134
@Test

0 commit comments

Comments
 (0)