Permalink
Browse files

CAMEL-5972 fixed the NPE error

git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@1435107 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 6516838 commit 02cab21e3ffb5199c5636854be74f4fec724aecf @WillemJiang WillemJiang committed Jan 18, 2013
View
25 components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
@@ -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 OsgiServiceRegistry(BundleContext bc) {
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 Object lookup(String name) {
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));
+ }
}
}
}
View
13 ...ents/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java
@@ -66,9 +66,22 @@ public Language resolveLanguage(String name, CamelContext context) {
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();
View
3 components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java
@@ -31,6 +31,9 @@ public void camelContextFactoryServiceRegistryTest() throws Exception {
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);

0 comments on commit 02cab21

Please sign in to comment.