[DOSGI-245] Fix intents not found when published interface is not Object
diff --git a/common/src/main/java/org/apache/cxf/dosgi/common/intent/impl/IntentManagerImpl.java b/common/src/main/java/org/apache/cxf/dosgi/common/intent/impl/IntentManagerImpl.java
index 7c0f6c8..d40fcd4 100644
--- a/common/src/main/java/org/apache/cxf/dosgi/common/intent/impl/IntentManagerImpl.java
+++ b/common/src/main/java/org/apache/cxf/dosgi/common/intent/impl/IntentManagerImpl.java
@@ -18,8 +18,6 @@
*/
package org.apache.cxf.dosgi.common.intent.impl;
-import static org.osgi.service.component.annotations.ReferenceCardinality.MULTIPLE;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -34,10 +32,15 @@
import org.apache.cxf.dosgi.common.intent.IntentManager;
import org.apache.cxf.dosgi.common.intent.IntentProvider;
import org.apache.cxf.endpoint.AbstractEndpointFactory;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-import org.osgi.service.component.annotations.ReferencePolicy;
-import org.osgi.service.component.annotations.ReferencePolicyOption;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,28 +50,41 @@
static final Logger LOG = LoggerFactory.getLogger(IntentManagerImpl.class);
private static final int DEFAULT_INTENT_TIMEOUT = 30000;
- private final Map<String, Object> intentMap;
+ private final Map<String, Object> intentMap = new HashMap<String, Object>();
private final long maxIntentWaitTime = DEFAULT_INTENT_TIMEOUT;
+ private ServiceTracker<Object, Object> tracker;
- public IntentManagerImpl() {
- this.intentMap = new HashMap<String, Object>();
+ @Activate
+ public void activate(BundleContext context) throws InvalidSyntaxException {
+ Filter filter = FrameworkUtil.createFilter("(" + IntentManager.INTENT_NAME_PROP + "=*)");
+ tracker = new ServiceTracker<Object, Object>(context, filter, null) {
+ @Override
+ public Object addingService(ServiceReference<Object> reference) {
+ Object intent = super.addingService(reference);
+ addIntent(intent, (String)reference.getProperty(INTENT_NAME_PROP));
+ return intent;
+ }
+
+ @Override
+ public void removedService(ServiceReference<Object> reference, Object intent) {
+ removeIntent(intent, (String)reference.getProperty(INTENT_NAME_PROP));
+ super.removedService(reference, intent);
+ }
+ };
+ tracker.open();
+ }
+
+ @Deactivate
+ public void deactivate() {
+ tracker.close();
}
- @Reference //
- (//
- cardinality = MULTIPLE, //
- policy = ReferencePolicy.DYNAMIC, //
- target = "(" + IntentManager.INTENT_NAME_PROP + "=*)", //
- policyOption = ReferencePolicyOption.GREEDY
- )
- public synchronized void addIntent(Object intent, Map<String, ?> props) {
- String intentName = (String)props.get(INTENT_NAME_PROP);
+ public synchronized void addIntent(Object intent, String intentName) {
LOG.info("Adding custom intent " + intentName);
intentMap.put(intentName, intent);
}
- public synchronized void removeIntent(Object intent, Map<String, ?> props) {
- String intentName = (String)props.get(INTENT_NAME_PROP);
+ public synchronized void removeIntent(Object intent, String intentName) {
intentMap.remove(intentName);
}
@@ -118,12 +134,21 @@
public synchronized String[] assertAllIntentsSupported(Set<String> requiredIntents) {
long endTime = System.currentTimeMillis() + maxIntentWaitTime;
Set<String> unsupportedIntents;
+ boolean first = true;
do {
unsupportedIntents = getMissingIntents(requiredIntents);
long remainingSeconds = (endTime - System.currentTimeMillis()) / 1000;
if (!unsupportedIntents.isEmpty() && remainingSeconds > 0) {
- LOG.info("Waiting for custom intents " + Arrays.toString(unsupportedIntents.toArray()) + " timeout in "
- + remainingSeconds);
+ String msg = "Waiting for custom intents {} timeout in {} seconds";
+ if (first) {
+ LOG.info(msg, Arrays.toString(unsupportedIntents.toArray()), remainingSeconds);
+ first = false;
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(msg, Arrays.toString(unsupportedIntents.toArray()), remainingSeconds);
+ }
+ }
+
try {
wait(1000);
} catch (InterruptedException e) {
diff --git a/itests/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/customintent/CustomIntentActivator.java b/itests/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/customintent/CustomIntentActivator.java
index ca4efda..a69c6f3 100644
--- a/itests/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/customintent/CustomIntentActivator.java
+++ b/itests/multi-bundle/src/test/java/org/apache/cxf/dosgi/systests2/multi/customintent/CustomIntentActivator.java
@@ -29,7 +29,7 @@
public void start(BundleContext context) throws Exception {
Dictionary<String, String> props = new Hashtable<String, String>();
props.put("org.apache.cxf.dosgi.IntentName", "myIntent");
- context.registerService(Object.class.getName(), new CustomFeature(), props);
+ context.registerService(CustomFeature.class.getName(), new CustomFeature(), props);
}
public void stop(BundleContext context) throws Exception {
diff --git a/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/provider/RsProviderCustomTest.java b/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/provider/RsProviderCustomTest.java
index 19689c1..e28e108 100644
--- a/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/provider/RsProviderCustomTest.java
+++ b/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/provider/RsProviderCustomTest.java
@@ -28,7 +28,6 @@
import org.apache.aries.rsa.spi.Endpoint;
import org.apache.cxf.dosgi.common.httpservice.HttpServiceManager;
-import org.apache.cxf.dosgi.common.intent.IntentManager;
import org.apache.cxf.dosgi.common.intent.IntentProvider;
import org.apache.cxf.dosgi.common.intent.impl.IntentManagerImpl;
import org.apache.cxf.dosgi.dsw.handlers.rest.RsConstants;
@@ -58,7 +57,7 @@
Map<String, Object> props = new HashMap<>();
props.put(Constants.OBJECTCLASS, new String[]{TaskService.class.getName()});
- String serviceAddress = "http://localhost:8181/";
+ String serviceAddress = "http://localhost:9181/";
props.put(RsConstants.RS_ADDRESS_PROPERTY, serviceAddress);
props.put(RemoteConstants.SERVICE_EXPORTED_INTENTS, "my");
Class<?>[] ifaces = new Class[]{TaskService.class};
@@ -82,7 +81,7 @@
private void addIntent(IntentManagerImpl intentManager, String name, Object ... intents) {
IntentProvider provider = intentProvider(intents);
- intentManager.addIntent(provider, intentProps(name));
+ intentManager.addIntent(provider, name);
}
private IntentProvider intentProvider(final Object ... intents) {
@@ -95,9 +94,4 @@
};
}
- private Map<String, String> intentProps(String name) {
- Map<String, String> props = new HashMap<>();
- props.put(IntentManager.INTENT_NAME_PROP, name);
- return props;
- }
}
diff --git a/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/simple/RsProviderTest.java b/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/simple/RsProviderTest.java
index 16a49a9..d082242 100644
--- a/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/simple/RsProviderTest.java
+++ b/provider-rs/src/test/java/org/apache/cxf/dosgi/dsw/handlers/rest/simple/RsProviderTest.java
@@ -52,7 +52,7 @@
Map<String, Object> props = new HashMap<>();
props.put(Constants.OBJECTCLASS, new String[]{TaskService.class.getName()});
- String serviceAddress = "http://localhost:8181/";
+ String serviceAddress = "http://localhost:9181/";
props.put(RsConstants.RS_ADDRESS_PROPERTY, serviceAddress);
Class<?>[] ifaces = new Class[]{TaskService.class};
Endpoint endpoint = rsProvider.exportService(taskService,