Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslewis2011-01-03 22:48:50 -0500
committerslewis2011-01-03 22:48:50 -0500
commit664abb0e37fe12d477661a168391e05019be8cb0 (patch)
treef3b70a9e6f407a932f7e1f09ca81070750426aed /incubation
parent5245018f4890f53d694d85966b3df3e5ceaead2d (diff)
downloadorg.eclipse.ecf-664abb0e37fe12d477661a168391e05019be8cb0.tar.gz
org.eclipse.ecf-664abb0e37fe12d477661a168391e05019be8cb0.tar.xz
org.eclipse.ecf-664abb0e37fe12d477661a168391e05019be8cb0.zip
rsa refactoring and new test case
Diffstat (limited to 'incubation')
-rw-r--r--incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/AbstractTopologyManager.java204
-rw-r--r--incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdmin.java153
-rw-r--r--incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdminEvent.java8
-rw-r--r--incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/TopologyManager.java211
-rw-r--r--incubation/bundles/org.eclipse.ecf.tests.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/tests/osgi/services/remoteserviceadmin/AbstractRemoteServiceRegisterTest.java47
5 files changed, 349 insertions, 274 deletions
diff --git a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/AbstractTopologyManager.java b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/AbstractTopologyManager.java
index e1371db00..384466cd8 100644
--- a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/AbstractTopologyManager.java
+++ b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/AbstractTopologyManager.java
@@ -9,12 +9,24 @@
******************************************************************************/
package org.eclipse.ecf.osgi.services.remoteserviceadmin;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
import org.eclipse.core.runtime.IStatus;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.DebugOptions;
import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.LogUtility;
+import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.remoteserviceadmin.ExportRegistration;
+import org.osgi.service.remoteserviceadmin.ImportRegistration;
import org.osgi.util.tracker.ServiceTracker;
public abstract class AbstractTopologyManager {
@@ -29,6 +41,10 @@ public abstract class AbstractTopologyManager {
private ServiceTracker remoteServiceAdminTracker;
private Object remoteServiceAdminTrackerLock = new Object();
+ protected Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> exportedRegistrations = new ArrayList<org.osgi.service.remoteserviceadmin.ExportRegistration>();
+
+ protected Collection<org.osgi.service.remoteserviceadmin.ImportRegistration> importedRegistrations = new ArrayList<org.osgi.service.remoteserviceadmin.ImportRegistration>();
+
public AbstractTopologyManager(BundleContext context) {
this.context = context;
}
@@ -137,6 +153,11 @@ public abstract class AbstractTopologyManager {
+ " FAILED", result);
}
+ protected void logError(String methodName, String message, Throwable exception) {
+ LogUtility.logError(methodName, DebugOptions.TOPOLOGY_MANAGER,
+ this.getClass(), message, exception);
+ }
+
protected void logError(String methodName, String message, IStatus result) {
LogUtility.logError(methodName, DebugOptions.TOPOLOGY_MANAGER,
this.getClass(), result);
@@ -152,4 +173,187 @@ public abstract class AbstractTopologyManager {
this.getClass(), message);
}
+ protected void handleEndpointAdded(EndpointDescription endpointDescription) {
+ // First, select importing remote service admin
+ org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
+
+ if (rsa == null) {
+ logError("handleEndpointAdded",
+ "RemoteServiceAdmin not found for importing endpointDescription="
+ + endpointDescription);
+ return;
+ }
+
+ trace("handleEndpointAdded", "endpointDescription="
+ + endpointDescription+" rsa="+rsa);
+
+ // now call rsa.import
+ org.osgi.service.remoteserviceadmin.ImportRegistration importRegistration = rsa
+ .importService(endpointDescription);
+
+ if (importRegistration == null) {
+ logError("handleEndpointAdded",
+ "Import registration is null for endpointDescription="
+ + endpointDescription + " and rsa=" + rsa);
+ } else {
+ Throwable t = importRegistration.getException();
+ if (t != null) handleInvalidImportRegistration(importRegistration,t);
+ else {
+ trace("handleEndpointAdded","service imported. importRegistration="+importRegistration);
+ synchronized (importedRegistrations) {
+ importedRegistrations.add(importRegistration);
+ }
+ }
+ }
+ }
+
+ protected void handleInvalidImportRegistration(
+ ImportRegistration importRegistration, Throwable t) {
+ logError("handleInvalidImportRegistration","importRegistration="+importRegistration,t);
+ }
+
+ protected void handleEndpointRemoved(EndpointDescription endpointDescription) {
+ trace("handleEndpointRemoved", "endpointDescription="
+ + endpointDescription);
+ unimportService(endpointDescription);
+ }
+
+ protected void handleServiceRegistering(ServiceReference serviceReference) {
+ // Using OSGI 4.2 Chap 13 Remote Services spec, get the specified remote
+ // interfaces for the given service reference
+ String[] exportedInterfaces = PropertiesUtil
+ .getExportedInterfaces(serviceReference);
+ // If no remote interfaces set, then we don't do anything with it
+ if (exportedInterfaces == null)
+ return;
+
+ // Select remote service admin
+ org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
+
+ // if no remote service admin available, then log error and return
+ if (rsa == null) {
+ logError("handleServiceRegistered",
+ "No RemoteServiceAdmin found for serviceReference="
+ + serviceReference
+ + ". Remote service NOT EXPORTED");
+ return;
+ }
+
+ // prepare export properties
+ Map<String, Object> exportProperties = new TreeMap<String, Object>(
+ String.CASE_INSENSITIVE_ORDER);
+ exportProperties
+ .put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_INTERFACES,
+ exportedInterfaces);
+ trace("handleServiceRegistering","serviceReference="+serviceReference+" exportProperties="+exportProperties);
+ Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> registrations =
+ rsa.exportService(serviceReference,
+ exportProperties);
+
+ if (registrations == null || registrations.size() == 0) {
+ logError("handleServiceRegistered",
+ "No export registrations created by RemoteServiceAdmin="
+ + rsa + ". ServiceReference="
+ + serviceReference + " NOT EXPORTED");
+ return;
+ }
+
+ List<EndpointDescription> endpointDescriptions = new ArrayList<EndpointDescription>();
+
+ for(org.osgi.service.remoteserviceadmin.ExportRegistration exportRegistration: registrations) {
+ // If they are invalid report as such
+ Throwable t = exportRegistration.getException();
+ if (t != null)
+ handleInvalidExportRegistration(exportRegistration,t);
+ else {
+ trace("handleServiceRegistering","service exported. exportRegistration="+exportRegistration);
+ endpointDescriptions.add((EndpointDescription) exportRegistration.getExportReference().getExportedEndpoint());
+ synchronized (exportedRegistrations) {
+ exportedRegistrations.add(exportRegistration);
+ }
+ }
+ }
+ // adversitise valid exported registrations
+ for (EndpointDescription ed : endpointDescriptions) advertiseEndpointDescription(ed);
+ }
+
+ protected void handleInvalidExportRegistration(ExportRegistration exportRegistration, Throwable t) {
+ logError("handleInvalidExportRegistration","exportRegistration="+exportRegistration,t);
+ }
+
+ protected void handleServiceModifying(ServiceReference serviceReference) {
+ handleServiceUnregistering(serviceReference);
+ handleServiceRegistering(serviceReference);
+ }
+
+ protected void handleServiceUnregistering(ServiceReference serviceReference) {
+ Collection<EndpointDescription> endpointDescriptions = unexportService(serviceReference);
+ if (endpointDescriptions != null)
+ for (EndpointDescription ed : endpointDescriptions)
+ unadvertiseEndpointDescription(ed);
+ }
+
+ protected Collection<EndpointDescription> unexportService(ServiceReference serviceReference) {
+ Map<org.osgi.service.remoteserviceadmin.ExportRegistration, EndpointDescription> matchingExportRegistrations = null;
+ synchronized (exportedRegistrations) {
+ for(Iterator<org.osgi.service.remoteserviceadmin.ExportRegistration> i=exportedRegistrations.iterator(); i.hasNext(); ) {
+ if (matchingExportRegistrations == null) matchingExportRegistrations = new HashMap<org.osgi.service.remoteserviceadmin.ExportRegistration, EndpointDescription>();
+ org.osgi.service.remoteserviceadmin.ExportRegistration exportRegistration = i.next();
+ // Only check valid registrations (no exceptions)
+ if (exportRegistration.getException() == null) {
+ org.osgi.service.remoteserviceadmin.ExportReference exportRef = exportRegistration.getExportReference();
+ if (exportRef != null) {
+ ServiceReference exportReference = exportRef.getExportedService();
+ if (exportReference != null && serviceReference.equals(exportReference)) {
+ matchingExportRegistrations.put(exportRegistration, (EndpointDescription) exportRef.getExportedEndpoint());
+ }
+ }
+ // remove no matter what
+ i.remove();
+ }
+ }
+ }
+ // If no matching export registrations then we return null and are done
+ if (matchingExportRegistrations == null
+ || matchingExportRegistrations.size() == 0)
+ return null;
+ // We close all matching export registrations
+ for (Iterator<org.osgi.service.remoteserviceadmin.ExportRegistration> i = matchingExportRegistrations
+ .keySet().iterator(); i.hasNext();) {
+ org.osgi.service.remoteserviceadmin.ExportRegistration exportRegistration = i
+ .next();
+ trace("unexportService", "closing exportRegistration=" + exportRegistration);
+ exportRegistration.close();
+ }
+ // And return endpointDescriptions for matching registrations
+ return matchingExportRegistrations.values();
+ }
+
+ protected void unimportService(EndpointDescription endpointDescription) {
+ List<org.osgi.service.remoteserviceadmin.ImportRegistration> removedRegistrations = null;
+ synchronized (importedRegistrations) {
+ for (Iterator<org.osgi.service.remoteserviceadmin.ImportRegistration> i = importedRegistrations
+ .iterator(); i.hasNext();) {
+ if (removedRegistrations == null) removedRegistrations = new ArrayList<org.osgi.service.remoteserviceadmin.ImportRegistration>();
+ org.osgi.service.remoteserviceadmin.ImportRegistration importRegistration = i.next();
+ if (importRegistration.getException() == null) {
+ org.osgi.service.remoteserviceadmin.ImportReference importRef = importRegistration.getImportReference();
+ if (importRef != null) {
+ EndpointDescription ed = (EndpointDescription) importRef.getImportedEndpoint();
+ if (ed != null && ed.isSameService(endpointDescription)) {
+ removedRegistrations.add(importRegistration);
+ }
+ }
+ // remove no matter what
+ i.remove();
+ }
+ }
+ }
+ // Now close all of them
+ for (org.osgi.service.remoteserviceadmin.ImportRegistration removedReg : removedRegistrations) {
+ trace("unimportService", "closing importRegistration="+removedReg);
+ removedReg.close();
+ }
+ }
+
}
diff --git a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdmin.java b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdmin.java
index 3dd5b5d90..b0aefbf60 100644
--- a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdmin.java
+++ b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdmin.java
@@ -103,6 +103,28 @@ public class RemoteServiceAdmin implements
return bundle;
}
+ private boolean removeExportRegistration(
+ ExportRegistration exportRegistration) {
+ synchronized (exportedRegistrations) {
+ boolean removed = exportedRegistrations.remove(exportRegistration);
+ trace("removeExportRegistration", "exportRegistration="
+ + exportRegistration + " exportedRegistrations="
+ + exportedRegistrations + " removed=" + removed);
+ return removed;
+ }
+ }
+
+ private boolean removeImportRegistration(
+ ImportRegistration importRegistration) {
+ synchronized (importedRegistrations) {
+ boolean removed = importedRegistrations.remove(importRegistration);
+ trace("removeImportRegistration", "importRegistration="
+ + importRegistration + " importedRegistrations="
+ + importedRegistrations + " removed=" + removed);
+ return removed;
+ }
+ }
+
public RemoteServiceAdmin(Bundle bundle) {
this.bundle = bundle;
Assert.isNotNull(bundle);
@@ -114,39 +136,51 @@ public class RemoteServiceAdmin implements
// create and register a default package version comparator
packageVersionComparator = new PackageVersionComparator();
}
-
+
private HostContainerSelector hostContainerSelector;
private ServiceRegistration hostContainerSelectorRegistration;
-
+
private ConsumerContainerSelector consumerContainerSelector;
private ServiceRegistration consumerContainerSelectorRegistration;
private void setupDefaultContainerSelectors() {
- // Only setup defaults if it hasn't already been done by some other Remote Service Admin instance
+ // Only setup defaults if it hasn't already been done by some other
+ // Remote Service Admin instance
Properties props = new Properties();
- props.put(org.osgi.framework.Constants.SERVICE_RANKING, new Integer(Integer.MIN_VALUE));
- // host container selector. register default only if none exist
+ props.put(org.osgi.framework.Constants.SERVICE_RANKING, new Integer(
+ Integer.MIN_VALUE));
+ // host container selector. register default only if none exist
ServiceReference[] hostContainerSelectorRefs = null;
try {
- hostContainerSelectorRefs = getContext().getServiceReferences(IHostContainerSelector.class.getName(), null);
+ hostContainerSelectorRefs = getContext().getServiceReferences(
+ IHostContainerSelector.class.getName(), null);
} catch (InvalidSyntaxException e) {
// will not happen
}
- if (hostContainerSelectorRefs == null || hostContainerSelectorRefs.length == 0) {
+ if (hostContainerSelectorRefs == null
+ || hostContainerSelectorRefs.length == 0) {
hostContainerSelector = new HostContainerSelector(
hostDefaultConfigTypes, hostAutoCreateContainer);
- hostContainerSelectorRegistration = getContext().registerService(IHostContainerSelector.class.getName(), hostContainerSelector, props);
+ hostContainerSelectorRegistration = getContext().registerService(
+ IHostContainerSelector.class.getName(),
+ hostContainerSelector, props);
}
- // consumer container selector. register default only if none exist
+ // consumer container selector. register default only if none exist
ServiceReference[] consumerContainerSelectorRefs = null;
try {
- consumerContainerSelectorRefs = getContext().getServiceReferences(IConsumerContainerSelector.class.getName(), null);
+ consumerContainerSelectorRefs = getContext().getServiceReferences(
+ IConsumerContainerSelector.class.getName(), null);
} catch (InvalidSyntaxException e) {
// will not happen
}
- if (consumerContainerSelectorRefs == null || consumerContainerSelectorRefs.length == 0) {
- consumerContainerSelector = new ConsumerContainerSelector(consumerAutoCreateContainer);
- consumerContainerSelectorRegistration = getContext().registerService(IConsumerContainerSelector.class.getName(), consumerContainerSelector, props);
+ if (consumerContainerSelectorRefs == null
+ || consumerContainerSelectorRefs.length == 0) {
+ consumerContainerSelector = new ConsumerContainerSelector(
+ consumerAutoCreateContainer);
+ consumerContainerSelectorRegistration = getContext()
+ .registerService(
+ IConsumerContainerSelector.class.getName(),
+ consumerContainerSelector, props);
}
}
@@ -168,7 +202,7 @@ public class RemoteServiceAdmin implements
consumerContainerSelector = null;
}
}
-
+
// RemoteServiceAdmin service interface impl methods
public Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> exportService(
ServiceReference serviceReference,
@@ -259,7 +293,7 @@ public class RemoteServiceAdmin implements
exportRegistrations);
}
- class ExportEndpoint {
+ public class ExportEndpoint {
private ServiceReference serviceReference;
private EndpointDescription endpointDescription;
@@ -344,7 +378,7 @@ public class RemoteServiceAdmin implements
}
- class ExportRegistration implements
+ public class ExportRegistration implements
org.osgi.service.remoteserviceadmin.ExportRegistration {
private ExportEndpoint exportEndpoint;
@@ -382,12 +416,20 @@ public class RemoteServiceAdmin implements
synchronized boolean match(ServiceReference serviceReference,
ID containerID) {
+ ServiceReference ourServiceReference = getServiceReference();
+ if (ourServiceReference == null)
+ return false;
+ boolean serviceReferenceCompare = ourServiceReference
+ .equals(serviceReference);
+ // If the second parameter is null, then we compare only on service
+ // references
+ if (containerID == null)
+ return serviceReferenceCompare;
ID ourContainerID = getContainerID();
if (ourContainerID == null)
return false;
- ServiceReference ourServiceReference = getServiceReference();
- return ourContainerID.equals(containerID)
- && ourServiceReference.equals(serviceReference);
+ return serviceReferenceCompare
+ && ourContainerID.equals(containerID);
}
synchronized ExportEndpoint getExportEndpoint(
@@ -419,13 +461,15 @@ public class RemoteServiceAdmin implements
exportEndpoint = null;
}
}
- if (closed)
+ if (closed) {
+ removeExportRegistration(this);
publishEvent(
new RemoteServiceAdminEvent(
endpointDescription.getContainerID(),
RemoteServiceAdminEvent.EXPORT_UNREGISTRATION,
getBundle(), exportReference, t),
endpointDescription);
+ }
}
public synchronized Throwable getException() {
@@ -439,7 +483,7 @@ public class RemoteServiceAdmin implements
}
- class ExportReference implements
+ public class ExportReference implements
org.osgi.service.remoteserviceadmin.ExportReference {
private ServiceReference serviceReference;
@@ -471,7 +515,7 @@ public class RemoteServiceAdmin implements
}
- class ImportEndpoint {
+ public class ImportEndpoint {
private IRemoteServiceContainerAdapter rsContainerAdapter;
private EndpointDescription endpointDescription;
@@ -585,7 +629,7 @@ public class RemoteServiceAdmin implements
}
- class ImportRegistration implements
+ public class ImportRegistration implements
org.osgi.service.remoteserviceadmin.ImportRegistration {
private ImportEndpoint importEndpoint;
@@ -644,13 +688,15 @@ public class RemoteServiceAdmin implements
importEndpoint = null;
}
}
- if (closed)
+ if (closed) {
+ removeImportRegistration(this);
publishEvent(
new RemoteServiceAdminEvent(
endpointDescription.getContainerID(),
RemoteServiceAdminEvent.IMPORT_UNREGISTRATION,
getBundle(), importReference, t),
endpointDescription);
+ }
}
public synchronized Throwable getException() {
@@ -998,7 +1044,6 @@ public class RemoteServiceAdmin implements
private Object consumerContainerSelectorTrackerLock = new Object();
private ServiceTracker consumerContainerSelectorTracker;
-
private void closeConsumerContainerSelectorTracker() {
synchronized (consumerContainerSelectorTrackerLock) {
@@ -1015,7 +1060,7 @@ public class RemoteServiceAdmin implements
private Object hostContainerSelectorTrackerLock = new Object();
private ServiceTracker hostContainerSelectorTracker;
-
+
private void closeHostContainerSelectorTracker() {
synchronized (hostContainerSelectorTrackerLock) {
if (hostContainerSelectorTracker != null) {
@@ -1032,21 +1077,26 @@ public class RemoteServiceAdmin implements
protected IHostContainerSelector getHostContainerSelector() {
synchronized (hostContainerSelectorTrackerLock) {
if (hostContainerSelectorTracker == null) {
- hostContainerSelectorTracker = new ServiceTracker(getContext(),IHostContainerSelector.class.getName(),null);
+ hostContainerSelectorTracker = new ServiceTracker(getContext(),
+ IHostContainerSelector.class.getName(), null);
hostContainerSelectorTracker.open();
}
}
- return (IHostContainerSelector) hostContainerSelectorTracker.getService();
+ return (IHostContainerSelector) hostContainerSelectorTracker
+ .getService();
}
protected IConsumerContainerSelector getConsumerContainerSelector() {
synchronized (consumerContainerSelectorTrackerLock) {
if (consumerContainerSelectorTracker == null) {
- consumerContainerSelectorTracker = new ServiceTracker(getContext(),IConsumerContainerSelector.class.getName(),null);
+ consumerContainerSelectorTracker = new ServiceTracker(
+ getContext(),
+ IConsumerContainerSelector.class.getName(), null);
consumerContainerSelectorTracker.open();
}
}
- return (IConsumerContainerSelector) consumerContainerSelectorTracker.getService();
+ return (IConsumerContainerSelector) consumerContainerSelectorTracker
+ .getService();
}
private Version getPackageVersion(Bundle registeringBundle,
@@ -1603,19 +1653,6 @@ public class RemoteServiceAdmin implements
Map<String, Object> overridingProperties,
String[] exportedInterfaces, String[] serviceIntents,
IRemoteServiceContainer rsContainer) {
- ID containerID = rsContainer.getContainer().getID();
- trace("exportService",
- "serviceReference="
- + serviceReference
- + ",overridingProperties="
- + overridingProperties
- + ",exportedInterfaces="
- + Arrays.asList(exportedInterfaces)
- + ",serviceIntents="
- + ((serviceIntents == null) ? "null" : Arrays.asList(
- serviceIntents).toString()) + ",rsContainerID="
- + containerID);
- IRemoteServiceRegistration remoteRegistration = null;
Map endpointDescriptionProperties = createExportEndpointDescriptionProperties(
serviceReference, overridingProperties, exportedInterfaces,
@@ -1627,9 +1664,10 @@ public class RemoteServiceAdmin implements
IRemoteServiceContainerAdapter containerAdapter = rsContainer
.getContainerAdapter();
- Throwable exception = null;
// Register remote service via ECF container adapter to create
// remote service registration
+ IRemoteServiceRegistration remoteRegistration = null;
+ Throwable exception = null;
try {
if (containerAdapter instanceof IOSGiRemoteServiceContainerAdapter) {
IOSGiRemoteServiceContainerAdapter osgiContainerAdapter = (IOSGiRemoteServiceContainerAdapter) containerAdapter;
@@ -1772,37 +1810,28 @@ public class RemoteServiceAdmin implements
return null;
}
- protected Collection<ImportRegistration> unimportService(
- IRemoteServiceID remoteServiceID) {
- trace("unimport", "remoteServiceID=" + remoteServiceID);
+ protected void unimportService(IRemoteServiceID remoteServiceID) {
List<ImportRegistration> removedRegistrations = new ArrayList<ImportRegistration>();
synchronized (importedRegistrations) {
for (Iterator<ImportRegistration> i = importedRegistrations
.iterator(); i.hasNext();) {
ImportRegistration importRegistration = i.next();
- if (importRegistration != null) {
- if (importRegistration.match(remoteServiceID)) {
- removedRegistrations.add(importRegistration);
- i.remove();
- }
- }
+ if (importRegistration != null
+ && importRegistration.match(remoteServiceID))
+ removedRegistrations.add(importRegistration);
}
}
// Now close all of them
- for (ImportRegistration removedReg : removedRegistrations)
+ for (ImportRegistration removedReg : removedRegistrations) {
+ trace("unimportService", "closing importRegistration=" + removedReg);
removedReg.close();
- return removedRegistrations;
+ }
}
protected class RemoteServiceListener implements IRemoteServiceListener {
public void handleServiceEvent(IRemoteServiceEvent event) {
- if (event instanceof IRemoteServiceUnregisteredEvent) {
- Collection<ImportRegistration> removedRegistrations = unimportService(event
- .getReference().getID());
- trace("RemoteServiceListener.handleServiceEvent",
- "Removed importRegistrations=" + removedRegistrations
- + " via event=" + event);
- }
+ if (event instanceof IRemoteServiceUnregisteredEvent)
+ unimportService(event.getReference().getID());
}
}
diff --git a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdminEvent.java b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdminEvent.java
index 16e82cac1..fb7336cfc 100644
--- a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdminEvent.java
+++ b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/RemoteServiceAdminEvent.java
@@ -26,4 +26,12 @@ public class RemoteServiceAdminEvent extends
return containerID;
}
+ public String toString() {
+ return "RemoteServiceAdminEvent[containerID=" + containerID
+ + ", getType()=" + getType() + ", getSource()=" + getSource()
+ + ", getException()=" + getException()
+ + ", getImportReference()=" + getImportReference()
+ + ", getExportReference()=" + getExportReference() + "]";
+ }
+
}
diff --git a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/TopologyManager.java b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/TopologyManager.java
index 23c6ca1e8..d4f9c40cd 100644
--- a/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/TopologyManager.java
+++ b/incubation/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/TopologyManager.java
@@ -9,19 +9,12 @@
******************************************************************************/
package org.eclipse.ecf.osgi.services.remoteserviceadmin;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
import java.util.Properties;
-import java.util.TreeMap;
-import org.eclipse.ecf.internal.osgi.services.remoteserviceadmin.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.hooks.service.EventHook;
import org.osgi.service.remoteserviceadmin.EndpointListener;
@@ -33,9 +26,6 @@ public class TopologyManager extends AbstractTopologyManager implements
private ServiceRegistration eventHookRegistration;
- protected Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> exportedRegistrations = new ArrayList<org.osgi.service.remoteserviceadmin.ExportRegistration>();
- protected Collection<org.osgi.service.remoteserviceadmin.ImportRegistration> importedRegistrations = new ArrayList<org.osgi.service.remoteserviceadmin.ImportRegistration>();
-
public TopologyManager(BundleContext context) {
super(context);
}
@@ -92,49 +82,6 @@ public class TopologyManager extends AbstractTopologyManager implements
+ endpoint + ",matchedFilter=" + matchedFilter);
}
- private void handleEndpointAdded(EndpointDescription endpointDescription) {
- trace("handleEndpointAdded", "endpointDescription="
- + endpointDescription);
- // First, select importing remote service admin
- org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
-
- if (rsa == null) {
- logError("handleEndpointAdded",
- "RemoteServiceAdmin not found for importing endpointDescription="
- + endpointDescription);
- return;
- }
- // now call rsa.import
- synchronized (importedRegistrations) {
- org.osgi.service.remoteserviceadmin.ImportRegistration importRegistration = rsa
- .importService(endpointDescription);
- if (importRegistration == null) {
- logError("handleEndpointAdded",
- "Import registration is null for endpointDescription="
- + endpointDescription + " and rsa=" + rsa);
- } else
- importedRegistrations.add(importRegistration);
- }
-
- }
-
- private void handleEndpointRemoved(EndpointDescription endpointDescription) {
- trace("handleEndpointRemoved", "endpointDescription="
- + endpointDescription);
- // First, select importing remote service admin
- org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
- if (rsa == null) {
- logError("handleEndpointRemoved",
- "RemoteServiceAdmin not found for importing endpointDescription="
- + endpointDescription);
- return;
- }
- Collection<RemoteServiceAdmin.ImportRegistration> unimportRegistrations = unimportService(endpointDescription);
- trace("handleEndpointRemoved", "importRegistration="
- + unimportRegistrations + " removed for endpointDescription="
- + endpointDescription);
- }
-
public void event(ServiceEvent event, Collection contexts) {
switch (event.getType()) {
case ServiceEvent.MODIFIED:
@@ -154,162 +101,4 @@ public class TopologyManager extends AbstractTopologyManager implements
}
- private void handleServiceRegistering(ServiceReference serviceReference) {
- // Using OSGI 4.2 Chap 13 Remote Services spec, get the specified remote
- // interfaces for the given service reference
- String[] exportedInterfaces = PropertiesUtil
- .getExportedInterfaces(serviceReference);
- // If no remote interfaces set, then we don't do anything with it
- if (exportedInterfaces == null)
- return;
-
- // Select remote service admin
- org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
-
- // if no remote service admin available, then log error and return
- if (rsa == null) {
- logError("handleServiceRegistered",
- "No RemoteServiceAdmin found for serviceReference="
- + serviceReference
- + ". Remote service NOT EXPORTED");
- return;
- }
-
- // prepare export properties
- Map<String, Object> exportProperties = new TreeMap<String, Object>(
- String.CASE_INSENSITIVE_ORDER);
- exportProperties
- .put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_INTERFACES,
- exportedInterfaces);
- Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> registrations = null;
- synchronized (exportedRegistrations) {
- // Export the remote service using the selected remote service admin
- registrations = rsa.exportService(serviceReference,
- exportProperties);
-
- if (registrations.size() == 0) {
- logError("handleServiceRegistered",
- "No export registrations created by RemoteServiceAdmin="
- + rsa + ". ServiceReference="
- + serviceReference + " NOT EXPORTED");
- return;
- }
- // add them all
- exportedRegistrations.addAll(registrations);
- }
- // publish exported registrations
- for (org.osgi.service.remoteserviceadmin.ExportRegistration reg : registrations) {
- advertiseEndpointDescription((EndpointDescription) reg
- .getExportReference().getExportedEndpoint());
- }
-
- }
-
- private void handleServiceModifying(ServiceReference serviceReference) {
- handleServiceUnregistering(serviceReference);
- handleServiceRegistering(serviceReference);
- }
-
- private void handleServiceUnregistering(ServiceReference serviceReference) {
- org.osgi.service.remoteserviceadmin.RemoteServiceAdmin rsa = getRemoteServiceAdmin();
- if (rsa == null) {
- logError("handleServiceUnregistering",
- "No RemoteServiceAdmin found for serviceReference="
- + serviceReference
- + ". Remote service NOT UNEXPORTED");
- return;
- }
- EndpointDescription[] endpointDescriptions = unexportService(serviceReference);
- if (endpointDescriptions != null) {
- for (int i = 0; i < endpointDescriptions.length; i++) {
- unadvertiseEndpointDescription(endpointDescriptions[i]);
- }
- }
- }
-
- protected RemoteServiceAdmin.ExportRegistration[] findExportRegistrations(
- ServiceReference serviceReference) {
- List<RemoteServiceAdmin.ExportRegistration> results = new ArrayList<RemoteServiceAdmin.ExportRegistration>();
- for (org.osgi.service.remoteserviceadmin.ExportRegistration reg : exportedRegistrations) {
- RemoteServiceAdmin.ExportRegistration exportReg = (RemoteServiceAdmin.ExportRegistration) reg;
- if (exportReg.match(serviceReference))
- results.add(exportReg);
- }
- return results
- .toArray(new RemoteServiceAdmin.ExportRegistration[results
- .size()]);
- }
-
- protected EndpointDescription[] unexportService(
- ServiceReference serviceReference) {
- List<EndpointDescription> endpointDescriptions = new ArrayList<EndpointDescription>();
- synchronized (exportedRegistrations) {
- RemoteServiceAdmin.ExportRegistration[] exportRegs = findExportRegistrations(serviceReference);
- if (exportRegs != null) {
- for (int i = 0; i < exportRegs.length; i++) {
- org.osgi.service.remoteserviceadmin.ExportReference exportRef = null;
- try {
- exportRef = exportRegs[i].getExportReference();
- if (exportRef != null) {
- org.osgi.service.remoteserviceadmin.EndpointDescription endpointDescription = exportRef
- .getExportedEndpoint();
- if (endpointDescription != null
- && endpointDescription instanceof EndpointDescription) {
- endpointDescriptions
- .add((EndpointDescription) endpointDescription);
- }
- exportRegs[i].close();
- exportedRegistrations.remove(exportRegs[i]);
- }
- } catch (IllegalStateException e) {
- // no export ref because ExportRegistration not
- // initialized properly
- logWarning("unexportService",
- "IllegalStateException accessing export reference for exportRegistration="
- + exportRegs[i]);
- }
- }
- }
- }
- return endpointDescriptions
- .toArray(new EndpointDescription[endpointDescriptions.size()]);
- }
-
- protected Collection<RemoteServiceAdmin.ImportRegistration> unimportService(
- EndpointDescription endpointDescription) {
- trace("unimportService", "endpointDescription=" + endpointDescription);
- List<RemoteServiceAdmin.ImportRegistration> removedRegistrations = new ArrayList<RemoteServiceAdmin.ImportRegistration>();
- synchronized (importedRegistrations) {
- for (Iterator<org.osgi.service.remoteserviceadmin.ImportRegistration> i = importedRegistrations
- .iterator(); i.hasNext();) {
- RemoteServiceAdmin.ImportRegistration reg = (RemoteServiceAdmin.ImportRegistration) i
- .next();
- org.osgi.service.remoteserviceadmin.ImportReference importReference = null;
- try {
- importReference = reg.getImportReference();
- if (importReference != null) {
- org.osgi.service.remoteserviceadmin.EndpointDescription importedDescription = importReference
- .getImportedEndpoint();
- if (importedDescription != null
- && importedDescription
- .equals(endpointDescription)) {
- removedRegistrations.add(reg);
- i.remove();
- }
- }
- } catch (IllegalStateException e) {
- // no export ref because ExportRegistration not
- // initialized properly
- logWarning("unimportService",
- "IllegalStateException accessing export reference for importRegistration="
- + reg);
- }
- }
- }
- // Now close all of them
- for (RemoteServiceAdmin.ImportRegistration removedReg : removedRegistrations)
- removedReg.close();
- return removedRegistrations;
- }
-
}
diff --git a/incubation/bundles/org.eclipse.ecf.tests.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/tests/osgi/services/remoteserviceadmin/AbstractRemoteServiceRegisterTest.java b/incubation/bundles/org.eclipse.ecf.tests.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/tests/osgi/services/remoteserviceadmin/AbstractRemoteServiceRegisterTest.java
index e95abdc94..56e703c6d 100644
--- a/incubation/bundles/org.eclipse.ecf.tests.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/tests/osgi/services/remoteserviceadmin/AbstractRemoteServiceRegisterTest.java
+++ b/incubation/bundles/org.eclipse.ecf.tests.osgi.services.remoteserviceadmin/src/org/eclipse/ecf/tests/osgi/services/remoteserviceadmin/AbstractRemoteServiceRegisterTest.java
@@ -10,6 +10,7 @@
package org.eclipse.ecf.tests.osgi.services.remoteserviceadmin;
import java.util.Properties;
+import java.util.Vector;
import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.ContainerTypeDescription;
@@ -20,16 +21,20 @@ import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
import org.eclipse.ecf.remoteservice.IRemoteServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.remoteserviceadmin.RemoteConstants;
+import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent;
+import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener;
public abstract class AbstractRemoteServiceRegisterTest extends
AbstractDistributionTest {
protected static final int REGISTER_WAIT = 2000;
private ServiceRegistration registration;
-
+ protected Vector<RemoteServiceAdminEvent> remoteServiceAdminEvents = new Vector<RemoteServiceAdminEvent>();
+
protected abstract String getServerContainerTypeName();
protected void tearDown() throws Exception {
+ remoteServiceAdminEvents.clear();
// Then unregister
if(registration != null) {
registration.unregister();
@@ -193,5 +198,45 @@ public abstract class AbstractRemoteServiceRegisterTest extends
registerWaitAndUnregister(props, true);
}
+
+ protected boolean containsEventType(int eventType) {
+ for(RemoteServiceAdminEvent event: remoteServiceAdminEvents) if (event.getType() == eventType) return true;
+ return false;
+ }
+
+ protected RemoteServiceAdminListener createRemoteServiceAdminListener() {
+ return new RemoteServiceAdminListener() {
+ public void remoteAdminEvent(RemoteServiceAdminEvent event) {
+ System.out.println("remoteAdminEvent event="+event);
+ remoteServiceAdminEvents.add(event);
+ }
+ };
+ }
+
+ public void testRemoteServiceAdminListener() throws Exception {
+ RemoteServiceAdminListener listener = createRemoteServiceAdminListener();
+ ServiceRegistration listenerReg = getContext().registerService(RemoteServiceAdminListener.class.getName(), listener, null);
+
+ Properties props = getServiceProperties();
+ registration = registerDefaultService(props);
+ // Wait a while
+ Thread.sleep(REGISTER_WAIT);
+
+ assertTrue(remoteServiceAdminEvents.size() > 0);
+ assertTrue(containsEventType(RemoteServiceAdminEvent.EXPORT_REGISTRATION));
+
+ // Now bring down
+ registration.unregister();
+ registration = null;
+
+ // Wait a while
+ Thread.sleep(REGISTER_WAIT);
+
+ assertTrue(remoteServiceAdminEvents.size() > 2);
+ assertTrue(containsEventType(RemoteServiceAdminEvent.EXPORT_UNREGISTRATION));
+
+ // finally unregister the listenerReg
+ listenerReg.unregister();
+ }
}

Back to the top