Skip to content

Commit d403dae

Browse files
committed
Merge pull request #16053 from juliojgd
* pr/16053: Polish "Add loadOnStartup property to EndpointServlet" Add loadOnStartup property to EndpointServlet
2 parents 873fd3f + 4f029d6 commit d403dae

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

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

+34-8
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.
@@ -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,28 +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

3840
private final Map<String, String> initParameters;
3941

42+
private final int loadOnStartup;
43+
4044
public EndpointServlet(Class<? extends Servlet> servlet) {
45+
this(instantiateClass(servlet));
46+
}
47+
48+
private static Servlet instantiateClass(Class<? extends Servlet> servlet) {
4149
Assert.notNull(servlet, "Servlet must not be null");
42-
this.servlet = BeanUtils.instantiateClass(servlet);
43-
this.initParameters = Collections.emptyMap();
50+
return BeanUtils.instantiateClass(servlet);
4451
}
4552

4653
public EndpointServlet(Servlet servlet) {
47-
Assert.notNull(servlet, "Servlet must not be null");
48-
this.servlet = servlet;
49-
this.initParameters = Collections.emptyMap();
54+
this(servlet, Collections.emptyMap(), -1);
5055
}
5156

52-
private EndpointServlet(Servlet servlet, Map<String, String> initParameters) {
57+
private EndpointServlet(Servlet servlet, Map<String, String> initParameters,
58+
int loadOnStartup) {
59+
Assert.notNull(servlet, "Servlet must not be null");
5360
this.servlet = servlet;
5461
this.initParameters = Collections.unmodifiableMap(initParameters);
62+
this.loadOnStartup = loadOnStartup;
5563
}
5664

5765
public EndpointServlet withInitParameter(String name, String value) {
@@ -67,7 +75,21 @@ public EndpointServlet withInitParameters(Map<String, String> initParameters) {
6775
Map<String, String> mergedInitParameters = new LinkedHashMap<>(
6876
this.initParameters);
6977
mergedInitParameters.putAll(initParameters);
70-
return new EndpointServlet(this.servlet, mergedInitParameters);
78+
return new EndpointServlet(this.servlet, mergedInitParameters,
79+
this.loadOnStartup);
80+
}
81+
82+
/**
83+
* Sets the {@code loadOnStartup} priority that will be set on Servlet registration.
84+
* The default value for {@code loadOnStartup} is {@code -1}.
85+
* @param loadOnStartup the initialization priority of the Servlet
86+
* @return a new instance of {@link EndpointServlet} with the provided
87+
* {@code loadOnStartup} value set
88+
* @since 2.2.0
89+
* @see ServletRegistration.Dynamic#setLoadOnStartup(int)
90+
*/
91+
public EndpointServlet withLoadOnStartup(int loadOnStartup) {
92+
return new EndpointServlet(this.servlet, this.initParameters, loadOnStartup);
7193
}
7294

7395
Servlet getServlet() {
@@ -78,4 +100,8 @@ Map<String, String> getInitParameters() {
78100
return this.initParameters;
79101
}
80102

103+
int getLoadOnStartup() {
104+
return this.loadOnStartup;
105+
}
106+
81107
}

Diff for: spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrar.java

+2-1
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.
@@ -75,6 +75,7 @@ private void register(ServletContext servletContext,
7575
endpointServlet.getServlet());
7676
registration.addMapping(urlMapping);
7777
registration.setInitParameters(endpointServlet.getInitParameters());
78+
registration.setLoadOnStartup(endpointServlet.getLoadOnStartup());
7879
logger.info("Registered '" + path + "' to " + name);
7980
}
8081

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

+14-5
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,14 +127,25 @@ 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"));
130+
}
132131

132+
@Test
133+
public void withLoadOnStartupNotSetShouldReturnDefaultValue() {
134+
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class);
135+
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(-1);
136+
}
137+
138+
@Test
139+
public void withLoadOnStartupSetShouldReturnValue() {
140+
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class)
141+
.withLoadOnStartup(3);
142+
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(3);
133143
}
134144

135145
private static class TestServlet extends GenericServlet {
136146

137147
@Override
138-
public void service(ServletRequest req, ServletResponse res)
139-
throws ServletException, IOException {
148+
public void service(ServletRequest req, ServletResponse res) {
140149
}
141150

142151
}

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

+23-1
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.
@@ -120,6 +120,28 @@ public void onStartupWhenHasInitParametersShouldRegisterInitParameters()
120120
verify(this.dynamic).setInitParameters(Collections.singletonMap("a", "b"));
121121
}
122122

123+
@Test
124+
public void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup()
125+
throws Exception {
126+
ExposableServletEndpoint endpoint = mockEndpoint(
127+
new EndpointServlet(TestServlet.class).withLoadOnStartup(7));
128+
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
129+
Collections.singleton(endpoint));
130+
registrar.onStartup(this.servletContext);
131+
verify(this.dynamic).setLoadOnStartup(7);
132+
}
133+
134+
@Test
135+
public void onStartupWhenHasNotLoadOnStartupShouldRegisterDefaultValue()
136+
throws Exception {
137+
ExposableServletEndpoint endpoint = mockEndpoint(
138+
new EndpointServlet(TestServlet.class));
139+
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
140+
Collections.singleton(endpoint));
141+
registrar.onStartup(this.servletContext);
142+
verify(this.dynamic).setLoadOnStartup(-1);
143+
}
144+
123145
private ExposableServletEndpoint mockEndpoint(EndpointServlet endpointServlet) {
124146
ExposableServletEndpoint endpoint = mock(ExposableServletEndpoint.class);
125147
given(endpoint.getEndpointId()).willReturn(EndpointId.of("test"));

0 commit comments

Comments
 (0)