Commit 451acb56 authored by Dave Syer's avatar Dave Syer Committed by Phillip Webb

Move JolokiaEndpoint so it is not an Endpoint

parent 7f1264bb
...@@ -20,7 +20,8 @@ import java.util.Map; ...@@ -20,7 +20,8 @@ import java.util.Map;
import org.jolokia.http.AgentServlet; import org.jolokia.http.AgentServlet;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.JolokiaEndpoint; import org.springframework.boot.actuate.endpoint.mvc.JolokiaMvcEndpoint;
import org.springframework.boot.actuate.properties.ManagementServerProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
...@@ -65,6 +66,9 @@ public class JolokiaAutoConfiguration { ...@@ -65,6 +66,9 @@ public class JolokiaAutoConfiguration {
private RelaxedPropertyResolver environment; private RelaxedPropertyResolver environment;
@Autowired
private ManagementServerProperties management;
@Autowired @Autowired
public void setEnvironment(Environment environment) { public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment); this.environment = new RelaxedPropertyResolver(environment);
...@@ -77,19 +81,17 @@ public class JolokiaAutoConfiguration { ...@@ -77,19 +81,17 @@ public class JolokiaAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean() public ServletRegistrationBean jolokiaServletRegistration(AgentServlet servlet) {
public ServletRegistrationBean jolokiaServletRegistration() { ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet,
ServletRegistrationBean registrationBean = new ServletRegistrationBean( this.management.getContextPath() + jolokiaEndpoint().getPath() + "/*");
jolokiaServlet(), this.environment.getProperty("endpoints.jolokia.path",
"/jolokia") + "/*");
addInitParameters(registrationBean); addInitParameters(registrationBean);
return registrationBean; return registrationBean;
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public JolokiaEndpoint jolokiaEndpoint() { public JolokiaMvcEndpoint jolokiaEndpoint() {
return new JolokiaEndpoint(); return new JolokiaMvcEndpoint();
} }
protected void addInitParameters(ServletRegistrationBean registrationBean) { protected void addInitParameters(ServletRegistrationBean registrationBean) {
......
...@@ -27,6 +27,7 @@ import javax.servlet.Filter; ...@@ -27,6 +27,7 @@ import javax.servlet.Filter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.properties.ManagementServerProperties; import org.springframework.boot.actuate.properties.ManagementServerProperties;
import org.springframework.boot.actuate.web.ErrorController; import org.springframework.boot.actuate.web.ErrorController;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
...@@ -211,9 +212,9 @@ public class ManagementSecurityAutoConfiguration { ...@@ -211,9 +212,9 @@ public class ManagementSecurityAutoConfiguration {
return NO_PATHS; return NO_PATHS;
} }
Set<? extends Endpoint<?>> endpoints = endpointHandlerMapping.getEndpoints(); Set<? extends MvcEndpoint> endpoints = endpointHandlerMapping.getEndpoints();
List<String> paths = new ArrayList<String>(endpoints.size()); List<String> paths = new ArrayList<String>(endpoints.size());
for (Endpoint<?> endpoint : endpoints) { for (MvcEndpoint endpoint : endpoints) {
if (endpoint.isSensitive() == secure) { if (endpoint.isSensitive() == secure) {
paths.add(endpoint.getPath()); paths.add(endpoint.getPath());
} }
......
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.endpoint;
/**
* {@link RuntimeException} indicating an {@link Endpoint} implementation is not enabled.
*
* @author Christian Dupuis
*/
public class EndpointDisabledException extends RuntimeException {
}
...@@ -159,7 +159,7 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme ...@@ -159,7 +159,7 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme
/** /**
* Return the endpoints * Return the endpoints
*/ */
public Set<? extends Endpoint<?>> getEndpoints() { public Set<? extends MvcEndpoint> getEndpoints() {
return this.endpoints; return this.endpoints;
} }
} }
...@@ -14,8 +14,12 @@ ...@@ -14,8 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.endpoint; package org.springframework.boot.actuate.endpoint.mvc;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
...@@ -25,14 +29,48 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -25,14 +29,48 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @author Christian Dupuis * @author Christian Dupuis
*/ */
@ConfigurationProperties(name = "endpoints.jolokia", ignoreUnknownFields = false) @ConfigurationProperties(name = "endpoints.jolokia", ignoreUnknownFields = false)
public class JolokiaEndpoint extends AbstractEndpoint<String> { public class JolokiaMvcEndpoint implements MvcEndpoint {
@NotNull
@Pattern(regexp = "/[^/]*", message = "Path must start with /")
private String path;
private boolean sensitive;
private boolean enabled = true;
public JolokiaMvcEndpoint() {
this.path = "/jolokia";
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public boolean isSensitive() {
return this.sensitive;
}
public JolokiaEndpoint() { public void setSensitive(boolean sensitive) {
super("/jolokia"); this.sensitive = sensitive;
} }
@Override @Override
public String invoke() { public Class<?> getEndpointType() {
return null; return null;
} }
......
...@@ -27,7 +27,11 @@ import org.springframework.boot.actuate.endpoint.Endpoint; ...@@ -27,7 +27,11 @@ import org.springframework.boot.actuate.endpoint.Endpoint;
* *
* @author Dave Syer * @author Dave Syer
*/ */
public interface MvcEndpoint extends Endpoint<Object> { public interface MvcEndpoint {
String getPath();
boolean isSensitive();
Class<?> getEndpointType(); Class<?> getEndpointType();
......
...@@ -60,7 +60,9 @@ public class JolokiaAutoConfigurationTests { ...@@ -60,7 +60,9 @@ public class JolokiaAutoConfigurationTests {
public void agentServletRegisteredWithAppContext() throws Exception { public void agentServletRegisteredWithAppContext() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class); ManagementServerPropertiesAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
JolokiaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(AgentServlet.class).length); assertEquals(1, this.context.getBeanNamesForType(AgentServlet.class).length);
} }
...@@ -70,7 +72,9 @@ public class JolokiaAutoConfigurationTests { ...@@ -70,7 +72,9 @@ public class JolokiaAutoConfigurationTests {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
TestUtils.addEnviroment(this.context, "endpoints.jolokia.enabled:false"); TestUtils.addEnviroment(this.context, "endpoints.jolokia.enabled:false");
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class); ManagementServerPropertiesAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
JolokiaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(0, this.context.getBeanNamesForType(AgentServlet.class).length); assertEquals(0, this.context.getBeanNamesForType(AgentServlet.class).length);
} }
...@@ -79,7 +83,9 @@ public class JolokiaAutoConfigurationTests { ...@@ -79,7 +83,9 @@ public class JolokiaAutoConfigurationTests {
public void agentServletRegisteredWithServletContainer() throws Exception { public void agentServletRegisteredWithServletContainer() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class); ManagementServerPropertiesAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
JolokiaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Servlet servlet = null; Servlet servlet = null;
......
...@@ -14,28 +14,50 @@ ...@@ -14,28 +14,50 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.endpoint; package org.springframework.boot.actuate.endpoint.mvc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
import org.springframework.boot.actuate.endpoint.mvc.JolokiaEndpointTests.Config;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.junit.Assert.assertEquals;
/** /**
* @author Christian Dupuis * @author Christian Dupuis
* @author Dave Syer
*/ */
public class JolokiaEndpointTests extends AbstractEndpointTests<JolokiaEndpoint> { @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Config.class })
@WebAppConfiguration
public class JolokiaEndpointTests {
@Autowired
private MvcEndpoints endpoints;
public JolokiaEndpointTests() { @Test
super(Config.class, JolokiaEndpoint.class, "/jolokia", true, "endpoints.jolokia"); public void endpointRegistered() throws Exception {
assertEquals(1, this.endpoints.getEndpoints().size());
} }
@Configuration @Configuration
@EnableConfigurationProperties @EnableConfigurationProperties
@EnableWebMvc
@Import(EndpointWebMvcAutoConfiguration.class)
public static class Config { public static class Config {
@Bean @Bean
public JolokiaEndpoint endpoint() { public JolokiaMvcEndpoint endpoint() {
return new JolokiaEndpoint(); return new JolokiaMvcEndpoint();
} }
} }
......
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