Repository: camel Updated Branches: refs/heads/master 82490dbe3 -> 5089ab27a
Created a mechanism to configure Camel CDI via CDI event. Currently the only setting is to turn off automatic add of RouteBuilders to context. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ca9035ba Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ca9035ba Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ca9035ba Branch: refs/heads/master Commit: ca9035ba607ef7aec9218368de2ceeb344752fd8 Parents: 0ee24c1 Author: Sverker Abrahamsson <sver...@limetransit.com> Authored: Tue Nov 29 19:51:31 2016 +0100 Committer: Sverker Abrahamsson <sver...@limetransit.com> Committed: Fri Dec 2 15:38:35 2016 +0100 ---------------------------------------------------------------------- .../apache/camel/cdi/CdiCamelConfiguration.java | 41 +++++++++++ .../camel/cdi/CdiCamelConfigurationEvent.java | 46 +++++++++++++ .../org/apache/camel/cdi/CdiCamelExtension.java | 34 ++++++---- .../cdi/test/CdiCamelConfigurationTest.java | 71 ++++++++++++++++++++ 4 files changed, 179 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ca9035ba/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfiguration.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfiguration.java new file mode 100644 index 0000000..4ea53c6 --- /dev/null +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfiguration.java @@ -0,0 +1,41 @@ +/** + * 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.cdi; + +/** + * The Camel CDI configuration. Camel CDI fires a {@code CdiCamelConfiguration} event + * during the deployment phase that the application can observe and use to configure it. + * + * Note that the event fired can only be used within the observer method invocation context. + * Any attempt to call one of its methods outside of that context will result in an + * `IllegalStateException` to be thrown. + */ +public interface CdiCamelConfiguration { + + /** + * Overrides the Camel CDI behavior to automatically add all RouteBuilders to the corresponding Camel contexts. + * + * @return this Camel CDI configuration + * @throws IllegalStateException if called outside of the observer method invocation + */ + CdiCamelConfiguration autoConfigureRoutes(boolean autoConfigureRoutes); + + /** + * @return Current state of autoConfigureRoutes parameter. + */ + boolean autoConfigureRoutes(); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ca9035ba/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfigurationEvent.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfigurationEvent.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfigurationEvent.java new file mode 100644 index 0000000..bf96ea0 --- /dev/null +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelConfigurationEvent.java @@ -0,0 +1,46 @@ +/** + * 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.cdi; + +/* package-private */ final class CdiCamelConfigurationEvent implements CdiCamelConfiguration { + + private boolean autoConfigureRoutes = true; + private volatile boolean unmodifiable; + + @Override + public CdiCamelConfiguration autoConfigureRoutes(boolean autoConfigureRoutes) { + throwsIfUnmodifiable(); + this.autoConfigureRoutes = autoConfigureRoutes; + return this; + } + + @Override + public boolean autoConfigureRoutes() { + return autoConfigureRoutes; + } + + void unmodifiable() { + unmodifiable = true; + } + + private void throwsIfUnmodifiable() { + if (unmodifiable) { + throw new IllegalStateException("Camel CDI configuration event must not be used outside " + + "its observer method!"); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ca9035ba/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java index f4bb9bb..e525936 100755 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelExtension.java @@ -120,6 +120,8 @@ public class CdiCamelExtension implements Extension { private final Set<ImportResource> resources = newSetFromMap(new ConcurrentHashMap<>()); + private final CdiCamelConfigurationEvent configuration = new CdiCamelConfigurationEvent(); + CdiEventEndpoint<?> getEventEndpoint(String uri) { return cdiEventEndpoints.get(uri); } @@ -373,6 +375,10 @@ public class CdiCamelExtension implements Extension { } private void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager manager) { + // Send event for Camel CDI configuration + manager.fireEvent(configuration); + configuration.unmodifiable(); + Collection<CamelContext> contexts = new ArrayList<>(); for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) { contexts.add(getReference(manager, CamelContext.class, context)); @@ -387,21 +393,23 @@ public class CdiCamelExtension implements Extension { } // Add routes to Camel contexts - boolean deploymentException = false; - Set<Bean<?>> routes = new HashSet<>(manager.getBeans(RoutesBuilder.class, ANY)); - routes.addAll(manager.getBeans(RouteContainer.class, ANY)); - for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) { - for (Bean<?> route : routes) { - Set<Annotation> qualifiers = new HashSet<>(context.getQualifiers()); - qualifiers.retainAll(route.getQualifiers()); - if (qualifiers.size() > 1) { - deploymentException |= !addRouteToContext(route, context, manager, adv); + if (configuration.autoConfigureRoutes()) { + boolean deploymentException = false; + Set<Bean<?>> routes = new HashSet<>(manager.getBeans(RoutesBuilder.class, ANY)); + routes.addAll(manager.getBeans(RouteContainer.class, ANY)); + for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) { + for (Bean<?> route : routes) { + Set<Annotation> qualifiers = new HashSet<>(context.getQualifiers()); + qualifiers.retainAll(route.getQualifiers()); + if (qualifiers.size() > 1) { + deploymentException |= !addRouteToContext(route, context, manager, adv); + } } } - } - // Let's return to avoid starting misconfigured contexts - if (deploymentException) { - return; + // Let's return to avoid starting misconfigured contexts + if (deploymentException) { + return; + } } // Trigger eager beans instantiation (calling toString is necessary to force http://git-wip-us.apache.org/repos/asf/camel/blob/ca9035ba/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CdiCamelConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CdiCamelConfigurationTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CdiCamelConfigurationTest.java new file mode 100644 index 0000000..b0e316a --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CdiCamelConfigurationTest.java @@ -0,0 +1,71 @@ +/** + * 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.cdi.test; + +import javax.enterprise.event.Observes; +import javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.cdi.CdiCamelConfiguration; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.bean.EndpointInjectRoute; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + +@RunWith(Arquillian.class) +public class CdiCamelConfigurationTest { + private static boolean configMethodHasBeenCalled; + + @Inject + private CamelContext camelContext; + + @Deployment + static Archive<?> createTestArchive() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(CdiCamelConfigurationTest.class) + // RouteBuilder which should not appear in the context + .addClass(EndpointInjectRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + static void configuration(@Observes CdiCamelConfiguration configuration) { + configMethodHasBeenCalled = true; + configuration.autoConfigureRoutes(false); + } + + @Test + public void checkThatConfigMethodHasBeenCalled() { + assertTrue("Config method has not been called", configMethodHasBeenCalled); + } + + @Test + public void checkThatNoRouteBuildersAddedToContext() { + assertTrue("There are RouteBuilder instances in context", camelContext.getRoutes().isEmpty()); + } +} \ No newline at end of file