Skip to content

Commit 8b5ceac

Browse files
committed
[KARAF-7437] Improve DOSGi exported interface property parsing
Match with OSGI R7 specs section 100.2
1 parent 2a44d1a commit 8b5ceac

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

dosgi/src/main/java/org/apache/karaf/cellar/dosgi/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ public abstract class Constants {
2828
public static final String EXPORTED_INTERFACES = "service.exported.interfaces";
2929
public static final String ENDPOINT_FRAMEWORK_UUID = "frameworkUUID";
3030

31+
public static final String[] NO_EXPORTED_INTERFACES = {};
3132
}

dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ExportServiceListener.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
2828

29+
import java.util.Collection;
2930
import java.util.HashMap;
3031
import java.util.LinkedHashSet;
3132
import java.util.Map;
@@ -111,10 +112,9 @@ public void exportService(ServiceReference serviceReference) {
111112
try {
112113
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
113114

114-
String exportedServices = (String) serviceReference.getProperty(Constants.EXPORTED_INTERFACES);
115-
if (exportedServices != null && exportedServices.length() > 0) {
116-
LOGGER.debug("CELLAR DOSGI: registering services {} in the cluster", exportedServices);
117-
String[] interfaces = exportedServices.split(Constants.INTERFACE_SEPARATOR);
115+
String[] interfaces = getExportedInterfaces(serviceReference);
116+
if (interfaces.length > 0) {
117+
LOGGER.debug("CELLAR DOSGI: registering services {} in the cluster", (Object) interfaces);
118118
Object service = bundleContext.getService(serviceReference);
119119

120120
Set<String> exportedInterfaces = getServiceInterfaces(service, interfaces);
@@ -159,10 +159,9 @@ public void unExportService(ServiceReference serviceReference) {
159159
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
160160
try {
161161
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
162-
String exportedServices = (String) serviceReference.getProperty(Constants.EXPORTED_INTERFACES);
163-
if (exportedServices != null && exportedServices.length() > 0) {
164-
LOGGER.debug("CELLAR DOSGI: un-register service {} from the cluster", exportedServices);
165-
String[] interfaces = exportedServices.split(Constants.INTERFACE_SEPARATOR);
162+
String[] interfaces = getExportedInterfaces(serviceReference);
163+
if (interfaces.length > 0) {
164+
LOGGER.debug("CELLAR DOSGI: un-register service {} from the cluster", (Object) interfaces);
166165
Object service = bundleContext.getService(serviceReference);
167166

168167
Set<String> exportedInterfaces = getServiceInterfaces(service, interfaces);
@@ -188,6 +187,21 @@ public void unExportService(ServiceReference serviceReference) {
188187
}
189188
}
190189

190+
191+
private String[] getExportedInterfaces(ServiceReference serviceReference) {
192+
Object property = serviceReference.getProperty(Constants.EXPORTED_INTERFACES);
193+
if (property != null) {
194+
if (property instanceof String)
195+
return ((String) property).split(Constants.INTERFACE_SEPARATOR);
196+
if (property instanceof String[])
197+
return (String[]) property;
198+
if (property instanceof Collection)
199+
return (String[])((Collection) property).toArray();
200+
LOGGER.warn("CELLAR DOSGI: Illegal value type on property {} on service reference {}", Constants.EXPORTED_INTERFACES, serviceReference);
201+
}
202+
return Constants.NO_EXPORTED_INTERFACES;
203+
}
204+
191205
/**
192206
* Get the interfaces that match the exported service interfaces.
193207
*

0 commit comments

Comments
 (0)