Made the toString(ServiceReference) more robust with respect to dealing with references that belong to services that are no longer registered.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@987473 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/ServiceUtil.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/ServiceUtil.java
index 301f867..73a0fe1 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/ServiceUtil.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/ServiceUtil.java
@@ -18,6 +18,7 @@
*/
package org.apache.felix.dm;
+import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
@@ -82,21 +83,30 @@
*/
public static String toString(ServiceReference ref) {
if (ref == null) {
- throw new IllegalArgumentException("Service reference cannot be null.");
+ return "ServiceReference[null]";
}
- StringBuffer buf = new StringBuffer();
-
- buf.append("ServiceReference[" + ref.getBundle().getBundleId() + "]{");
- String[] keys = ref.getPropertyKeys();
- for (int i = 0; i < keys.length; i++) {
- if (i > 0) {
- buf.append(',');
+ else {
+ StringBuffer buf = new StringBuffer();
+ Bundle bundle = ref.getBundle();
+ if (bundle != null) {
+ buf.append("ServiceReference[");
+ buf.append(bundle.getBundleId());
+ buf.append("]{");
}
- buf.append(keys[i]);
- buf.append('=');
- buf.append(ref.getProperty(keys[i]));
+ else {
+ buf.append("ServiceReference[unregistered]{");
+ }
+ String[] keys = ref.getPropertyKeys();
+ for (int i = 0; i < keys.length; i++) {
+ if (i > 0) {
+ buf.append(',');
+ }
+ buf.append(keys[i]);
+ buf.append('=');
+ buf.append(ref.getProperty(keys[i]));
+ }
+ buf.append("}");
+ return buf.toString();
}
- buf.append("}");
- return buf.toString();
}
}