From 31dc745c2cf7730c6c747d44d2c754646b3c25ed Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 25 Jun 2009 00:17:16 +0000 Subject: Add more logic to handle the Endpoint publication and discovery git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@788223 13f79535-47bb-0310-9956-ffa450edef68 --- .../dosgi/discovery/AbstractDiscoveryService.java | 11 ++++-- .../dosgi/discovery/DomainDiscoveryService.java | 44 +++++++++++++++++----- .../sca/dosgi/discovery/LocalDiscoveryService.java | 12 ++++++ 3 files changed, 54 insertions(+), 13 deletions(-) (limited to 'java/sca/modules/implementation-osgi-runtime') diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java index 48a2777579..22c0e5bc7d 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java @@ -41,6 +41,7 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.discovery.DiscoveredServiceTracker; import org.osgi.service.discovery.Discovery; import org.osgi.service.discovery.ServiceEndpointDescription; @@ -103,7 +104,7 @@ public abstract class AbstractDiscoveryService implements Discovery { ServiceTracker tracker = new ServiceTracker(context, ExtensionPointRegistry.class.getName(), null); tracker.open(); // tracker.waitForService(1000); - ExtensionPointRegistry extensionPointRegistry = (ExtensionPointRegistry) tracker.getService(); + ExtensionPointRegistry extensionPointRegistry = (ExtensionPointRegistry)tracker.getService(); tracker.close(); return extensionPointRegistry; } @@ -315,9 +316,13 @@ public abstract class AbstractDiscoveryService implements Discovery { } } - protected void localServicePublished(ServiceReference ref, Endpoint endpoint) { + protected ServiceRegistration localServicePublished(ServiceReference ref, Endpoint endpoint) { EndpointPublication publication = new EndpointPublication(ref, endpoint); - ref.getBundle().getBundleContext().registerService(ServicePublication.class.getName(), publication, publication.getProperties()); + ServiceRegistration registration = + ref.getBundle().getBundleContext().registerService(ServicePublication.class.getName(), + publication, + publication.getProperties()); + return registration; } } diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java index 90305a9ae1..acd112395e 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java @@ -22,6 +22,11 @@ package org.apache.tuscany.sca.dosgi.discovery; import static org.osgi.service.discovery.DiscoveredServiceNotification.AVAILABLE; import static org.osgi.service.discovery.DiscoveredServiceNotification.UNAVAILABLE; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.core.ExtensionPointRegistry; @@ -34,7 +39,9 @@ import org.apache.tuscany.sca.runtime.EndpointRegistry; import org.osgi.framework.BundleContext; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.discovery.ServiceEndpointDescription; +import org.osgi.service.discovery.ServicePublication; /** * Discovery service based on the distributed SCA domain @@ -42,6 +49,9 @@ import org.osgi.service.discovery.ServiceEndpointDescription; public class DomainDiscoveryService extends AbstractDiscoveryService implements EndpointListener { private EndpointRegistry endpointRegistry; + private Map endpointRegistrations = + new ConcurrentHashMap(); + public DomainDiscoveryService(BundleContext context) { super(context); @@ -60,8 +70,7 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements OSGiImplementation osgiImpl = (OSGiImplementation)impl; BundleContext bundleContext = osgiImpl.getBundle().getBundleContext(); - boolean local = true; - if (local) { + if (!endpoint.isRemote()) { Interface intf = endpoint.getService().getInterfaceContract().getInterface(); JavaInterface javaInterface = (JavaInterface)intf; @@ -76,7 +85,8 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements // Ignore } if (ref != null) { - localServicePublished(ref, endpoint); + ServiceRegistration registration = localServicePublished(ref, endpoint); + endpointRegistrations.put(endpoint.getURI(), registration); } } else { // Remote endpoints @@ -85,14 +95,13 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements } } - private boolean isLocal(Endpoint endpoint) { - // FIXME: To be implemented - return true; - } - public void endpointRemoved(Endpoint endpoint) { - if (isLocal(endpoint)) { + if (!endpoint.isRemote()) { // unregister the ServicePublication here + ServiceRegistration registration = endpointRegistrations.get(endpoint.getURI()); + if (registration != null) { + registration.unregister(); + } } else { // Remote endpoints ServiceEndpointDescription description = getServiceEndpointDescription(endpoint); @@ -101,6 +110,9 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements } public void endpointUpdated(Endpoint oldEndpoint, Endpoint newEndpoint) { + // FIXME: This is a quick and dirty way for the update + endpointRemoved(oldEndpoint); + endpointAdded(newEndpoint); } public void stop() { @@ -108,7 +120,19 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements super.stop(); } + public Map getServiceProperties(Endpoint endpoint) { + Map serviceProps = new HashMap(); + serviceProps.put(ServicePublication.ENDPOINT_LOCATION, endpoint.getURI()); + // TODO: Populate the properties from the Endpoint object + return serviceProps; + } + private ServiceEndpointDescription getServiceEndpointDescription(Endpoint endpoint) { - return null; + Interface interface1 = endpoint.getService().getInterfaceContract().getInterface(); + JavaInterface javaInterface = (JavaInterface)interface1; + ServiceEndpointDescription description = + new ServiceEndpointDescriptionImpl(Collections.singleton(javaInterface.getName()), + getServiceProperties(endpoint)); + return description; } } diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/LocalDiscoveryService.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/LocalDiscoveryService.java index d985fe708c..2a04a80426 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/LocalDiscoveryService.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/LocalDiscoveryService.java @@ -25,12 +25,14 @@ import static org.osgi.service.discovery.DiscoveredServiceNotification.AVAILABLE import static org.osgi.service.discovery.DiscoveredServiceNotification.UNAVAILABLE; import java.util.Collections; +import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; @@ -72,6 +74,16 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B } private void discover(Bundle b) { + String path = (String)b.getHeaders().get(ServiceDescriptions.REMOTE_SERVICE_HEADER); + if (path == null) { + Enumeration files = b.findEntries(ServiceDescriptions.REMOTE_SERVICE_FOLDER, "*.xml", false); + if (files == null || !files.hasMoreElements()) { + return; + } + } + + ServiceDescriptions descriptions = null; + // TODO: Use SCA contribution to load the service discription files List refs = Collections.emptyList(); for (ServiceEndpointDescription sed : refs) { -- cgit v1.2.3