Commit d403dae6 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #16053 from juliojgd

* pr/16053:
  Polish "Add loadOnStartup property to EndpointServlet"
  Add loadOnStartup property to EndpointServlet
parents 873fd3f6 4f029d6d
/* /*
* 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.
...@@ -21,6 +21,7 @@ import java.util.LinkedHashMap; ...@@ -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,6 +31,7 @@ import org.springframework.util.StringUtils; ...@@ -30,6 +31,7 @@ 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 {
...@@ -37,21 +39,27 @@ public final class EndpointServlet { ...@@ -37,21 +39,27 @@ public final class EndpointServlet {
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) {
this(instantiateClass(servlet));
}
private static Servlet instantiateClass(Class<? extends Servlet> servlet) {
Assert.notNull(servlet, "Servlet must not be null"); Assert.notNull(servlet, "Servlet must not be null");
this.servlet = BeanUtils.instantiateClass(servlet); return BeanUtils.instantiateClass(servlet);
this.initParameters = Collections.emptyMap();
} }
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();
} }
private EndpointServlet(Servlet servlet, Map<String, String> initParameters) { private EndpointServlet(Servlet servlet, Map<String, String> initParameters,
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;
} }
public EndpointServlet withInitParameter(String name, String value) { public EndpointServlet withInitParameter(String name, String value) {
...@@ -67,7 +75,21 @@ public final class EndpointServlet { ...@@ -67,7 +75,21 @@ public final class EndpointServlet {
Map<String, String> mergedInitParameters = new LinkedHashMap<>( Map<String, String> mergedInitParameters = new LinkedHashMap<>(
this.initParameters); this.initParameters);
mergedInitParameters.putAll(initParameters); mergedInitParameters.putAll(initParameters);
return new EndpointServlet(this.servlet, mergedInitParameters); return new EndpointServlet(this.servlet, mergedInitParameters,
this.loadOnStartup);
}
/**
* Sets the {@code loadOnStartup} priority that will be set on Servlet registration.
* The default value for {@code loadOnStartup} is {@code -1}.
* @param loadOnStartup the initialization priority of the Servlet
* @return a new instance of {@link EndpointServlet} with the provided
* {@code loadOnStartup} value set
* @since 2.2.0
* @see ServletRegistration.Dynamic#setLoadOnStartup(int)
*/
public EndpointServlet withLoadOnStartup(int loadOnStartup) {
return new EndpointServlet(this.servlet, this.initParameters, loadOnStartup);
} }
Servlet getServlet() { Servlet getServlet() {
...@@ -78,4 +100,8 @@ public final class EndpointServlet { ...@@ -78,4 +100,8 @@ public final class EndpointServlet {
return this.initParameters; return this.initParameters;
} }
int getLoadOnStartup() {
return this.loadOnStartup;
}
} }
/* /*
* 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.
...@@ -75,6 +75,7 @@ public class ServletEndpointRegistrar implements ServletContextInitializer { ...@@ -75,6 +75,7 @@ public class ServletEndpointRegistrar implements ServletContextInitializer {
endpointServlet.getServlet()); endpointServlet.getServlet());
registration.addMapping(urlMapping); registration.addMapping(urlMapping);
registration.setInitParameters(endpointServlet.getInitParameters()); registration.setInitParameters(endpointServlet.getInitParameters());
registration.setLoadOnStartup(endpointServlet.getLoadOnStartup());
logger.info("Registered '" + path + "' to " + name); logger.info("Registered '" + path + "' to " + name);
} }
......
/* /*
* 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 @@ ...@@ -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,14 +127,25 @@ public class EndpointServletTests { ...@@ -129,14 +127,25 @@ 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
public void withLoadOnStartupNotSetShouldReturnDefaultValue() {
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class);
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(-1);
}
@Test
public void withLoadOnStartupSetShouldReturnValue() {
EndpointServlet endpointServlet = new EndpointServlet(TestServlet.class)
.withLoadOnStartup(3);
assertThat(endpointServlet.getLoadOnStartup()).isEqualTo(3);
} }
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 {
} }
} }
......
/* /*
* 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.
...@@ -120,6 +120,28 @@ public class ServletEndpointRegistrarTests { ...@@ -120,6 +120,28 @@ public class ServletEndpointRegistrarTests {
verify(this.dynamic).setInitParameters(Collections.singletonMap("a", "b")); verify(this.dynamic).setInitParameters(Collections.singletonMap("a", "b"));
} }
@Test
public void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup()
throws Exception {
ExposableServletEndpoint endpoint = mockEndpoint(
new EndpointServlet(TestServlet.class).withLoadOnStartup(7));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
Collections.singleton(endpoint));
registrar.onStartup(this.servletContext);
verify(this.dynamic).setLoadOnStartup(7);
}
@Test
public void onStartupWhenHasNotLoadOnStartupShouldRegisterDefaultValue()
throws Exception {
ExposableServletEndpoint endpoint = mockEndpoint(
new EndpointServlet(TestServlet.class));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
Collections.singleton(endpoint));
registrar.onStartup(this.servletContext);
verify(this.dynamic).setLoadOnStartup(-1);
}
private ExposableServletEndpoint mockEndpoint(EndpointServlet endpointServlet) { private ExposableServletEndpoint mockEndpoint(EndpointServlet endpointServlet) {
ExposableServletEndpoint endpoint = mock(ExposableServletEndpoint.class); ExposableServletEndpoint endpoint = mock(ExposableServletEndpoint.class);
given(endpoint.getEndpointId()).willReturn(EndpointId.of("test")); given(endpoint.getEndpointId()).willReturn(EndpointId.of("test"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment