17 changed files with 336 additions and 190 deletions
@ -1,34 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-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.mvc; |
|
||||||
|
|
||||||
import java.lang.annotation.ElementType; |
|
||||||
import java.lang.annotation.Retention; |
|
||||||
import java.lang.annotation.RetentionPolicy; |
|
||||||
import java.lang.annotation.Target; |
|
||||||
|
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
/** |
|
||||||
* @author Dave Syer |
|
||||||
*/ |
|
||||||
@Component |
|
||||||
@Retention(RetentionPolicy.RUNTIME) |
|
||||||
@Target(ElementType.TYPE) |
|
||||||
public @interface FrameworkEndpoint { |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,88 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-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.mvc; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import org.springframework.beans.BeansException; |
||||||
|
import org.springframework.beans.factory.InitializingBean; |
||||||
|
import org.springframework.boot.actuate.endpoint.Endpoint; |
||||||
|
import org.springframework.context.ApplicationContext; |
||||||
|
import org.springframework.context.ApplicationContextAware; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
/** |
||||||
|
* A registry for all {@link MvcEndpoint} beans, and a factory for a set of generic ones |
||||||
|
* wrapping existing {@link Endpoint} instances that are not already exposed as MVC |
||||||
|
* endpoints. |
||||||
|
* |
||||||
|
* @author Dave Syer |
||||||
|
*/ |
||||||
|
@Component |
||||||
|
public class MvcEndpoints implements ApplicationContextAware, InitializingBean { |
||||||
|
|
||||||
|
private ApplicationContext applicationContext; |
||||||
|
|
||||||
|
private Set<MvcEndpoint> endpoints = new HashSet<MvcEndpoint>(); |
||||||
|
|
||||||
|
private Set<Class<?>> customTypes; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) |
||||||
|
throws BeansException { |
||||||
|
this.applicationContext = applicationContext; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void afterPropertiesSet() throws Exception { |
||||||
|
Collection<MvcEndpoint> existing = this.applicationContext.getBeansOfType( |
||||||
|
MvcEndpoint.class).values(); |
||||||
|
this.endpoints.addAll(existing); |
||||||
|
this.customTypes = findEndpointClasses(existing); |
||||||
|
@SuppressWarnings("rawtypes") |
||||||
|
Collection<Endpoint> delegates = this.applicationContext.getBeansOfType( |
||||||
|
Endpoint.class).values(); |
||||||
|
for (Endpoint<?> endpoint : delegates) { |
||||||
|
if (isGenericEndpoint(endpoint.getClass())) { |
||||||
|
this.endpoints.add(new GenericMvcEndpoint(endpoint)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private Set<Class<?>> findEndpointClasses(Collection<MvcEndpoint> existing) { |
||||||
|
Set<Class<?>> types = new HashSet<Class<?>>(); |
||||||
|
for (MvcEndpoint endpoint : existing) { |
||||||
|
Class<?> type = endpoint.getEndpointType(); |
||||||
|
if (type != null) { |
||||||
|
types.add(type); |
||||||
|
} |
||||||
|
} |
||||||
|
return types; |
||||||
|
} |
||||||
|
|
||||||
|
public Set<? extends MvcEndpoint> getEndpoints() { |
||||||
|
return this.endpoints; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean isGenericEndpoint(Class<?> type) { |
||||||
|
return !this.customTypes.contains(type) |
||||||
|
&& !MvcEndpoint.class.isAssignableFrom(type); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,93 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-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.mvc; |
||||||
|
|
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.TestUtils; |
||||||
|
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration; |
||||||
|
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint; |
||||||
|
import org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpointTests.TestConfiguration; |
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration; |
||||||
|
import org.springframework.context.ConfigurableApplicationContext; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
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.test.web.servlet.MockMvc; |
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
||||||
|
import org.springframework.web.context.WebApplicationContext; |
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString; |
||||||
|
import static org.hamcrest.Matchers.equalToIgnoringCase; |
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Dave Syer |
||||||
|
*/ |
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@SpringApplicationConfiguration(classes = { TestConfiguration.class }) |
||||||
|
@WebAppConfiguration |
||||||
|
public class EnvironmentMvcEndpointTests { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private WebApplicationContext context; |
||||||
|
|
||||||
|
private MockMvc mvc; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); |
||||||
|
TestUtils.addEnviroment((ConfigurableApplicationContext) this.context, "foo:bar"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void home() throws Exception { |
||||||
|
this.mvc.perform(get("/env")).andExpect(status().isOk()) |
||||||
|
.andExpect(content().string(containsString("systemProperties"))); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void sub() throws Exception { |
||||||
|
this.mvc.perform(get("/env/foo")).andExpect(status().isOk()) |
||||||
|
.andExpect(content().string(equalToIgnoringCase("bar"))); |
||||||
|
} |
||||||
|
|
||||||
|
@Import(EndpointWebMvcAutoConfiguration.class) |
||||||
|
@EnableWebMvc |
||||||
|
@Configuration |
||||||
|
public static class TestConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public EnvironmentEndpoint endpoint() { |
||||||
|
return new EnvironmentEndpoint(); |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
public EnvironmentMvcEndpoint mvcEndpoint() { |
||||||
|
return new EnvironmentMvcEndpoint(endpoint()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue