Repository: camel Updated Branches: refs/heads/master 380705097 -> 1724bf278
CAMEL-11518: Add an Mvc Actuator endpoint for exposing Camel routes Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1724bf27 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1724bf27 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1724bf27 Branch: refs/heads/master Commit: 1724bf27863dc60961bdd4764870010d072381d1 Parents: 3807050 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Thu Jul 6 14:14:52 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Thu Jul 6 14:14:52 2017 +0200 ---------------------------------------------------------------------- .../endpoint/CamelMvcRoutesEndpoint.java | 69 ++++++++++++++++++++ .../actuate/endpoint/CamelRoutesEndpoint.java | 20 ++++-- .../CamelRoutesEndpointAutoConfiguration.java | 20 +++--- .../endpoint/CamelMvcRoutesEndpointTest.java | 67 +++++++++++++++++++ .../endpoint/CamelRoutesEndpointTest.java | 2 + 5 files changed, 164 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1724bf27/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpoint.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpoint.java new file mode 100644 index 0000000..1886f07 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpoint.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.spring.boot.actuate.endpoint; + +import org.springframework.boot.actuate.endpoint.mvc.ActuatorMediaTypes; +import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * Adapter to expose {@link CamelRoutesEndpoint} as an {@link MvcEndpoint}. + * + * @author Ben Hale + * @author Kazuki Shimizu + * @author Eddú Meléndez + * @since 1.5.0 + */ +@ConfigurationProperties(prefix = "endpoints." + CamelRoutesEndpoint.ENDPOINT_ID) +public class CamelMvcRoutesEndpoint extends EndpointMvcAdapter { + private static final ResponseEntity<?> NOT_FOUND = ResponseEntity.notFound().build(); + private final CamelRoutesEndpoint delegate; + + public CamelMvcRoutesEndpoint(CamelRoutesEndpoint delegate) { + super(delegate); + + this.delegate = delegate; + } + + @RequestMapping( + method = RequestMethod.GET, + value = "/{id}", + produces = { + ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON_VALUE, + MediaType.APPLICATION_JSON_VALUE + } + ) + @ResponseBody + public Object get(@PathVariable String id) { + if (!delegate.isEnabled()) { + return getDisabledResponse(); + } + + Object result = delegate.getRouteInfo(id); + if (result == null) { + result = NOT_FOUND; + } + + return result; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1724bf27/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java index a131ed1..f8e31a9 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpoint.java @@ -21,7 +21,6 @@ import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; - import org.apache.camel.CamelContext; import org.apache.camel.Route; import org.apache.camel.StatefulService; @@ -47,11 +46,22 @@ public class CamelRoutesEndpoint extends AbstractEndpoint<List<RouteEndpointInfo @Override public List<RouteEndpointInfo> invoke() { - // @formatter:off + return getRoutesInfo(); + } + + public List<RouteEndpointInfo> getRoutesInfo() { return camelContext.getRoutes().stream() - .map(RouteEndpointInfo::new) - .collect(Collectors.toList()); - // @formatter:on + .map(RouteEndpointInfo::new) + .collect(Collectors.toList()); + } + + public RouteEndpointInfo getRouteInfo(String id) { + Route route = camelContext.getRoute(id); + if (route != null) { + return new RouteEndpointInfo(route); + } + + return null; } /** http://git-wip-us.apache.org/repos/asf/camel/blob/1724bf27/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java index 628a705..485c28c 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointAutoConfiguration.java @@ -33,15 +33,17 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnBean(CamelAutoConfiguration.class) @AutoConfigureAfter(CamelAutoConfiguration.class) public class CamelRoutesEndpointAutoConfiguration { + @Bean + @ConditionalOnClass(CamelContext.class) + @ConditionalOnMissingBean + public CamelRoutesEndpoint camelEndpoint(CamelContext camelContext) { + return new CamelRoutesEndpoint(camelContext); + } - @ConditionalOnClass({CamelContext.class}) - @ConditionalOnMissingBean(CamelRoutesEndpoint.class) - protected static class CamelEndpointInitializer { - - @Bean - public CamelRoutesEndpoint camelEndpoint(CamelContext camelContext) { - return new CamelRoutesEndpoint(camelContext); - } - + @Bean + @ConditionalOnClass(CamelContext.class) + @ConditionalOnMissingBean + public CamelMvcRoutesEndpoint camelMvcEndpoint(CamelRoutesEndpoint delegate) { + return new CamelMvcRoutesEndpoint(delegate); } } http://git-wip-us.apache.org/repos/asf/camel/blob/1724bf27/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpointTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpointTest.java new file mode 100644 index 0000000..518b7a2 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelMvcRoutesEndpointTest.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.spring.boot.actuate.endpoint; + +import java.util.List; + +import org.apache.camel.CamelContext; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.actuate.endpoint.CamelRoutesEndpoint.RouteEndpointInfo; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Test for the {@link CamelRoutesEndpoint} actuator endpoint. + */ +@DirtiesContext +@RunWith(SpringRunner.class) +@EnableAutoConfiguration +@SpringBootApplication +@SpringBootTest(classes = {CamelAutoConfiguration.class, CamelRoutesEndpointAutoConfiguration.class, ActuatorTestRoute.class}) +public class CamelMvcRoutesEndpointTest extends Assert { + + @Autowired + CamelMvcRoutesEndpoint endpoint; + + @Autowired + CamelContext camelContext; + + @Test + public void testRoutesEndpoint() throws Exception { + List<RouteEndpointInfo> routes = (List<RouteEndpointInfo>)endpoint.invoke(); + + assertFalse(routes.isEmpty()); + assertEquals(routes.size(), camelContext.getRoutes().size()); + assertTrue(routes.stream().anyMatch(r -> "foo-route".equals(r.getId()))); + } + + @Test + public void testMvcRoutesEndpoint() throws Exception { + Object result = endpoint.get("foo-route"); + + assertTrue(result instanceof RouteEndpointInfo); + assertEquals("foo-route", ((RouteEndpointInfo)result).getId()); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1724bf27/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java index 45e2ffb..ea6e8cb 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/endpoint/CamelRoutesEndpointTest.java @@ -28,11 +28,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; /** * Test for the {@link CamelRoutesEndpoint} actuator endpoint. */ +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootApplication