Permalink
Browse files

CAMEL-5969, CAMEL-5972 added the OsgiiServiceRegistry.lookupByType() …

…and supports to lookup service with filter on name=blabla

git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@1435006 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 43c3cfb commit 8f3bb3eee3c5282f37597a14e3f0858a25d22648 @WillemJiang WillemJiang committed Jan 18, 2013
View
56 components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
@@ -17,6 +17,7 @@
package org.apache.camel.core.osgi;
import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
@@ -25,6 +26,7 @@
import org.apache.camel.CamelContext;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.LifecycleStrategySupport;
+import org.apache.camel.util.ObjectHelper;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@@ -34,21 +36,46 @@
public class OsgiServiceRegistry extends LifecycleStrategySupport implements Registry {
private final BundleContext bundleContext;
private final Map<String, Object> serviceCacheMap = new ConcurrentHashMap<String, Object>();
- private final Queue<ServiceReference> serviceReferenceQueue = new ConcurrentLinkedQueue<ServiceReference>();
+ private final Map<Class<?>, Map<String, Object>> typeCacheMap = new ConcurrentHashMap<Class<?>, Map<String, Object>>();
+ private final Queue<ServiceReference<?>> serviceReferenceQueue = new ConcurrentLinkedQueue<ServiceReference<?>>();
public OsgiServiceRegistry(BundleContext bc) {
bundleContext = bc;
}
+ /**
+ * Support to lookup the Object with filter with the (name=NAME) and class type
+ *
+ */
public <T> T lookup(String name, Class<T> type) {
- Object service = lookup(name);
+ Object service = serviceCacheMap.get(name);
+ if (service == null) {
+ ServiceReference<?> sr = null;
+ try {
+ ServiceReference<?>[] refs = bundleContext.getServiceReferences(type.getName(), "(name=" + name + ")");
+ if (refs != null && refs.length > 0) {
+ // just return the first one
+ sr = refs[0];
+ }
+ serviceReferenceQueue.add(sr);
+ service = bundleContext.getService(sr);
+ if (service != null) {
+ serviceCacheMap.put(name, service);
+ }
+ } catch (Exception ex) {
+ throw ObjectHelper.wrapRuntimeCamelException(ex);
+ }
+ }
return type.cast(service);
}
+ /**
+ * It's only support to look up the ServiceReference with Class name
+ */
public Object lookup(String name) {
Object service = serviceCacheMap.get(name);
if (service == null) {
- ServiceReference sr = bundleContext.getServiceReference(name);
+ ServiceReference<?> sr = bundleContext.getServiceReference(name);
if (sr != null) {
// Need to keep the track of Service
// and call ungetService when the camel context is closed
@@ -63,14 +90,31 @@ public Object lookup(String name) {
}
public <T> Map<String, T> lookupByType(Class<T> type) {
- // not implemented so we return an empty map
- return Collections.<String, T>emptyMap();
+ Map<String, T> result = new HashMap<String, T>();
+ try {
+ ServiceReference<?>[] refs = bundleContext.getAllServiceReferences(type.getName(), null);
+ if (refs != null) {
+ for (ServiceReference<?> sr : refs) {
+ serviceReferenceQueue.add(sr);
+ Object service = bundleContext.getService(sr);
+ if (service != null) {
+ String name = (String)sr.getProperty("name");
+ if (name != null) {
+ result.put(name , type.cast(service));
+ }
+ }
+ }
+ }
+ } catch (Exception ex) {
+ throw ObjectHelper.wrapRuntimeCamelException(ex);
+ }
+ return result;
}
@Override
public void onContextStop(CamelContext context) {
// Unget the OSGi service
- ServiceReference sr = serviceReferenceQueue.poll();
+ ServiceReference<?> sr = serviceReferenceQueue.poll();
while (sr != null) {
bundleContext.ungetService(sr);
sr = serviceReferenceQueue.poll();
View
17 ...ents/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java
@@ -16,6 +16,10 @@
*/
package org.apache.camel.core.osgi;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import com.sun.org.apache.xml.internal.security.encryption.Reference;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.component.file.FileComponent;
@@ -26,8 +30,10 @@
import org.apache.camel.spi.LanguageResolver;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.springframework.osgi.mock.MockBundleContext;
+import org.springframework.osgi.mock.MockServiceReference;
public class CamelMockBundleContext extends MockBundleContext {
@@ -61,5 +67,14 @@ public Language resolveLanguage(String name, CamelContext context) {
return null;
}
}
-
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
+ MockServiceReference reference = new MockServiceReference(getBundle(), new String[] {clazz});
+ // setup the name property with the class name
+ Dictionary properties = new Hashtable();
+ properties.put("name", clazz);
+ reference.setProperties(properties);
+ return new ServiceReference[] {reference};
+ }
}
View
8 components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.core.osgi;
+import java.util.Map;
+
import org.apache.camel.core.osgi.test.MyService;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.Test;
@@ -32,9 +34,11 @@ public void camelContextFactoryServiceRegistryTest() throws Exception {
Object service = context.getRegistry().lookup(MyService.class.getName());
assertNotNull("MyService should not be null", service);
+ assertTrue("It should be the instance of MyService ", service instanceof MyService);
- service = context.getRegistry().lookupByType(MyService.class);
- assertNotNull("MyService should not be null", service);
+ Map<String, MyService> collection = context.getRegistry().lookupByType(MyService.class);
+ assertNotNull("MyService should not be null", collection);
+ assertNotNull("There should have one MyService.", collection.get(MyService.class.getName()));
context.stop();
}

0 comments on commit 8f3bb3e

Please sign in to comment.