camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astefanu...@apache.org
Subject camel git commit: CAMEL-9918: Support observing route events filtered by route id in Camel CDI
Date Wed, 27 Apr 2016 15:16:08 GMT
Repository: camel
Updated Branches:
  refs/heads/master bba76eff1 -> 55cfbff4d


CAMEL-9918: Support observing route events filtered by route id in Camel CDI


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

Branch: refs/heads/master
Commit: 55cfbff4d1c53f7b306e9a77ba2d047fc117ecc9
Parents: bba76ef
Author: Antonin Stefanutti <antonin@stefanutti.fr>
Authored: Wed Apr 27 17:15:37 2016 +0200
Committer: Antonin Stefanutti <antonin@stefanutti.fr>
Committed: Wed Apr 27 17:15:57 2016 +0200

----------------------------------------------------------------------
 .../org/apache/camel/cdi/CdiCamelExtension.java |  12 +-
 .../org/apache/camel/cdi/CdiEventNotifier.java  |  30 ++-
 .../apache/camel/cdi/bean/OtherCamelRoute.java  |  27 ++
 .../apache/camel/cdi/bean/SimpleCamelRoute.java |   2 +-
 .../camel/cdi/test/CamelEventNotifierTest.java  |   8 +-
 .../cdi/test/CamelRouteEventNotifierTest.java   | 256 +++++++++++++++++++
 6 files changed, 326 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/55cfbff4/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 5b3affb..0c5c408 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
@@ -30,7 +30,6 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Stream;
 
 import static java.util.Collections.newSetFromMap;
-import static java.util.Collections.singleton;
 import static java.util.function.Predicate.isEqual;
 import static java.util.stream.Collectors.collectingAndThen;
 
