Author: ningjiang Date: Fri Jan 18 12:20:15 2013 New Revision: 1435107 URL: http://svn.apache.org/viewvc?rev=1435107&view=rev Log: CAMEL-5972 fixed the NPE error
Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java?rev=1435107&r1=1435106&r2=1435107&view=diff ============================================================================== --- camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java (original) +++ camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java Fri Jan 18 12:20:15 2013 @@ -16,7 +16,6 @@ */ package org.apache.camel.core.osgi; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Queue; @@ -55,11 +54,11 @@ public class OsgiServiceRegistry extends 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); + serviceReferenceQueue.add(sr); + service = bundleContext.getService(sr); + if (service != null) { + serviceCacheMap.put(name, service); + } } } catch (Exception ex) { throw ObjectHelper.wrapRuntimeCamelException(ex); @@ -94,12 +93,14 @@ public class OsgiServiceRegistry extends 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)); + if (sr != null) { + Object service = bundleContext.getService(sr); + serviceReferenceQueue.add(sr); + if (service != null) { + String name = (String)sr.getProperty("name"); + if (name != null) { + result.put(name , type.cast(service)); + } } } } Modified: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java?rev=1435107&r1=1435106&r2=1435107&view=diff ============================================================================== --- camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java (original) +++ camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java Fri Jan 18 12:20:15 2013 @@ -66,9 +66,22 @@ public class CamelMockBundleContext exte return null; } } + + public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException { + // just simulate when the bundle context doesn't have right service reference + if (filter != null && filter.indexOf("name=test") > 0) { + return null; + } else { + return super.getServiceReferences(clazz, filter); + } + } @SuppressWarnings({"rawtypes", "unchecked"}) public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException { + // just simulate when the bundle context doesn't have right service reference + if (filter != null && filter.indexOf("name=test") > 0) { + return null; + } MockServiceReference reference = new MockServiceReference(getBundle(), new String[] {clazz}); // setup the name property with the class name Dictionary properties = new Hashtable(); Modified: camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java?rev=1435107&r1=1435106&r2=1435107&view=diff ============================================================================== --- camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java (original) +++ camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java Fri Jan 18 12:20:15 2013 @@ -31,6 +31,9 @@ public class ServiceRegistryTest extends context.start(); MyService myService = context.getRegistry().lookup(MyService.class.getName(), MyService.class); assertNotNull("MyService should not be null", myService); + + myService = context.getRegistry().lookup("test", MyService.class); + assertNull("We should not get the MyService Object here", myService); Object service = context.getRegistry().lookup(MyService.class.getName()); assertNotNull("MyService should not be null", service);