camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [4/9] camel git commit: Automatically deploy @ContextName Camel context beans
Date Fri, 22 Jan 2016 14:59:17 GMT
Automatically deploy @ContextName Camel context beans


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3c0bc162
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3c0bc162
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3c0bc162

Branch: refs/heads/master
Commit: 3c0bc162088ca183aa992514999a868f2b8b218d
Parents: 861a097
Author: Antonin Stefanutti <antonin@stefanutti.fr>
Authored: Thu Jan 21 17:45:39 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Jan 22 15:57:18 2016 +0100

----------------------------------------------------------------------
 .../apache/camel/cdi/CdiCamelContextBean.java   |  7 +-
 .../org/apache/camel/cdi/CdiCamelExtension.java | 43 +++++++++---
 .../FirstCamelContextEndpointInjectRoute.java   |  8 ++-
 .../test/RouteBuilderWithContextNameTest.java   | 73 ++++++++++++++++++++
 .../apache/camel/itest/cdi/CamelContextA.java   | 28 --------
 .../apache/camel/itest/cdi/CamelContextB.java   | 28 --------
 .../apache/camel/itest/cdi/CamelContextC.java   | 28 --------
 .../apache/camel/itest/cdi/CamelContextD.java   | 28 --------
 .../apache/camel/itest/cdi/RoutesContextB.java  |  2 +-
 .../apache/camel/itest/cdi/RoutesContextC.java  |  2 +-
 .../apache/camel/itest/cdi/CamelCdiTest.java    |  9 +--
 11 files changed, 123 insertions(+), 133 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelContextBean.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelContextBean.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelContextBean.java
index 6f4dc10..63db497 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelContextBean.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiCamelContextBean.java
@@ -37,10 +37,13 @@ final class CdiCamelContextBean implements Bean<DefaultCamelContext>,
Passivatio
 
     private final InjectionTarget<DefaultCamelContext> target;
 
+    private final String name;
+
     CdiCamelContextBean(CdiCamelContextAnnotated annotated, InjectionTarget<DefaultCamelContext>
target) {
         this.qualifiers = annotated.getAnnotations();
         this.types = annotated.getTypeClosure();
         this.target = target;
+        this.name = annotated.isAnnotationPresent(ContextName.class) ? annotated.getAnnotation(ContextName.class).value()
: "Default";
     }
 
     @Override
@@ -87,7 +90,7 @@ final class CdiCamelContextBean implements Bean<DefaultCamelContext>,
Passivatio
 
     @Override
     public String toString() {
-        return "Default CDI Camel Context";
+        return "Camel context bean [" + name + "]";
     }
 
     @Override
