Skip to content

Commit b99c053

Browse files
Gómez Díaz, Julio Josésnicoll
Gómez Díaz, Julio José
authored andcommitted
Add loadOnStartup property to EndpointServlet
loadOnStartup property was missing from EndpointServlet and cannot be set inside ServletEndpointRegistrar. Now it can be set and register a Servlet with that integer property ready to act upon registration. See gh-16053
1 parent 873fd3f commit b99c053

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

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

+31-3
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.
@@ -35,23 +35,31 @@ public final class EndpointServlet {
3535

3636
private final Servlet servlet;
3737

38+
private final int loadOnStartup;
39+
3840
private final Map<String, String> initParameters;
3941

4042
public EndpointServlet(Class<? extends Servlet> servlet) {
4143
Assert.notNull(servlet, "Servlet must not be null");
4244
this.servlet = BeanUtils.instantiateClass(servlet);
4345
this.initParameters = Collections.emptyMap();
46+
this.loadOnStartup = -1;
47+
4448
}
4549

4650
public EndpointServlet(Servlet servlet) {
4751
Assert.notNull(servlet, "Servlet must not be null");
4852
this.servlet = servlet;
4953
this.initParameters = Collections.emptyMap();
54+
this.loadOnStartup = -1;
5055
}
5156

52-
private EndpointServlet(Servlet servlet, Map<String, String> initParameters) {
57+
private EndpointServlet(Servlet servlet, Map<String, String> initParameters,
58+
int loadOnStartup) {
5359
this.servlet = servlet;
5460
this.initParameters = Collections.unmodifiableMap(initParameters);
61+
this.loadOnStartup = loadOnStartup;
62+
5563
}
5664

5765
public EndpointServlet withInitParameter(String name, String value) {
@@ -67,7 +75,23 @@ 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</code> priority that will be set on Servlet
84+
* registration
85+
* <p>
86+
* The default value for <tt>loadOnStartup</tt> is <code>-1</code>.
87+
* @param loadOnStartup the initialization priority of the Servlet
88+
* @return a new instance of {@link EndpointServlet} with the provided
89+
* <tt>loadOnStartup</tt> value set
90+
* @since 2.2.0
91+
* @see ServletRegistration.Dynamic#setLoadOnStartup(int)
92+
*/
93+
public EndpointServlet withLoadOnStartup(int loadOnStartup) {
94+
return new EndpointServlet(this.servlet, this.initParameters, loadOnStartup);
7195
}
7296

7397
Servlet getServlet() {
@@ -78,4 +102,8 @@ Map<String, String> getInitParameters() {
78102
return this.initParameters;
79103
}
80104

105+
int getLoadOnStartup() {
106+
return this.loadOnStartup;
107+
}
108+
81109
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,32 @@ public void withInitParametersWhenHasExistingShouldMergeParameters() {
132132

133133
}
134134

135+
@Test
136+
public void withLoadOnStartupNotSetShouldReturnDefaultValue() {
137+
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class);
138+
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(-1);
139+
}
140+
141+
@Test
142+
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;
152+
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);
159+
}
160+
135161
private static class TestServlet extends GenericServlet {
136162

137163
@Override

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

+24
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,30 @@ public void onStartupWhenHasInitParametersShouldRegisterInitParameters()
120120
verify(this.dynamic).setInitParameters(Collections.singletonMap("a", "b"));
121121
}
122122

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

0 commit comments

Comments
 (0)