@@ -199,8 +198,15 @@ public class CdiCamelExtension implements Extension {
         Type type = pom.getObserverMethod().getObservedType();
         // Camel events are raw types
         if (type instanceof Class && Class.class.cast(type).getPackage().equals(AbstractExchangeEvent.class.getPackage()))
{
-            eventQualifiers.addAll(pom.getObserverMethod().getObservedQualifiers().isEmpty()
-                ? singleton(ANY) : pom.getObserverMethod().getObservedQualifiers());
+            Set<Annotation> qualifiers = pom.getObserverMethod().getObservedQualifiers();
+            if (qualifiers.isEmpty()) {
+                eventQualifiers.add(ANY);
+            } else if (qualifiers.size() == 1 && qualifiers.stream()
+                .filter(isAnnotationType(Named.class)).findAny().isPresent()) {
+                eventQualifiers.add(DEFAULT);
+            } else {
+                eventQualifiers.addAll(qualifiers);
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/55cfbff4/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiEventNotifier.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiEventNotifier.java
b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiEventNotifier.java
index e9d27e7..5bd2d65 100644
--- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiEventNotifier.java
+++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/CdiEventNotifier.java
@@ -17,12 +17,21 @@
 package org.apache.camel.cdi;
 
 import java.lang.annotation.Annotation;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.EventObject;
+import java.util.List;
 import javax.enterprise.inject.spi.BeanManager;
 
+import org.apache.camel.management.event.RouteAddedEvent;
+import org.apache.camel.management.event.RouteRemovedEvent;
+import org.apache.camel.management.event.RouteStartedEvent;
+import org.apache.camel.management.event.RouteStoppedEvent;
 import org.apache.camel.support.EventNotifierSupport;
 
+import static org.apache.camel.util.ObjectHelper.isNotEmpty;
+
 final class CdiEventNotifier extends EventNotifierSupport {
 
     private final BeanManager manager;
@@ -37,7 +46,26 @@ final class CdiEventNotifier extends EventNotifierSupport {
 
     @Override
     public void notify(EventObject event) {
-        manager.fireEvent(event, qualifiers);
+        String id = null;
+
+        if (event instanceof RouteAddedEvent) {
+            id = ((RouteAddedEvent) event).getRoute().getId();
+        } else if (event instanceof RouteStartedEvent) {
+            id = ((RouteStartedEvent) event).getRoute().getId();
+        } else if (event instanceof RouteStoppedEvent) {
+            id = ((RouteStoppedEvent) event).getRoute().getId();
+        } else if (event instanceof RouteRemovedEvent) {
+            id = ((RouteRemovedEvent) event).getRoute().getId();
+        }
+
+        if (isNotEmpty(id)) {
+            List<Annotation> annotations = new ArrayList<>();
+            Collections.addAll(annotations, qualifiers);
+            annotations.add(NamedLiteral.of(id));
+            manager.fireEvent(event, annotations.stream().toArray(Annotation[]::new));
+        } else {
+            manager.fireEvent(event, qualifiers);
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/55cfbff4/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/OtherCamelRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/OtherCamelRoute.java
b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/OtherCamelRoute.java
new file mode 100644
index 0000000..6c5fa1d
--- /dev/null
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/OtherCamelRoute.java
@@ -0,0 +1,27 @@
+/**
+ * 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.bean;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class OtherCamelRoute extends RouteBuilder {
+
+    @Override
+    public void configure() {
+        from("direct:in").routeId("other").startupOrder(20).to("mock:out");
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/55cfbff4/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/SimpleCamelRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/SimpleCamelRoute.java
b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/SimpleCamelRoute.java
index 48de1e7..e2d8762 100644
--- a/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/SimpleCamelRoute.java
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/bean/SimpleCamelRoute.java
@@ -35,6 +35,6 @@ public class SimpleCamelRoute extends RouteBuilder {
 
     @Override
     public void configure() {
-        from(direct).routeId("simple").to(mock);
+        from(direct).routeId("simple").startupOrder(10).to(mock);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/55cfbff4/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java
b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java
index 96b656a..8b7d64d 100644
--- a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java
@@ -101,8 +101,8 @@ public class CamelEventNotifierTest {
 
     @Test
     @InSequence(1)
-    public void startedCamelContext(List<Class> events) throws Exception {
-        assertThat("Events fired are incorrect", events,
+    public void startedCamelContext(List<Class> events) {
+        assertThat("Events fired are incorrect!", events,
             contains(
                 CamelContextStartingEvent.class,
                 CamelContextStartedEvent.class));
@@ -118,7 +118,7 @@ public class CamelEventNotifierTest {
 
         assertIsSatisfied(2L, TimeUnit.SECONDS, outbound);
 
-        assertThat("Events fired are incorrect", events,
+        assertThat("Events fired are incorrect!", events,
             contains(
                 CamelContextStartingEvent.class,
                 CamelContextStartedEvent.class,
@@ -135,7 +135,7 @@ public class CamelEventNotifierTest {
     public void stopCamelContext(CamelContext context, List<Class> events) throws Exception
{
         context.stop();
 
-        assertThat("Events fired are incorrect", events,
+        assertThat("Events fired are incorrect!", events,
             contains(
                 CamelContextStartingEvent.class,
                 CamelContextStartedEvent.class,

http://git-wip-us.apache.org/repos/asf/camel/blob/55cfbff4/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelRouteEventNotifierTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelRouteEventNotifierTest.java
b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelRouteEventNotifierTest.java
new file mode 100644
index 0000000..39977ef
--- /dev/null
+++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelRouteEventNotifierTest.java
@@ -0,0 +1,256 @@
+/**
+ * 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.ArrayList;
+import java.util.EventObject;
+import java.util.List;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.cdi.CdiCamelExtension;
+import org.apache.camel.cdi.bean.OtherCamelRoute;
+import org.apache.camel.cdi.bean.SimpleCamelRoute;
+import org.apache.camel.management.event.RouteAddedEvent;
+import org.apache.camel.management.event.RouteRemovedEvent;
+import org.apache.camel.management.event.RouteStartedEvent;
+import org.apache.camel.management.event.RouteStoppedEvent;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.junit.InSequence;
+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.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasProperty;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+@RunWith(Arquillian.class)
+public class CamelRouteEventNotifierTest {
+
+    @Produces
+    @Named("all")
+    @ApplicationScoped
+    private List<EventObject> allFiredEvents = new ArrayList<>();
+
+    @Produces
+    @Named("simple")
+    @ApplicationScoped
+    private List<EventObject> simpleFiredEvents = new ArrayList<>();
+
+    @Produces
+    @Named("other")
+    @ApplicationScoped
+    private List<EventObject> otherFiredEvents = new ArrayList<>();
+
+    private void onRouteAddedEventEventAll(@Observes RouteAddedEvent event,
+                                           @Named("all") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteAddedEventEventSimple(@Observes @Named("simple") RouteAddedEvent
event,
+                                              @Named("simple") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteAddedEventEventOther(@Observes @Named("other") RouteAddedEvent event,
+                                             @Named("other") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteStartedEventEventAll(@Observes RouteStartedEvent event,
+                                             @Named("all") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteStartedEventEventSimple(@Observes @Named("simple") RouteStartedEvent
event,
+                                                @Named("simple") List<EventObject>
events) {
+        events.add(event);
+    }
+
+    private void onRouteStartedEventEventOther(@Observes @Named("other") RouteStartedEvent
event,
+                                               @Named("other") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteStoppedEventEventAll(@Observes RouteStoppedEvent event,
+                                             @Named("all") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteStoppedEventEventSimple(@Observes @Named("simple") RouteStoppedEvent
event,
+                                                @Named("simple") List<EventObject>
events) {
+        events.add(event);
+    }
+
+    private void onRouteStoppedEventEventOther(@Observes @Named("other") RouteStoppedEvent
event,
+                                               @Named("other") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteRemovedEventEventAll(@Observes RouteRemovedEvent event,
+                                             @Named("all") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    private void onRouteRemovedEventEventSimple(@Observes @Named("simple") RouteRemovedEvent
event,
+                                                @Named("simple") List<EventObject>
events) {
+        events.add(event);
+    }
+
+    private void onRouteRemovedEventEventOther(@Observes @Named("other") RouteRemovedEvent
event,
+                                               @Named("other") List<EventObject> events)
{
+        events.add(event);
+    }
+
+    @Deployment
+    public static Archive<?> deployment() {
+        return ShrinkWrap.create(JavaArchive.class)
+            // Camel CDI
+            .addPackage(CdiCamelExtension.class.getPackage())
+            // Test classes
+            .addClasses(SimpleCamelRoute.class, OtherCamelRoute.class)
+            // Bean archive deployment descriptor
+            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+
+    @Test
+    @InSequence(1)
+    public void startedCamelContext(@Named("all") List<EventObject> all,
+                                    @Named("simple") List<EventObject> simple,
+                                    @Named("other") List<EventObject> other) {
+        assertThat("Events fired are incorrect!", all,
+            contains(
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other")))))
+            )
+        );
+        assertThat("Events fired are incorrect!", simple,
+            contains(
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple")))))
+            )
+        );
+        assertThat("Events fired are incorrect!", other,
+            contains(
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other")))))
+            )
+        );
+    }
+
+    @Test
+    @InSequence(3)
+    public void stopCamelContext(CamelContext context,
+                                 @Named("all") List<EventObject> all,
+                                 @Named("simple") List<EventObject> simple,
+                                 @Named("other") List<EventObject> other) throws Exception
{
+        context.stop();
+
+        assertThat("Events fired are incorrect!", all,
+            contains(
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStoppedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteRemovedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStoppedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteRemovedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple")))))
+            )
+        );
+        assertThat("Events fired are incorrect!", simple,
+            contains(
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteStoppedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple"))))),
+                both(
+                    instanceOf(RouteRemovedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("simple")))))
+            )
+        );
+        assertThat("Events fired are incorrect!", other,
+            contains(
+                both(
+                    instanceOf(RouteAddedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStartedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteStoppedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other"))))),
+                both(
+                    instanceOf(RouteRemovedEvent.class))
+                    .and(hasProperty("route", hasProperty("id", is(equalTo("other")))))
+            )
+        );
+    }
+}


Mime
View raw message