diff --git a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/Constants.java b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/Constants.java index c26b686ec..05cfe65ab 100644 --- a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/Constants.java +++ b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/Constants.java @@ -28,4 +28,5 @@ public abstract class Constants { public static final String EXPORTED_INTERFACES = "service.exported.interfaces"; public static final String ENDPOINT_FRAMEWORK_UUID = "frameworkUUID"; + public static final String[] NO_EXPORTED_INTERFACES = {}; } diff --git a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ExportServiceListener.java b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ExportServiceListener.java index fff299b73..01f27be6b 100644 --- a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ExportServiceListener.java +++ b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ExportServiceListener.java @@ -26,6 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; @@ -111,10 +112,9 @@ public void exportService(ServiceReference serviceReference) { try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); - String exportedServices = (String) serviceReference.getProperty(Constants.EXPORTED_INTERFACES); - if (exportedServices != null && exportedServices.length() > 0) { - LOGGER.debug("CELLAR DOSGI: registering services {} in the cluster", exportedServices); - String[] interfaces = exportedServices.split(Constants.INTERFACE_SEPARATOR); + String[] interfaces = getExportedInterfaces(serviceReference); + if (interfaces.length > 0) { + LOGGER.debug("CELLAR DOSGI: registering services {} in the cluster", (Object) interfaces); Object service = bundleContext.getService(serviceReference); Set exportedInterfaces = getServiceInterfaces(service, interfaces); @@ -159,10 +159,9 @@ public void unExportService(ServiceReference serviceReference) { ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); - String exportedServices = (String) serviceReference.getProperty(Constants.EXPORTED_INTERFACES); - if (exportedServices != null && exportedServices.length() > 0) { - LOGGER.debug("CELLAR DOSGI: un-register service {} from the cluster", exportedServices); - String[] interfaces = exportedServices.split(Constants.INTERFACE_SEPARATOR); + String[] interfaces = getExportedInterfaces(serviceReference); + if (interfaces.length > 0) { + LOGGER.debug("CELLAR DOSGI: un-register service {} from the cluster", (Object) interfaces); Object service = bundleContext.getService(serviceReference); Set exportedInterfaces = getServiceInterfaces(service, interfaces); @@ -188,6 +187,39 @@ public void unExportService(ServiceReference serviceReference) { } } + + private String[] getExportedInterfaces(ServiceReference serviceReference) { + Object property = serviceReference.getProperty(Constants.EXPORTED_INTERFACES); + if (property != null) { + if (property instanceof String) + return ((String) property).split(Constants.INTERFACE_SEPARATOR); + if (property instanceof String[]) + return (String[]) property; + if (property instanceof Class) + return getClassBinaryNames(serviceReference, property); + if (property instanceof Class[]) + return getClassBinaryNames(serviceReference, (Object[]) property); + if (property instanceof Collection) + return getClassBinaryNames(serviceReference, ((Collection) property).toArray()); + LOGGER.warn("CELLAR DOSGI: Illegal value type of property {} on service reference {}", Constants.EXPORTED_INTERFACES, serviceReference); + } + return Constants.NO_EXPORTED_INTERFACES; + } + + private String[] getClassBinaryNames(ServiceReference serviceReference, Object... classes) { + String[] names = new String[classes.length]; + for (int i = 0; i < classes.length; ++i) { + Object o = classes[i]; + if (o instanceof String) + names[i] = (String) o; + else if (o instanceof Class) + names[i] = ((Class)o).getName(); + else + LOGGER.warn("CELLAR DOSGI: Illegal value type of property {} on service reference {}", Constants.EXPORTED_INTERFACES, serviceReference); + } + return names; + } + /** * Get the interfaces that match the exported service interfaces. *