import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Utilities for OSGi's {@link ServiceReference}.
*/
public class ServiceReferenceUtil {
+ private static final Logger LOG = LoggerFactory.getLogger(ServiceReferenceUtil.class);
+
public Map<String, Object> getProperties(ServiceReference<?> serviceRef) {
String[] propertyKeys = serviceRef.getPropertyKeys();
Map<String, Object> properties = new HashMap<>(propertyKeys.length);
for (String propertyKey : propertyKeys) {
Object propertyValue = serviceRef.getProperty(propertyKey);
- if (propertyValue.getClass().isArray()) {
- propertyValue = Arrays.asList((Object[]) propertyValue);
+ if (propertyValue != null) {
+ if (propertyValue.getClass().isArray()) {
+ propertyValue = Arrays.asList((Object[]) propertyValue);
+ }
}
+ // maintain the null value in the property map anyway
properties.put(propertyKey, propertyValue);
}
return properties;
}
public List<String> getUsingBundleSymbolicNames(ServiceReference<?> serviceRef) {
- if (serviceRef.getUsingBundles() == null) {
+ Bundle[] usingBundles = serviceRef.getUsingBundles();
+ if (usingBundles == null) {
return Collections.emptyList();
} else {
- return Arrays.asList(serviceRef.getUsingBundles()).stream()
- .map(bundle -> bundle.getSymbolicName()).collect(Collectors.toList());
+ return Arrays.asList(usingBundles).stream()
+ .map(bundle -> bundle.getSymbolicName()).collect(Collectors.toList());
}
}
import org.apache.karaf.bundle.core.BundleService;
import org.awaitility.Awaitility;
import org.awaitility.core.ConditionTimeoutException;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
+ "diag failure BundleService reported bundle(s) which are not active");
try {
for (ServiceReference<?> serviceRef : bundleContext.getAllServiceReferences(null, null)) {
- LOG.info("{} defines OSGi Service {} used by {}", serviceRef.getBundle().getSymbolicName(),
- util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+ Bundle bundle = serviceRef.getBundle();
+ // serviceRef.getBundle() can return null if the bundle was destroyed
+ if (bundle != null) {
+ LOG.info("{} defines OSGi Service {} used by {}", bundle.getSymbolicName(),
+ util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+ } else {
+ LOG.trace("skipping reporting service reference as the underlying bundle is null");
+ }
}
} catch (InvalidSyntaxException e) {
LOG.error("logOSGiServices() failed due to InvalidSyntaxException", e);
import static com.google.common.truth.Truth.assertThat;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import java.util.Arrays;
+import java.util.Map;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
public void testGetProperties() {
assertThat(new ServiceReferenceUtil().getProperties(getServiceReference())).containsExactly(
"property1", "value1",
- "property2", Arrays.asList(new String[] { "value2.1", "value2.2" }));
+ "property2", Arrays.asList(new String[] { "value2.1", "value2.2" }),
+ "property3", null);
}
- private ServiceReference<?> getServiceReference() {
- return new ServiceReference<Object>() {
-
- @Override
- public Object getProperty(String key) {
- if ("property1".equals(key)) {
- return "value1";
- } else if ("property2".equals(key)) {
- return new String[] { "value2.1", "value2.2" };
- } else {
- return null;
- }
- }
-
- @Override
- public String[] getPropertyKeys() {
- return new String[] { "property1", "property2"};
- }
-
- @Override
- public Bundle getBundle() {
- return null;
- }
-
- @Override
- public Bundle[] getUsingBundles() {
- return null;
- }
-
- @Override
- public boolean isAssignableTo(Bundle bundle, String className) {
- return false;
- }
-
- @Override
- public int compareTo(Object reference) {
- return 0;
- }
- };
+ private static ServiceReference<?> getServiceReference() {
+ return new TestServiceReference();
+ }
+
+ private static final class TestServiceReference implements ServiceReference<Object> {
+
+ private Map<String, Object> properties = Maps.newHashMap();
+
+ TestServiceReference() {
+ properties.put("property1", "value1");
+ properties.put("property2", Lists.newArrayList("value2.1", "value2.2"));
+ properties.put("property3", null);
+ }
+
+ @Override
+ public Object getProperty(String key) {
+ return properties.get(key);
+ }
+
+ @Override
+ public String[] getPropertyKeys() {
+ return properties.keySet().stream().toArray(String[]::new);
+ }
+
+ @Override
+ public Bundle getBundle() {
+ return null;
+ }
+
+ @Override
+ public Bundle[] getUsingBundles() {
+ return null;
+ }
+
+ @Override
+ public boolean isAssignableTo(Bundle bundle, String className) {
+ return false;
+ }
+
+ @Override
+ public int compareTo(Object reference) {
+ return 0;
+ }
}
}
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Utilities for OSGi's {@link ServiceReference}.
*/
public class ServiceReferenceUtil {
+ private static final Logger LOG = LoggerFactory.getLogger(ServiceReferenceUtil.class);
+
public Map<String, Object> getProperties(ServiceReference<?> serviceRef) {
String[] propertyKeys = serviceRef.getPropertyKeys();
Map<String, Object> properties = new HashMap<>(propertyKeys.length);
for (String propertyKey : propertyKeys) {
Object propertyValue = serviceRef.getProperty(propertyKey);
- if (propertyValue.getClass().isArray()) {
- propertyValue = Arrays.asList((Object[]) propertyValue);
+ if (propertyValue != null) {
+ if (propertyValue.getClass().isArray()) {
+ propertyValue = Arrays.asList((Object[]) propertyValue);
+ }
}
+ // maintain the null value in the property map anyway
properties.put(propertyKey, propertyValue);
}
return properties;
}
public List<String> getUsingBundleSymbolicNames(ServiceReference<?> serviceRef) {
- if (serviceRef.getUsingBundles() == null) {
+ Bundle[] usingBundles = serviceRef.getUsingBundles();
+ if (usingBundles == null) {
return Collections.emptyList();
} else {
- return Arrays.asList(serviceRef.getUsingBundles()).stream()
+ return Arrays.asList(usingBundles).stream()
.map(bundle -> bundle.getSymbolicName()).collect(Collectors.toList());
}
}
import org.apache.karaf.bundle.core.BundleService;
import org.awaitility.Awaitility;
import org.awaitility.core.ConditionTimeoutException;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
+ "diag failure BundleService reported bundle(s) which are not active");
try {
for (ServiceReference<?> serviceRef : bundleContext.getAllServiceReferences(null, null)) {
- LOG.info("{} defines OSGi Service {} used by {}", serviceRef.getBundle().getSymbolicName(),
- util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+ Bundle bundle = serviceRef.getBundle();
+ // serviceRef.getBundle() can return null if the bundle was destroyed
+ if (bundle != null) {
+ LOG.info("{} defines OSGi Service {} used by {}", bundle.getSymbolicName(),
+ util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+ } else {
+ LOG.trace("skipping reporting service reference as the underlying bundle is null");
+ }
}
} catch (InvalidSyntaxException e) {
LOG.error("logOSGiServices() failed due to InvalidSyntaxException", e);
import static com.google.common.truth.Truth.assertThat;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import java.util.Arrays;
+import java.util.Map;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
public void testGetProperties() {
assertThat(new ServiceReferenceUtil().getProperties(getServiceReference())).containsExactly(
"property1", "value1",
- "property2", Arrays.asList(new String[] { "value2.1", "value2.2" }));
+ "property2", Arrays.asList(new String[] { "value2.1", "value2.2" }),
+ "property3", null);
}
- private ServiceReference<?> getServiceReference() {
- return new ServiceReference<Object>() {
-
- @Override
- public Object getProperty(String key) {
- if ("property1".equals(key)) {
- return "value1";
- } else if ("property2".equals(key)) {
- return new String[] { "value2.1", "value2.2" };
- } else {
- return null;
- }
- }
-
- @Override
- public String[] getPropertyKeys() {
- return new String[] { "property1", "property2"};
- }
-
- @Override
- public Bundle getBundle() {
- return null;
- }
-
- @Override
- public Bundle[] getUsingBundles() {
- return null;
- }
-
- @Override
- public boolean isAssignableTo(Bundle bundle, String className) {
- return false;
- }
-
- @Override
- public int compareTo(Object reference) {
- return 0;
- }
- };
+ private static ServiceReference<?> getServiceReference() {
+ return new TestServiceReference();
+ }
+
+ private static final class TestServiceReference implements ServiceReference<Object> {
+
+ private Map<String, Object> properties = Maps.newHashMap();
+
+ TestServiceReference() {
+ properties.put("property1", "value1");
+ properties.put("property2", Lists.newArrayList("value2.1", "value2.2"));
+ properties.put("property3", null);
+ }
+
+ @Override
+ public Object getProperty(String key) {
+ return properties.get(key);
+ }
+
+ @Override
+ public String[] getPropertyKeys() {
+ return properties.keySet().stream().toArray(String[]::new);
+ }
+
+ @Override
+ public Bundle getBundle() {
+ return null;
+ }
+
+ @Override
+ public Bundle[] getUsingBundles() {
+ return null;
+ }
+
+ @Override
+ public boolean isAssignableTo(Bundle bundle, String className) {
+ return false;
+ }
+
+ @Override
+ public int compareTo(Object reference) {
+ return 0;
+ }
}
}