Polish "Add loadOnStartup property to EndpointServlet"

Closes gh-16053
pull/16106/head
Stephane Nicoll 6 years ago
parent b99c05329f
commit 4f029d6df9

@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletRegistration;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -30,36 +31,35 @@ import org.springframework.util.StringUtils;
* Contains details of a servlet that is exposed as an actuator endpoint. * Contains details of a servlet that is exposed as an actuator endpoint.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Julio José Gómez Díaz
*/ */
public final class EndpointServlet { public final class EndpointServlet {
private final Servlet servlet; private final Servlet servlet;
private final int loadOnStartup;
private final Map<String, String> initParameters; private final Map<String, String> initParameters;
private final int loadOnStartup;
public EndpointServlet(Class<? extends Servlet> servlet) { public EndpointServlet(Class<? extends Servlet> servlet) {
Assert.notNull(servlet, "Servlet must not be null"); this(instantiateClass(servlet));
this.servlet = BeanUtils.instantiateClass(servlet); }
this.initParameters = Collections.emptyMap();
this.loadOnStartup = -1;
private static Servlet instantiateClass(Class<? extends Servlet> servlet) {
Assert.notNull(servlet, "Servlet must not be null");
return BeanUtils.instantiateClass(servlet);
} }
public EndpointServlet(Servlet servlet) { public EndpointServlet(Servlet servlet) {
Assert.notNull(servlet, "Servlet must not be null"); this(servlet, Collections.emptyMap(), -1);
this.servlet = servlet;
this.initParameters = Collections.emptyMap();
this.loadOnStartup = -1;
} }
private EndpointServlet(Servlet servlet, Map<String, String> initParameters, private EndpointServlet(Servlet servlet, Map<String, String> initParameters,
int loadOnStartup) { int loadOnStartup) {
Assert.notNull(servlet, "Servlet must not be null");
this.servlet = servlet; this.servlet = servlet;
this.initParameters = Collections.unmodifiableMap(initParameters); this.initParameters = Collections.unmodifiableMap(initParameters);
this.loadOnStartup = loadOnStartup; this.loadOnStartup = loadOnStartup;
} }
public EndpointServlet withInitParameter(String name, String value) { public EndpointServlet withInitParameter(String name, String value) {
@ -80,13 +80,11 @@ public final class EndpointServlet {
} }
/** /**
* Sets the <code>loadOnStartup</code> priority that will be set on Servlet * Sets the {@code loadOnStartup} priority that will be set on Servlet registration.
* registration * The default value for {@code loadOnStartup} is {@code -1}.
* <p>
* The default value for <tt>loadOnStartup</tt> is <code>-1</code>.
* @param loadOnStartup the initialization priority of the Servlet * @param loadOnStartup the initialization priority of the Servlet
* @return a new instance of {@link EndpointServlet} with the provided * @return a new instance of {@link EndpointServlet} with the provided
* <tt>loadOnStartup</tt> value set * {@code loadOnStartup} value set
* @since 2.2.0 * @since 2.2.0
* @see ServletRegistration.Dynamic#setLoadOnStartup(int) * @see ServletRegistration.Dynamic#setLoadOnStartup(int)
*/ */

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,12 @@
package org.springframework.boot.actuate.endpoint.web; package org.springframework.boot.actuate.endpoint.web;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.GenericServlet; import javax.servlet.GenericServlet;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
@ -129,7 +127,6 @@ public class EndpointServletTests {
extra.put("e", "f"); extra.put("e", "f");
assertThat(endpointServlet.withInitParameters(extra).getInitParameters()) assertThat(endpointServlet.withInitParameters(extra).getInitParameters())
.containsExactly(entry("a", "b1"), entry("c", "d"), entry("e", "f")); .containsExactly(entry("a", "b1"), entry("c", "d"), entry("e", "f"));
} }
@Test @Test
@ -140,29 +137,15 @@ public class EndpointServletTests {
@Test @Test
public void withLoadOnStartupSetShouldReturnValue() { public void withLoadOnStartupSetShouldReturnValue() {
final int loadOnStartupTestValue = 3;
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class)
.withLoadOnStartup(loadOnStartupTestValue);
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(loadOnStartupTestValue);
}
@Test
public void withLoadOnStartupAndInitParamsShouldReturnValue() {
final int loadOnStartupTestValue = 9;
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class) EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class)
.withLoadOnStartup(loadOnStartupTestValue).withInitParameter("a", "b") .withLoadOnStartup(3);
.withInitParameter("c", "d"); assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(3);
Map<String, String> extra = new LinkedHashMap<>();
extra.put("a", "b1");
extra.put("e", "f");
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(loadOnStartupTestValue);
} }
private static class TestServlet extends GenericServlet { private static class TestServlet extends GenericServlet {
@Override @Override
public void service(ServletRequest req, ServletResponse res) public void service(ServletRequest req, ServletResponse res) {
throws ServletException, IOException {
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -123,14 +123,12 @@ public class ServletEndpointRegistrarTests {
@Test @Test
public void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup() public void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup()
throws Exception { throws Exception {
final int loadOnStartupTestValue = 7;
ExposableServletEndpoint endpoint = mockEndpoint( ExposableServletEndpoint endpoint = mockEndpoint(
new EndpointServlet(TestServlet.class) new EndpointServlet(TestServlet.class).withLoadOnStartup(7));
.withLoadOnStartup(loadOnStartupTestValue));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
Collections.singleton(endpoint)); Collections.singleton(endpoint));
registrar.onStartup(this.servletContext); registrar.onStartup(this.servletContext);
verify(this.dynamic).setLoadOnStartup(loadOnStartupTestValue); verify(this.dynamic).setLoadOnStartup(7);
} }
@Test @Test

Loading…
Cancel
Save