camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hekon...@apache.org
Subject camel git commit: [CAMEL-9328] KuraRouter should provide required and option service resolvers.
Date Mon, 16 Nov 2015 09:24:28 GMT
Repository: camel
Updated Branches:
  refs/heads/master dc0b730f6 -> 005df4cb7


[CAMEL-9328] KuraRouter should provide required and option service resolvers.


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

Branch: refs/heads/master
Commit: 005df4cb73063d19ae11e79a4106ba30c235f24c
Parents: dc0b730
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Mon Nov 16 10:24:21 2015 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Mon Nov 16 10:24:21 2015 +0100

----------------------------------------------------------------------
 .../apache/camel/component/kura/KuraRouter.java  | 10 ++++++++++
 .../camel/component/kura/KuraRouterTest.java     | 19 +++++++++++++++++++
 2 files changed, 29 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/005df4cb/components/camel-kura/src/main/java/org/apache/camel/component/kura/KuraRouter.java
----------------------------------------------------------------------
diff --git a/components/camel-kura/src/main/java/org/apache/camel/component/kura/KuraRouter.java
b/components/camel-kura/src/main/java/org/apache/camel/component/kura/KuraRouter.java
index 1548633..b3f16ca 100644
--- a/components/camel-kura/src/main/java/org/apache/camel/component/kura/KuraRouter.java
+++ b/components/camel-kura/src/main/java/org/apache/camel/component/kura/KuraRouter.java
@@ -49,7 +49,9 @@ public abstract class KuraRouter extends RouteBuilder implements BundleActivator
             this.bundleContext = bundleContext;
             log.debug("Initializing bundle {}.", bundleContext.getBundle().getBundleId());
             camelContext = createCamelContext();
+
             camelContext.addRoutes(this);
+
             beforeStart(camelContext);
             log.debug("About to start Camel Kura router: {}", getClass().getName());
             camelContext.start();
@@ -89,6 +91,14 @@ public abstract class KuraRouter extends RouteBuilder implements BundleActivator
 
     protected <T> T service(Class<T> serviceType) {
         ServiceReference reference = bundleContext.getServiceReference(serviceType);
+        return reference == null ? null : (T) bundleContext.getService(reference);
+    }
+
+    protected <T> T requiredService(Class<T> serviceType) {
+        ServiceReference reference = bundleContext.getServiceReference(serviceType);
+        if (reference == null) {
+            throw new IllegalStateException("Cannot find service: " + serviceType.getName());
+        }
         return (T) bundleContext.getService(reference);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/005df4cb/components/camel-kura/src/test/java/org/apache/camel/component/kura/KuraRouterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-kura/src/test/java/org/apache/camel/component/kura/KuraRouterTest.java
b/components/camel-kura/src/test/java/org/apache/camel/component/kura/KuraRouterTest.java
index 03fefa4..6e8dbf9 100644
--- a/components/camel-kura/src/test/java/org/apache/camel/component/kura/KuraRouterTest.java
+++ b/components/camel-kura/src/test/java/org/apache/camel/component/kura/KuraRouterTest.java
@@ -25,8 +25,10 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationAdmin;
 
 import static org.mockito.BDDMockito.given;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 
@@ -76,6 +78,23 @@ public class KuraRouterTest extends Assert {
         assertNotNull(router.consumerTemplate);
     }
 
+    @Test
+    public void shouldReturnNoService() {
+        given(bundleContext.getServiceReference(any(Class.class))).willReturn(null);
+        assertNull(router.service(ConfigurationAdmin.class));
+    }
+
+    @Test
+    public void shouldReturnService() {
+        assertNotNull(router.service(ConfigurationAdmin.class));
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void shouldValidateLackOfService() {
+        given(bundleContext.getServiceReference(any(Class.class))).willReturn(null);
+        router.requiredService(ConfigurationAdmin.class);
+    }
+
 }
 
 class TestKuraRouter extends KuraRouter {


Mime
View raw message