@@ -112,6 +115,6 @@ final class CdiCamelContextBean implements Bean<DefaultCamelContext>,
Passivatio
 
     @Override
     public String getId() {
-        return getClass().getName();
+        return getClass().getName() + "[" + name + "]";
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/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 e052206..6177370 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
@@ -93,6 +93,8 @@ public class CdiCamelExtension implements Extension {
 
     private final Map<Method, Set<Annotation>> producerQualifiers = new ConcurrentHashMap<>();
 
+    private final Set<ContextName> contextNames = newSetFromMap(new ConcurrentHashMap<ContextName,
Boolean>());
+
     ForwardingObserverMethod<?> getObserverMethod(InjectionPoint ip) {
         return cdiEventEndpoints.get(ip);
     }
@@ -189,6 +191,12 @@ public class CdiCamelExtension implements Extension {
         }
     }
 
+    private <T extends RoutesBuilder> void routeBuilderBeans(@Observes ProcessBean<T>
pb) {
+        if (pb.getAnnotated().isAnnotationPresent(ContextName.class)) {
+            contextNames.add(pb.getAnnotated().getAnnotation(ContextName.class));
+        }
+    }
+
     private <T extends CamelContext> void camelContextBeans(@Observes ProcessBean<T>
pb) {
         contextQualifiers.addAll(pb.getBean().getQualifiers());
     }
@@ -201,7 +209,22 @@ public class CdiCamelExtension implements Extension {
         contextQualifiers.addAll(pb.getBean().getQualifiers());
     }
 
-    private void cdiCamelFactoryProducers(@Observes AfterBeanDiscovery abd) {
+    private void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager manager)
{
+        // Add @ContextName Camel context beans if missing
+        contextNames.removeAll(contextQualifiers);
+        for (ContextName name : contextNames) {
+            abd.addBean(camelContextBean(manager, AnyLiteral.INSTANCE, name));
+        }
+
+        // Add a default Camel context bean if any
+        if (contextQualifiers.isEmpty() && contextNames.isEmpty()) {
+            abd.addBean(camelContextBean(manager, AnyLiteral.INSTANCE, DefaultLiteral.INSTANCE));
+        }
+
+        // Update @ContextName Camel context qualifiers
+        contextQualifiers.addAll(contextNames);
+
+        // Then update the Camel producer beans
         for (Map.Entry<Method, Bean<?>> producer : producerBeans.entrySet())
{
             Bean<?> bean = producer.getValue();
             Set<Annotation> qualifiers = new HashSet<>(producerQualifiers.get(producer.getKey()));
@@ -218,22 +241,19 @@ public class CdiCamelExtension implements Extension {
             // TODO: would be more correct to add a bean for each Camel context bean
             abd.addBean(new BeanDelegate<>(bean, qualifiers));
         }
-    }
 
-    private void addDefaultCamelContext(@Observes AfterBeanDiscovery abd, BeanManager manager)
{
-        if (contextQualifiers.isEmpty()) {
-            CdiCamelContextAnnotated annotated = new CdiCamelContextAnnotated(manager, AnyLiteral.INSTANCE,
DefaultLiteral.INSTANCE);
-            abd.addBean(new CdiCamelContextBean(annotated, environment.camelContextInjectionTarget(new
CamelContextDefaultProducer(), annotated, manager, this)));
-        }
-    }
-
-    private void addCdiEventObserverMethods(@Observes AfterBeanDiscovery abd) {
+        // Add CDI event endpoint observer methods
         for (ObserverMethod method : cdiEventEndpoints.values()) {
             abd.addObserverMethod(method);
         }
     }
 
-    private void createCamelContexts(@Observes AfterDeploymentValidation adv, BeanManager
manager) {
+    private Bean<?> camelContextBean(BeanManager manager, Annotation... qualifiers)
{
+        CdiCamelContextAnnotated annotated = new CdiCamelContextAnnotated(manager, qualifiers);
+        return new CdiCamelContextBean(annotated, environment.camelContextInjectionTarget(new
CamelContextDefaultProducer(), annotated, manager, this));
+    }
+
+    private void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager
manager) {
         Collection<CamelContext> contexts = new ArrayList<>();
         for (Bean<?> context : manager.getBeans(CamelContext.class, AnyLiteral.INSTANCE))
{
             contexts.add(BeanManagerHelper.getReference(manager, CamelContext.class, context));
@@ -290,6 +310,7 @@ public class CdiCamelExtension implements Extension {
         eagerBeans.clear();
         producerBeans.clear();
         producerQualifiers.clear();
+        contextNames.clear();
     }
 
     private boolean addRouteToContext(Bean<?> routeBean, Bean<?> contextBean,
BeanManager manager, AfterDeploymentValidation adv) {

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FirstCamelContextEndpointInjectRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FirstCamelContextEndpointInjectRoute.java
b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FirstCamelContextEndpointInjectRoute.java
index a85e8e5..1d68f95 100644
--- a/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FirstCamelContextEndpointInjectRoute.java
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/FirstCamelContextEndpointInjectRoute.java
@@ -16,15 +16,19 @@
  */
 package org.apache.camel.cdi.bean;
 
+import javax.inject.Inject;
+
 import org.apache.camel.Endpoint;
-import org.apache.camel.EndpointInject;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.cdi.ContextName;
+import org.apache.camel.cdi.Uri;
 
 @ContextName("first")
 public class FirstCamelContextEndpointInjectRoute extends RouteBuilder {
 
-    @EndpointInject(uri = "direct:inbound", context = "first")
+    @Inject
+    @ContextName("first")
+    @Uri("direct:inbound")
     private Endpoint inbound;
     
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/RouteBuilderWithContextNameTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/RouteBuilderWithContextNameTest.java
b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/RouteBuilderWithContextNameTest.java
new file mode 100644
index 0000000..6aa84e3
--- /dev/null
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/RouteBuilderWithContextNameTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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 java.util.concurrent.TimeUnit;
+import javax.inject.Inject;
+
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.cdi.CdiCamelExtension;
+import org.apache.camel.cdi.Uri;
+import org.apache.camel.cdi.bean.FirstCamelContextEndpointInjectRoute;
+import org.apache.camel.cdi.bean.SecondCamelContextEndpointInjectRoute;
+import org.apache.camel.component.mock.MockEndpoint;
+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.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
+
+@RunWith(Arquillian.class)
+public class RouteBuilderWithContextNameTest {
+
+    @Inject
+    @Uri(value = "direct:inbound", context = "first")
+    private ProducerTemplate inbound;
+
+    @Inject
+    @Uri(value = "mock:outbound", context = "first")
+    private MockEndpoint outbound;
+
+    @Deployment
+    public static Archive<?> deployment() {
+        return ShrinkWrap.create(JavaArchive.class)
+            // Camel CDI
+            .addPackage(CdiCamelExtension.class.getPackage())
+            // Test class
+            .addClasses(
+                FirstCamelContextEndpointInjectRoute.class,
+                SecondCamelContextEndpointInjectRoute.class)
+            // Bean archive deployment descriptor
+            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+
+    @Test
+    public void sendMessageToInbound() throws InterruptedException {
+        outbound.expectedMessageCount(1);
+        outbound.expectedBodiesReceived("test");
+        outbound.expectedHeaderReceived("context", "first");
+
+        inbound.sendBody("test");
+
+        assertIsSatisfied(2L, TimeUnit.SECONDS, outbound);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextA.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextA.java
b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextA.java
deleted file mode 100644
index 3ca839c..0000000
--- a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextA.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * 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.itest.cdi;
-
-import javax.enterprise.context.ApplicationScoped;
-
-import org.apache.camel.cdi.CdiCamelContext;
-import org.apache.camel.cdi.ContextName;
-
-@ApplicationScoped
-@ContextName("contextA")
-public class CamelContextA extends CdiCamelContext {
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextB.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextB.java
b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextB.java
deleted file mode 100644
index 2889cbd..0000000
--- a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextB.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * 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.itest.cdi;
-
-import javax.enterprise.context.ApplicationScoped;
-
-import org.apache.camel.cdi.CdiCamelContext;
-import org.apache.camel.cdi.ContextName;
-
-@ApplicationScoped
-@ContextName("contextB")
-public class CamelContextB extends CdiCamelContext {
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextC.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextC.java
b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextC.java
deleted file mode 100644
index d4de27d..0000000
--- a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextC.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * 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.itest.cdi;
-
-import javax.enterprise.context.ApplicationScoped;
-
-import org.apache.camel.cdi.CdiCamelContext;
-import org.apache.camel.cdi.ContextName;
-
-@ApplicationScoped
-@ContextName("contextC")
-public class CamelContextC extends CdiCamelContext {
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextD.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextD.java
b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextD.java
deleted file mode 100644
index 0934241..0000000
--- a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/CamelContextD.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * 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.itest.cdi;
-
-import javax.enterprise.context.ApplicationScoped;
-
-import org.apache.camel.cdi.CdiCamelContext;
-import org.apache.camel.cdi.ContextName;
-
-@ApplicationScoped
-@ContextName("contextD")
-public class CamelContextD extends CdiCamelContext {
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextB.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextB.java
b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextB.java
index 89e0933..5e44dbf 100644
--- a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextB.java
+++ b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextB.java
@@ -47,7 +47,7 @@ public class RoutesContextB extends RouteBuilder {
     ProducerTemplate producer;
 
     @Override
-    public void configure() throws Exception {
+    public void configure() {
         LOG.info("Adding route from " + a + " to " + b);
         from(a).to(b);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextC.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextC.java
b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextC.java
index 507a1d4..e3a9e2b 100644
--- a/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextC.java
+++ b/tests/camel-itest-cdi/src/main/java/org/apache/camel/itest/cdi/RoutesContextC.java
@@ -47,7 +47,7 @@ public class RoutesContextC extends RouteBuilder {
     ProducerTemplate producer;
 
     @Override
-    public void configure() throws Exception {
+    public void configure() {
         from(a).to(b);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/3c0bc162/tests/camel-itest-cdi/src/test/java/org/apache/camel/itest/cdi/CamelCdiTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-cdi/src/test/java/org/apache/camel/itest/cdi/CamelCdiTest.java
b/tests/camel-itest-cdi/src/test/java/org/apache/camel/itest/cdi/CamelCdiTest.java
index 23261ad..bee1905 100644
--- a/tests/camel-itest-cdi/src/test/java/org/apache/camel/itest/cdi/CamelCdiTest.java
+++ b/tests/camel-itest-cdi/src/test/java/org/apache/camel/itest/cdi/CamelCdiTest.java
@@ -73,10 +73,11 @@ public class CamelCdiTest {
     public static JavaArchive createDeployment() {
         return ShrinkWrap.create(JavaArchive.class)
             .addPackage(CdiCamelExtension.class.getPackage())
-            .addClasses(CamelContextA.class, RoutesContextA.class,
-                        CamelContextB.class, RoutesContextB.class,
-                        CamelContextC.class, RoutesContextC.class,
-                        CamelContextD.class, RoutesContextD.class)
+            .addClasses(
+                RoutesContextA.class,
+                RoutesContextB.class,
+                RoutesContextC.class,
+                RoutesContextD.class)
             .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
     }
 


Mime
View raw message