diff options
author | slewis | 2009-04-22 05:45:47 +0000 |
---|---|---|
committer | slewis | 2009-04-22 05:45:47 +0000 |
commit | 8f3dc5e9ca399415a69b6cd038ba29b63ea34362 (patch) | |
tree | 456bebd96c74898c1c0561f0769959bea862a740 /compendium/bundles | |
parent | 909955250eaa408a120001ccd806f83276b2cd1e (diff) | |
download | org.eclipse.ecf-8f3dc5e9ca399415a69b6cd038ba29b63ea34362.tar.gz org.eclipse.ecf-8f3dc5e9ca399415a69b6cd038ba29b63ea34362.tar.xz org.eclipse.ecf-8f3dc5e9ca399415a69b6cd038ba29b63ea34362.zip |
Added AbstractContainerFinder and DefaultHostContainerFinder, DefaultProxyContainerFinder
Diffstat (limited to 'compendium/bundles')
12 files changed, 534 insertions, 344 deletions
diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/.options b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/.options index 3df9205ff..726aeaa7f 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/.options +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/.options @@ -19,3 +19,4 @@ org.eclipse.ecf.osgi.services.distribution/debug/eventhook=true org.eclipse.ecf.osgi.services.distribution/debug/discoveredservicetracker=true +org.eclipse.ecf.osgi.services.distribution/debug/containerfinder=true diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/META-INF/MANIFEST.MF b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/META-INF/MANIFEST.MF index 8eb31d0c8..1fbc7a9be 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/META-INF/MANIFEST.MF +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/META-INF/MANIFEST.MF @@ -8,6 +8,7 @@ Bundle-Vendor: %pluginProvider Bundle-RequiredExecutionEnvironment: J2SE-1.4 Import-Package: org.eclipse.ecf.core, org.eclipse.ecf.core.identity, + org.eclipse.ecf.core.security, org.eclipse.ecf.core.util, org.eclipse.ecf.discovery;version="3.0.0", org.eclipse.ecf.discovery.identity, diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/Activator.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/Activator.java index dd2222fd4..f7cbf96b4 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/Activator.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/Activator.java @@ -98,30 +98,39 @@ public class Activator implements BundleActivator { public void start(BundleContext ctxt) throws Exception { plugin = this; this.context = ctxt; - this.distributionProvider = new DistributionProviderImpl(); - addDiscoveredServiceTracker(); - addServiceRegistryHooks(); - addDistributionProvider(); - } - private void addDiscoveredServiceTracker() { + // Create distribution provider impl + this.distributionProvider = new DistributionProviderImpl(); + // Create discovered service tracker impl DiscoveredServiceTrackerImpl dstImpl = new DiscoveredServiceTrackerImpl( this.distributionProvider, new ThreadsExecutor()); + // Register discovered service tracker this.discoveredServiceTrackerRegistration = this.context .registerService(DiscoveredServiceTracker.class.getName(), dstImpl, null); + + // Set service ranking to Integer.MIN_VALUE so that other impls + // will be prefered over the default one + final Properties proxyContainerFinderProps = new Properties(); + proxyContainerFinderProps.put(Constants.SERVICE_RANKING, + Integer.MIN_VALUE); + // Register default proxy container finder this.proxyrsContainerFinderRegistration = this.context.registerService( - IProxyContainerFinder.class.getName(), dstImpl, - null); - } + IProxyContainerFinder.class.getName(), + new DefaultProxyContainerFinder(), proxyContainerFinderProps); - private void addServiceRegistryHooks() { // register the event hook to get informed when new services appear final EventHookImpl hook = new EventHookImpl(distributionProvider); this.eventHookRegistration = this.context.registerService( EventHook.class.getName(), hook, null); + + // register the default host container finder + final Properties hostContainerFinderProps = new Properties(); + hostContainerFinderProps.put(Constants.SERVICE_RANKING, + Integer.MIN_VALUE); this.hostrsContainerFinderRegistration = this.context.registerService( - IHostContainerFinder.class.getName(), hook, null); + IHostContainerFinder.class.getName(), + new DefaultHostContainerFinder(), hostContainerFinderProps); // register all existing services which have the marker property try { @@ -134,11 +143,10 @@ public class Activator implements BundleActivator { } } } catch (InvalidSyntaxException e) { - e.printStackTrace(); + // not possible } - } - private void addDistributionProvider() { + // Setup properties for the distribution provider final Dictionary properties = new Hashtable(); properties.put(DistributionProvider.PROP_KEY_VENDOR_NAME, DistributionProviderImpl.VENDOR_NAME); @@ -148,30 +156,19 @@ public class Activator implements BundleActivator { DistributionProviderImpl.PRODUCT_VERSION); properties.put(DistributionProvider.PROP_KEY_SUPPORTED_INTENTS, distributionProvider.getSupportedIntents()); + // Register distribution provider this.distributionProviderRegistration = this.context.registerService( DistributionProvider.class.getName(), distributionProvider, properties); } - private void removeServiceRegistryHooks() { - if (this.eventHookRegistration != null) { - this.eventHookRegistration.unregister(); - this.eventHookRegistration = null; - } - if (this.hostrsContainerFinderRegistration != null) { - this.hostrsContainerFinderRegistration.unregister(); - this.hostrsContainerFinderRegistration = null; - } - } - - private void removeDistributionProvider() { - if (this.distributionProviderRegistration != null) { - this.distributionProviderRegistration.unregister(); - this.distributionProviderRegistration = null; - } - } - - private void removeDiscoveredServiceTracker() { + /* + * (non-Javadoc) + * + * @see + * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext ctxt) throws Exception { if (this.discoveredServiceTrackerRegistration != null) { this.discoveredServiceTrackerRegistration.unregister(); this.discoveredServiceTrackerRegistration = null; @@ -180,18 +177,18 @@ public class Activator implements BundleActivator { this.proxyrsContainerFinderRegistration.unregister(); this.proxyrsContainerFinderRegistration = null; } - } - - /* - * (non-Javadoc) - * - * @see - * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) - */ - public void stop(BundleContext ctxt) throws Exception { - removeDiscoveredServiceTracker(); - removeDistributionProvider(); - removeServiceRegistryHooks(); + if (this.distributionProviderRegistration != null) { + this.distributionProviderRegistration.unregister(); + this.distributionProviderRegistration = null; + } + if (this.eventHookRegistration != null) { + this.eventHookRegistration.unregister(); + this.eventHookRegistration = null; + } + if (this.hostrsContainerFinderRegistration != null) { + this.hostrsContainerFinderRegistration.unregister(); + this.hostrsContainerFinderRegistration = null; + } if (containerManagerTracker != null) { containerManagerTracker.close(); containerManagerTracker = null; @@ -226,26 +223,22 @@ public class Activator implements BundleActivator { return (IContainerManager) containerManagerTracker.getService(); } - public synchronized IProxyContainerFinder[] getProxyRemoteServiceContainerFinders() { + public synchronized IProxyContainerFinder getProxyRemoteServiceContainerFinder() { if (proxyrsContainerFinder == null) { proxyrsContainerFinder = new ServiceTracker(this.context, IProxyContainerFinder.class.getName(), null); proxyrsContainerFinder.open(); } - Object[] svcs = (Object[]) proxyrsContainerFinder.getServices(); - return (IProxyContainerFinder[]) Arrays.asList(svcs) - .toArray(new IProxyContainerFinder[] {}); + return (IProxyContainerFinder) proxyrsContainerFinder.getService(); } - public synchronized IHostContainerFinder[] getHostRemoteServiceContainerFinders() { + public synchronized IHostContainerFinder getHostRemoteServiceContainerFinder() { if (hostrsContainerFinder == null) { hostrsContainerFinder = new ServiceTracker(this.context, IHostContainerFinder.class.getName(), null); hostrsContainerFinder.open(); } - Object[] svcs = (Object[]) hostrsContainerFinder.getServices(); - return (IHostContainerFinder[]) Arrays.asList(svcs) - .toArray(new IHostContainerFinder[] {}); + return (IHostContainerFinder) hostrsContainerFinder.getService(); } public IAdapterManager getAdapterManager() { diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DebugOptions.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DebugOptions.java index ae9dbd604..ca697344a 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DebugOptions.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DebugOptions.java @@ -28,4 +28,6 @@ public interface DebugOptions { public static final String DISCOVEREDSERVICETRACKER = DEBUG + "/discoveredservicetracker"; + public static final String CONTAINERFINDER = DEBUG + "/containerfinder"; + } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DiscoveredServiceTrackerImpl.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DiscoveredServiceTrackerImpl.java index b91258aa5..bcc735ab2 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DiscoveredServiceTrackerImpl.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/DiscoveredServiceTrackerImpl.java @@ -10,11 +10,10 @@ package org.eclipse.ecf.internal.osgi.services.distribution; import java.util.*; -import org.eclipse.core.runtime.*; -import org.eclipse.ecf.core.*; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.ecf.core.ContainerConnectException; import org.eclipse.ecf.core.identity.ID; -import org.eclipse.ecf.core.identity.Namespace; -import org.eclipse.ecf.core.util.Trace; import org.eclipse.ecf.discovery.identity.IServiceID; import org.eclipse.ecf.osgi.services.discovery.*; import org.eclipse.ecf.osgi.services.distribution.IProxyContainerFinder; @@ -28,55 +27,15 @@ import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceRegistration; import org.osgi.service.discovery.*; -public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker, - IProxyContainerFinder { - - DistributionProviderImpl distributionProvider; - IExecutor executor; - IProgressMonitor executorProgressMonitor; - - List serviceLocations = new ArrayList(); - - private boolean addDiscoveredServiceID(ECFServiceEndpointDescription desc) { - if (desc == null) - return false; - synchronized (serviceLocations) { - return serviceLocations.add(new DiscoveredServiceID(desc - .getServiceID().getLocation(), desc.getRemoteServiceId())); - } - } - - private boolean removeDiscoveredServiceID(ECFServiceEndpointDescription desc) { - if (desc == null) - return false; - synchronized (serviceLocations) { - return serviceLocations.remove(new DiscoveredServiceID(desc - .getServiceID().getLocation(), desc.getRemoteServiceId())); - } - } - - private boolean containsDiscoveredServiceID( - ECFServiceEndpointDescription desc) { - if (desc == null) - return false; - synchronized (serviceLocations) { - return serviceLocations.contains(new DiscoveredServiceID(desc - .getServiceID().getLocation(), desc.getRemoteServiceId())); - } - } - - public DiscoveredServiceTrackerImpl(DistributionProviderImpl dp, - IExecutor executor) { - this.distributionProvider = dp; - this.executor = executor; - } +public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker { + private DistributionProviderImpl distributionProvider; + private IExecutor executor; + private List serviceLocations = new ArrayList(); // <Map<containerID><RemoteServiceRegistration> - - Map discoveredRemoteServiceRegistrations = Collections + private Map discoveredRemoteServiceRegistrations = Collections .synchronizedMap(new HashMap()); - - List ecfRemoteServiceProperties = Arrays.asList(new String[] { + private List ecfRemoteServiceProperties = Arrays.asList(new String[] { Constants.SERVICE_ID, Constants.OBJECTCLASS, IServicePublication.PROP_KEY_ENDPOINT_ID, IServicePublication.PROP_KEY_ENDPOINT_INTERFACE_NAME, @@ -85,6 +44,12 @@ public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker, IServicePublication.PROP_KEY_SERVICE_INTERFACE_VERSION, IServicePublication.PROP_KEY_SERVICE_PROPERTIES }); + public DiscoveredServiceTrackerImpl(DistributionProviderImpl dp, + IExecutor executor) { + this.distributionProvider = dp; + this.executor = executor; + } + /* * (non-Javadoc) * @@ -99,19 +64,14 @@ public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker, return; } int notificationType = notification.getType(); - ServiceEndpointDescription notificationDescription = notification - .getServiceEndpointDescription(); - - trace("serviceChanged", "type=" + notificationType - + ",notificationDescription=" + notificationDescription); - switch (notificationType) { case DiscoveredServiceNotification.AVAILABLE: ECFServiceEndpointDescription adesc = null; try { // If the service endpoint description is not ECF's then we // don't process it - adesc = getECFserviceEndpointDescription(notificationDescription); + adesc = getECFserviceEndpointDescription(notification + .getServiceEndpointDescription()); } catch (Exception e) { logError("serviceChanged.AVAILABLE", "Error creating ECF endpoint description", e); @@ -144,7 +104,8 @@ public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker, break; case DiscoveredServiceNotification.UNAVAILABLE: try { - ECFServiceEndpointDescription udesc = getECFserviceEndpointDescription(notificationDescription); + ECFServiceEndpointDescription udesc = getECFserviceEndpointDescription(notification + .getServiceEndpointDescription()); // If it's not for us then return if (udesc == null) return; @@ -196,43 +157,27 @@ public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker, } } - private IRemoteServiceContainer[] findRemoteServiceContainersViaService( + private IRemoteServiceContainer[] findRemoteServiceContainers( IServiceID serviceID, IServiceEndpointDescription description, IProgressMonitor monitor) { Activator activator = Activator.getDefault(); if (activator == null) return new IRemoteServiceContainer[0]; - IProxyContainerFinder[] finders = activator - .getProxyRemoteServiceContainerFinders(); - if (finders == null || finders.length == 0) { + IProxyContainerFinder finder = activator + .getProxyRemoteServiceContainerFinder(); + if (finder == null) { logError("findRemoteServiceContainersViaService", "No container finders available"); return new IRemoteServiceContainer[0]; } - List result = new ArrayList(); - for (int i = 0; i < finders.length; i++) { - IRemoteServiceContainer[] foundRSContainers = finders[i] - .findProxyContainers(serviceID, description, - monitor); - if (foundRSContainers != null && foundRSContainers.length > 0) { - trace("findRemoteServiceContainersViaService", - "findRemoteServiceContainers finder=" + finders[i] - + " ecfSED=" + description - + " foundRSContainers=" - + Arrays.asList(foundRSContainers)); - for (int j = 0; j < foundRSContainers.length; j++) - result.add(foundRSContainers[j]); - } - } - return (IRemoteServiceContainer[]) result - .toArray(new IRemoteServiceContainer[] {}); + return finder.findProxyContainers(serviceID, description, monitor); } private void handleDiscoveredServiceAvailable( ECFServiceEndpointDescription ecfSED, IProgressMonitor monitor) { // Find IRemoteServiceContainers for the given // ECFServiceEndpointDescription via registered services - IRemoteServiceContainer[] rsContainers = findRemoteServiceContainersViaService( + IRemoteServiceContainer[] rsContainers = findRemoteServiceContainers( ecfSED.getServiceID(), ecfSED, monitor); if (rsContainers == null || rsContainers.length == 0) { logError("handleDiscoveredServiceAvailable", @@ -526,107 +471,52 @@ public class DiscoveredServiceTrackerImpl implements DiscoveredServiceTracker, return results; } - protected void trace(String methodName, String message) { - Trace.trace(Activator.PLUGIN_ID, DebugOptions.DISCOVEREDSERVICETRACKER, - this.getClass(), methodName, message); + private boolean addDiscoveredServiceID(ECFServiceEndpointDescription desc) { + synchronized (serviceLocations) { + return serviceLocations.add(new DiscoveredServiceID(desc + .getServiceID().getLocation(), desc.getRemoteServiceId())); + } } - protected void traceException(String methodName, String message, Throwable t) { - Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, - this.getClass(), ((methodName == null) ? "<unknown>" - : methodName) - + ":" + ((message == null) ? "<empty>" : message), t); + private boolean removeDiscoveredServiceID(ECFServiceEndpointDescription desc) { + synchronized (serviceLocations) { + return serviceLocations.remove(new DiscoveredServiceID(desc + .getServiceID().getLocation(), desc.getRemoteServiceId())); + } } - protected void logError(String methodName, String message, Throwable t) { - if (t != null) - traceException(methodName, message, t); - else - trace(methodName, message); - Activator.getDefault() - .log( - new Status(IStatus.ERROR, Activator.PLUGIN_ID, - IStatus.ERROR, this.getClass().getName() - + ":" - + ((methodName == null) ? "<unknown>" - : methodName) - + ":" - + ((message == null) ? "<empty>" - : message), t)); + private boolean containsDiscoveredServiceID( + ECFServiceEndpointDescription desc) { + synchronized (serviceLocations) { + return serviceLocations.contains(new DiscoveredServiceID(desc + .getServiceID().getLocation(), desc.getRemoteServiceId())); + } } - protected void logError(String methodName, String message) { - logError(methodName, message, null); - traceException(methodName, message, null); + protected void trace(String methodName, String message) { + LogUtility.trace(methodName, DebugOptions.DISCOVEREDSERVICETRACKER, + this.getClass(), message); } - private void logWarning(String methodName, String message) { - trace(methodName, "WARNING:" + message); - Activator.getDefault().log( - new Status(IStatus.WARNING, Activator.PLUGIN_ID, - IStatus.WARNING, DiscoveredServiceTrackerImpl.class - .getName() - + ":" - + ((methodName == null) ? "<unknown>" - : methodName) - + ":" - + ((message == null) ? "<empty>" : message), - null)); + protected void traceException(String methodName, String message, Throwable t) { + LogUtility.traceException(methodName, DebugOptions.EXCEPTIONS_CATCHING, + this.getClass(), message, t); } - // Impl of IProxyContainerFinder - public IRemoteServiceContainer[] findProxyContainers( - IServiceID serviceID, - IServiceEndpointDescription endpointDescription, - IProgressMonitor monitor) { - IContainerManager containerManager = Activator.getDefault() - .getContainerManager(); - if (containerManager == null) - return null; - IContainer[] containers = containerManager.getAllContainers(); - if (containers == null) { - // log this? - logWarning("findRSCAs", "No containers found for container manager"); - return new IRemoteServiceContainer[0]; - } - // If the container id is equal to the endpointID, then we don't want to - // include it - List results = new ArrayList(); - for (int i = 0; i < containers.length; i++) { - // If the container under consideration has the same id - // as the endpoint id, then we don't want to consider it - ID containerID = containers[i].getID(); - if (containerID != null - && containerID.equals(endpointDescription - .getECFEndpointID())) { - continue; - } - IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) containers[i] - .getAdapter(IRemoteServiceContainerAdapter.class); - if (adapter != null - && includeRCSAForDescription(containers[i], - endpointDescription)) { - results.add(new RemoteServiceContainer(containers[i], adapter)); - } - } - return (IRemoteServiceContainer[]) results - .toArray(new IRemoteServiceContainer[] {}); + protected void logError(String methodName, String message, Throwable t) { + LogUtility.logError(methodName, DebugOptions.DISCOVEREDSERVICETRACKER, + this.getClass(), message, t); } - private boolean includeRCSAForDescription(IContainer container, - ServiceEndpointDescription description) { - // Then we check the namespace of the endpoint container ID. If it's the - // same as the - // container/adapter under test then we've found a compatible one - String connectNamespaceName = (String) description - .getProperty(IServicePublication.PROP_KEY_ENDPOINT_CONTAINERID_NAMESPACE); - if (connectNamespaceName != null) { - Namespace namespace = container.getConnectNamespace(); - if (namespace != null - && namespace.getName().equals(connectNamespaceName)) - return true; - } - return false; + protected void logError(String methodName, String message) { + LogUtility.logError(methodName, DebugOptions.DISCOVEREDSERVICETRACKER, + this.getClass(), message); + } + + protected void logWarning(String methodName, String message) { + LogUtility + .logWarning(methodName, DebugOptions.DISCOVEREDSERVICETRACKER, + this.getClass(), message); } } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/EventHookImpl.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/EventHookImpl.java index bd12dc014..a4e79d892 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/EventHookImpl.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/EventHookImpl.java @@ -10,7 +10,6 @@ package org.eclipse.ecf.internal.osgi.services.distribution; import java.util.*; -import org.eclipse.ecf.core.ContainerTypeDescription; import org.eclipse.ecf.core.IContainer; import org.eclipse.ecf.core.identity.ID; import org.eclipse.ecf.core.identity.Namespace; @@ -22,8 +21,7 @@ import org.eclipse.ecf.remoteservice.Constants; import org.osgi.framework.*; import org.osgi.service.discovery.ServicePublication; -public class EventHookImpl extends AbstractEventHookImpl implements - IHostContainerFinder { +public class EventHookImpl extends AbstractEventHookImpl { public EventHookImpl(DistributionProviderImpl distributionProvider) { super(distributionProvider); @@ -100,124 +98,18 @@ public class EventHookImpl extends AbstractEventHookImpl implements Activator activator = Activator.getDefault(); if (activator == null) return null; - IHostContainerFinder[] finders = activator - .getHostRemoteServiceContainerFinders(); - if (finders == null || finders.length == 0) { + IHostContainerFinder finder = activator + .getHostRemoteServiceContainerFinder(); + if (finder == null) { logError("findRemoteServiceContainers", "No container finders available"); return null; } - List result = new ArrayList(); - for (int i = 0; i < finders.length; i++) { - IRemoteServiceContainer[] foundRSContainers = finders[i] - .findHostContainers(serviceReference, - remoteInterfaces, remoteConfigurationType, - remoteRequiresIntents); - if (foundRSContainers != null && foundRSContainers.length > 0) { - trace("findRemoteServiceContainersViaService", - "findRemoteServiceContainers finder=" + finders[i] - + " foundRSContainers=" - + Arrays.asList(foundRSContainers)); - for (int j = 0; j < foundRSContainers.length; j++) - result.add(foundRSContainers[j]); - } - } - return (IRemoteServiceContainer[]) result - .toArray(new IRemoteServiceContainer[] {}); - } - - public IRemoteServiceContainer[] findHostContainers( - ServiceReference serviceReference, String[] remoteInterfaces, - String[] remoteConfigurationType, String[] remoteRequiresIntents) { - Collection rsContainers = findRemoteContainersSatisfyingRequiredIntents(remoteRequiresIntents); - List results = new ArrayList(); - for (Iterator i = rsContainers.iterator(); i.hasNext();) { - IRemoteServiceContainer rsContainer = (IRemoteServiceContainer) i - .next(); - if (includeContainer(serviceReference, rsContainer)) - results.add(rsContainer); - } - return (IRemoteServiceContainer[]) results - .toArray(new IRemoteServiceContainer[] {}); - } - - protected boolean includeContainer(ServiceReference serviceReference, - IRemoteServiceContainer rsContainer) { - IContainer container = rsContainer.getContainer(); - Object cID = serviceReference - .getProperty(org.eclipse.ecf.remoteservice.Constants.SERVICE_CONTAINER_ID); - // If the SERVICE_CONTAINER_ID property is not set, then we'll include - // it by default - if (cID == null || !(cID instanceof ID)) { - trace( - "includeContainer", - "serviceReference=" - + serviceReference - + " does not set remote service container id service property. INCLUDING containerID=" - + container.getID() + " in remote registration"); - return true; - } - // Or if the id is specified and it's the same as the containerID under - // consideration - // then it's included - ID containerID = (ID) cID; - if (container.getID().equals(containerID)) { - trace("includeContainer", "serviceReference=" + serviceReference - + " has MATCHING container id=" + containerID - + ". INCLUDING rsca=" + container.getID() - + " in remote registration"); - return true; - } - trace("includeContainer", "serviceReference=" + serviceReference - + " has non-matching id=" + containerID + ". EXCLUDING id=" - + container.getID() + " in remote registration"); - return false; - } - - private Collection findRemoteContainersSatisfyingRequiredIntents( - String[] remoteRequiresIntents) { - List results = new ArrayList(); - IContainer[] containers = Activator.getDefault().getContainerManager() - .getAllContainers(); - if (containers == null || containers.length == 0) - return null; - for (int i = 0; i < containers.length; i++) { - // Check to make sure it's a rs container adapter. If it's not go - // onto next one - IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) containers[i] - .getAdapter(IRemoteServiceContainerAdapter.class); - if (adapter == null) - continue; - // Get container type description and intents - ContainerTypeDescription description = Activator.getDefault() - .getContainerManager().getContainerTypeDescription( - containers[i].getID()); - // If it has no description continue - if (description == null) - continue; - List supportedIntents = Arrays.asList(description - .getSupportedIntents()); - boolean hasIntents = true; - if (remoteRequiresIntents != null) { - for (int j = 0; j < remoteRequiresIntents.length; j++) { - if (!supportedIntents.contains(remoteRequiresIntents[j])) - hasIntents = false; - } - } - if (hasIntents) { - trace("findHostRemoteServiceContainers.include", "containerID=" - + containers[i].getID()); - results.add(new RemoteServiceContainer(containers[i], adapter)); - } else { - trace("findHostRemoteServiceContainers.exclude", "containerID=" - + containers[i].getID() + " supported intents=" - + supportedIntents); - } - } - return results; + return finder.findHostContainers(serviceReference, remoteInterfaces, + remoteConfigurationType, remoteRequiresIntents); } - Dictionary getServicePublicationProperties( + private Dictionary getServicePublicationProperties( IRemoteServiceContainer rsContainer, ServiceReference ref, String[] remoteInterfaces, IRemoteServiceRegistration remoteRegistration) { @@ -295,7 +187,7 @@ public class EventHookImpl extends AbstractEventHookImpl implements return result; } - protected Dictionary getPropertiesForRemoteService(ServiceReference sr) { + private Dictionary getPropertiesForRemoteService(ServiceReference sr) { String[] propKeys = sr.getPropertyKeys(); Properties newProps = new Properties(); for (int i = 0; i < propKeys.length; i++) { @@ -305,7 +197,7 @@ public class EventHookImpl extends AbstractEventHookImpl implements return newProps; } - protected Map getServicePropertiesForRemotePublication(ServiceReference sr) { + private Map getServicePropertiesForRemotePublication(ServiceReference sr) { String[] propKeys = sr.getPropertyKeys(); Properties newProps = new Properties(); for (int i = 0; i < propKeys.length; i++) { diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/LogUtility.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/LogUtility.java new file mode 100644 index 000000000..d131d2b2e --- /dev/null +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/LogUtility.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2009 EclipseSource and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * EclipseSource - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.internal.osgi.services.distribution; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.ecf.core.util.Trace; + +public class LogUtility { + + public static void logError(String methodName, String debugOption, + Class clazz, String message) { + logError(methodName, debugOption, clazz, message, null); + traceException(methodName, debugOption, clazz, message, null); + } + + public static void logWarning(String methodName, String debugOption, + Class clazz, String message) { + trace(methodName, debugOption, clazz, "WARNING:" + message); + Activator.getDefault().log( + new Status(IStatus.WARNING, Activator.PLUGIN_ID, + IStatus.WARNING, clazz.getName() + + ":" + + ((methodName == null) ? "<unknown>" + : methodName) + ":" + + ((message == null) ? "<empty>" : message), + null)); + } + + public static void logError(String methodName, String debugOption, + Class clazz, String message, Throwable t) { + if (t != null) + traceException(methodName, debugOption, clazz, message, t); + else + trace(methodName, debugOption, clazz, message); + Activator.getDefault() + .log( + new Status(IStatus.ERROR, Activator.PLUGIN_ID, + IStatus.ERROR, clazz.getName() + + ":" + + ((methodName == null) ? "<unknown>" + : methodName) + + ":" + + ((message == null) ? "<empty>" + : message), t)); + } + + public static void trace(String methodName, String debugOptions, + Class clazz, String message) { + Trace.trace(Activator.PLUGIN_ID, debugOptions, clazz, methodName, + message); + } + + public static void traceException(String methodName, String debugOption, + Class clazz, String message, Throwable t) { + Trace.catching(Activator.PLUGIN_ID, debugOption, clazz, + ((methodName == null) ? "<unknown>" : methodName) + ":" + + ((message == null) ? "<empty>" : message), t); + } + +} diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/RemoteServiceRegistration.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/RemoteServiceRegistration.java index f034c139e..2f3831ac1 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/RemoteServiceRegistration.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/internal/osgi/services/distribution/RemoteServiceRegistration.java @@ -19,7 +19,7 @@ public class RemoteServiceRegistration { private final ECFServiceEndpointDescription serviceEndpointDescription; private final IRemoteServiceContainer rsContainer; - private IRemoteServiceListener listener; + private final IRemoteServiceListener listener; private Map serviceRegistrations = new HashMap(); public RemoteServiceRegistration(ECFServiceEndpointDescription sed, @@ -67,10 +67,9 @@ public class RemoteServiceRegistration { IRemoteServiceReference reference) { if (getContainerAdapter().ungetRemoteService(reference)) { List l = (List) serviceRegistrations.remove(reference.getID()); - if (l != null) { + if (l != null) return (ServiceRegistration[]) l .toArray(new ServiceRegistration[] {}); - } } return null; } @@ -79,9 +78,8 @@ public class RemoteServiceRegistration { List results = new ArrayList(); for (Iterator i = serviceRegistrations.keySet().iterator(); i.hasNext();) { List l = (List) serviceRegistrations.get(i.next()); - if (l != null) { + if (l != null) results.addAll(l); - } } return results; } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/AbstractContainerFinder.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/AbstractContainerFinder.java new file mode 100644 index 000000000..fbd45c1c5 --- /dev/null +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/AbstractContainerFinder.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2009 EclipseSource and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * EclipseSource - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.osgi.services.distribution; + +import java.util.ArrayList; +import java.util.List; +import org.eclipse.ecf.core.*; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.identity.Namespace; +import org.eclipse.ecf.core.security.IConnectContext; +import org.eclipse.ecf.internal.osgi.services.distribution.*; +import org.eclipse.ecf.remoteservice.*; + +/** + * Abstract superclass for IHostContainerFinders and IProxyContainerFinders. + * + */ +public abstract class AbstractContainerFinder { + + public static final IRemoteServiceContainer[] EMPTY_REMOTE_SERVICE_CONTAINER_ARRAY = new IRemoteServiceContainer[] {}; + + protected IContainer[] getAllContainers() { + Activator activator = Activator.getDefault(); + if (activator == null) + return null; + IContainerManager containerManager = activator.getContainerManager(); + if (containerManager == null) + return null; + return containerManager.getAllContainers(); + } + + protected IRemoteServiceContainerAdapter hasRemoteServiceContainerAdapter( + IContainer container) { + return (IRemoteServiceContainerAdapter) container + .getAdapter(IRemoteServiceContainerAdapter.class); + } + + protected IRemoteServiceContainer[] getRemoteServiceContainers( + IContainer[] containers) { + List results = new ArrayList(); + for (int i = 0; i < containers.length; i++) { + IRemoteServiceContainerAdapter adapter = hasRemoteServiceContainerAdapter(containers[i]); + if (adapter != null) + results.add(new RemoteServiceContainer(containers[i], adapter)); + } + return (IRemoteServiceContainer[]) results + .toArray(new IRemoteServiceContainer[] {}); + } + + protected boolean includeContainerWithConnectNamespace( + IContainer container, String connectNamespaceName) { + if (connectNamespaceName != null) { + Namespace namespace = container.getConnectNamespace(); + if (namespace != null + && namespace.getName().equals(connectNamespaceName)) + return true; + } + return false; + } + + protected void connectContainer(IContainer container, ID connectTargetID, + IConnectContext connectContext) throws ContainerConnectException { + trace("connectContainer", "Connecting container=" + container.getID() + + " to connectTargetID=" + connectTargetID); + container.connect(connectTargetID, connectContext); + } + + protected void trace(String methodName, String message) { + LogUtility.trace(methodName, DebugOptions.CONTAINERFINDER, this + .getClass(), message); + } + + protected void traceException(String methodName, String message, Throwable t) { + LogUtility.traceException(methodName, DebugOptions.EXCEPTIONS_CATCHING, + this.getClass(), message, t); + } + + protected void logError(String methodName, String message, Throwable t) { + LogUtility.logError(methodName, DebugOptions.CONTAINERFINDER, this + .getClass(), message, t); + } + + protected void logError(String methodName, String message) { + LogUtility.logError(methodName, DebugOptions.CONTAINERFINDER, this + .getClass(), message); + } + + protected void logWarning(String methodName, String message) { + LogUtility.logWarning(methodName, DebugOptions.CONTAINERFINDER, this + .getClass(), message); + } + +} diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/DefaultHostContainerFinder.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/DefaultHostContainerFinder.java new file mode 100644 index 000000000..a99bf22eb --- /dev/null +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/DefaultHostContainerFinder.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2009 EclipseSource and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * EclipseSource - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.osgi.services.distribution; + +import java.util.*; +import org.eclipse.ecf.core.ContainerTypeDescription; +import org.eclipse.ecf.core.IContainer; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.internal.osgi.services.distribution.Activator; +import org.eclipse.ecf.remoteservice.*; +import org.osgi.framework.ServiceReference; + +/** + * Default implementation of IHostContainerFinder. + * + */ +public class DefaultHostContainerFinder extends AbstractContainerFinder + implements IHostContainerFinder { + + public IRemoteServiceContainer[] findHostContainers( + ServiceReference serviceReference, String[] remoteInterfaces, + String[] remoteConfigurationType, String[] remoteRequiresIntents) { + Collection rsContainers = findRemoteContainersSatisfyingRequiredIntents(remoteRequiresIntents); + List results = new ArrayList(); + for (Iterator i = rsContainers.iterator(); i.hasNext();) { + IRemoteServiceContainer rsContainer = (IRemoteServiceContainer) i + .next(); + if (includeContainer(serviceReference, rsContainer)) + results.add(rsContainer); + } + return (IRemoteServiceContainer[]) results + .toArray(new IRemoteServiceContainer[] {}); + } + + protected Collection findRemoteContainersSatisfyingRequiredIntents( + String[] remoteRequiresIntents) { + List results = new ArrayList(); + IContainer[] containers = Activator.getDefault().getContainerManager() + .getAllContainers(); + if (containers == null || containers.length == 0) + return null; + for (int i = 0; i < containers.length; i++) { + // Check to make sure it's a rs container adapter. If it's not go + // onto next one + IRemoteServiceContainerAdapter adapter = (IRemoteServiceContainerAdapter) containers[i] + .getAdapter(IRemoteServiceContainerAdapter.class); + if (adapter == null) + continue; + // Get container type description and intents + ContainerTypeDescription description = Activator.getDefault() + .getContainerManager().getContainerTypeDescription( + containers[i].getID()); + // If it has no description continue + if (description == null) + continue; + List supportedIntents = Arrays.asList(description + .getSupportedIntents()); + boolean hasIntents = true; + if (remoteRequiresIntents != null) { + for (int j = 0; j < remoteRequiresIntents.length; j++) { + if (!supportedIntents.contains(remoteRequiresIntents[j])) + hasIntents = false; + } + } + if (hasIntents) { + trace("findRemoteContainersSatisfyingRequiredIntents", + "include containerID=" + containers[i].getID()); + results.add(new RemoteServiceContainer(containers[i], adapter)); + } else { + trace("findRemoteContainersSatisfyingRequiredIntents", + "exclude containerID=" + containers[i].getID() + + " supported intents=" + supportedIntents); + } + } + return results; + } + + protected boolean includeContainer(ServiceReference serviceReference, + IRemoteServiceContainer rsContainer) { + IContainer container = rsContainer.getContainer(); + Object cID = serviceReference + .getProperty(org.eclipse.ecf.remoteservice.Constants.SERVICE_CONTAINER_ID); + // If the SERVICE_CONTAINER_ID property is not set, then we'll include + // it by default + if (cID == null || !(cID instanceof ID)) { + trace( + "includeContainer", + "serviceReference=" + + serviceReference + + " does not set remote service container id service property. INCLUDING containerID=" + + container.getID() + " in remote registration"); + return true; + } + // Or if the id is specified and it's the same as the containerID under + // consideration + // then it's included + ID containerID = (ID) cID; + if (container.getID().equals(containerID)) { + trace("includeContainer", "serviceReference=" + serviceReference + + " has MATCHING container id=" + containerID + + ". INCLUDING rsca=" + container.getID() + + " in remote registration"); + return true; + } + trace("includeContainer", "serviceReference=" + serviceReference + + " has non-matching id=" + containerID + ". EXCLUDING id=" + + container.getID() + " in remote registration"); + return false; + } + +} diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/DefaultProxyContainerFinder.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/DefaultProxyContainerFinder.java new file mode 100644 index 000000000..2642aa88d --- /dev/null +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/DefaultProxyContainerFinder.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2009 EclipseSource and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * EclipseSource - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.osgi.services.distribution; + +import java.util.ArrayList; +import java.util.List; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.ecf.core.ContainerConnectException; +import org.eclipse.ecf.core.IContainer; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.security.IConnectContext; +import org.eclipse.ecf.discovery.identity.IServiceID; +import org.eclipse.ecf.osgi.services.discovery.IServiceEndpointDescription; +import org.eclipse.ecf.osgi.services.discovery.IServicePublication; +import org.eclipse.ecf.remoteservice.IRemoteServiceContainer; + +/** + * Default implementation of IProxyContainerFinder. + * + */ +public class DefaultProxyContainerFinder extends AbstractContainerFinder + implements IProxyContainerFinder { + + protected IContainer[] getContainers(IServiceID serviceID, + IServiceEndpointDescription endpointDescription) { + + // Get all containers available + IContainer[] allContainers = getAllContainers(); + // If none then return null + if (allContainers == null) + return null; + + List results = new ArrayList(); + for (int i = 0; i < allContainers.length; i++) { + // Do *not* include containers with same ID as endpoint ID + ID containerID = allContainers[i].getID(); + if (containerID == null + || containerID.equals(endpointDescription + .getECFEndpointID())) + continue; + // And make sure that the namespaces match + if (includeContainerWithConnectNamespace( + allContainers[i], + (String) endpointDescription + .getProperty(IServicePublication.PROP_KEY_ENDPOINT_CONTAINERID_NAMESPACE))) + results.add(allContainers[i]); + } + return (IContainer[]) results.toArray(new IContainer[] {}); + } + + protected IRemoteServiceContainer[] getRemoteServiceContainers( + IServiceID serviceID, + IServiceEndpointDescription endpointDescription) { + IContainer[] containers = getContainers(serviceID, endpointDescription); + if (containers == null) + return null; + + return getRemoteServiceContainers(containers); + } + + public IRemoteServiceContainer[] findProxyContainers(IServiceID serviceID, + IServiceEndpointDescription endpointDescription, + IProgressMonitor monitor) { + trace("findProxyContainers", "serviceID=" + serviceID + + " endpointDescription=" + endpointDescription); + // Get remote service containers under consideration + IRemoteServiceContainer[] rsContainers = getRemoteServiceContainers( + serviceID, endpointDescription); + // If none available then return + if (rsContainers == null) { + logWarning("findProxyContainers", + "No remote service containers found"); + return EMPTY_REMOTE_SERVICE_CONTAINER_ARRAY; + } + trace("findProxyContainers", "getRemoteServiceContainers.length=" + + rsContainers.length); + + ID connectTargetID = endpointDescription.getConnectTargetID(); + IRemoteServiceContainer[] connectedContainers = (connectTargetID == null) ? rsContainers + : connectRemoteServiceContainers(rsContainers, connectTargetID, + monitor); + if (connectedContainers == null) { + logWarning("findProxyContainers", + "No remote service containers found after connect"); + return EMPTY_REMOTE_SERVICE_CONTAINER_ARRAY; + } + trace("findProxyContainers", "connectRemoteServiceContainers.length=" + + rsContainers.length); + return connectedContainers; + } + + protected IConnectContext getConnectContext( + IRemoteServiceContainer rsContainer, ID connectTargetID) { + return null; + } + + protected IRemoteServiceContainer[] connectRemoteServiceContainers( + IRemoteServiceContainer[] rsContainers, ID connectTargetID, + IProgressMonitor monitor) { + List results = new ArrayList(); + for (int i = 0; i < rsContainers.length; i++) { + IContainer c = rsContainers[i].getContainer(); + try { + if (c.getConnectedID() == null) { + connectContainer(c, connectTargetID, getConnectContext( + rsContainers[i], connectTargetID)); + results.add(rsContainers[i]); + } + } catch (ContainerConnectException e) { + logError("connectRemoteServiceContainers", + "Exception connecting container=" + c.getID() + + " to connectTargetID=" + connectTargetID, e); + } + } + return (IRemoteServiceContainer[]) results + .toArray(new IRemoteServiceContainer[] {}); + } + +} diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/IHostContainerFinder.java b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/IHostContainerFinder.java index c3c6070d6..c03442f33 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/IHostContainerFinder.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.distribution/src/org/eclipse/ecf/osgi/services/distribution/IHostContainerFinder.java @@ -42,4 +42,5 @@ public interface IHostContainerFinder { public IRemoteServiceContainer[] findHostContainers( ServiceReference serviceReference, String[] remoteInterfaces, String[] remoteConfigurationType, String[] remoteRequiresIntents); + } |