From 60f5bce98ed1ce21b111c0d006d2dfd02106371b Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 20 Jan 2010 06:20:53 +0000 Subject: Update to the latest version of OSGi Remote Service Admin Fix the issues to pass OSGi SCA configuration type TCK git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@901079 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/EndpointIntrospector.java | 43 +++++++++--------- .../impl/ExportReferenceImpl.java | 23 +++++++++- .../impl/ExportRegistrationImpl.java | 27 ++++------- .../impl/ImportReferenceImpl.java | 26 +++++++++-- .../impl/ImportRegistrationImpl.java | 37 +++++---------- .../impl/OSGiServiceExporter.java | 30 ++++++++++--- .../impl/OSGiServiceImporter.java | 31 ++++++++++--- .../impl/RemoteServiceAdminImpl.java | 6 +-- .../discovery/impl/AbstractDiscoveryService.java | 8 ++-- .../discovery/impl/DomainDiscoveryService.java | 52 ++++++---------------- .../discovery/impl/LocalDiscoveryService.java | 4 +- 11 files changed, 159 insertions(+), 128 deletions(-) (limited to 'sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca') diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java index 0080e69540..c22506d560 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java @@ -21,9 +21,6 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import static org.apache.tuscany.sca.assembly.Base.SCA11_TUSCANY_NS; import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SCA_BINDINGS; -import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTENTS; -import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTENTS_EXTRA; -import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SERVICE_EXPORTED_INTERFACES; import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper.getStringArray; import static org.osgi.framework.Constants.OBJECTCLASS; import static org.osgi.framework.Constants.SERVICE_ID; @@ -77,9 +74,9 @@ import org.apache.tuscany.sca.policy.PolicyFactory; import org.oasisopen.sca.ServiceRuntimeException; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; +import org.osgi.service.remoteserviceadmin.RemoteConstants; import org.osgi.util.tracker.ServiceTracker; /** @@ -128,7 +125,6 @@ public class EndpointIntrospector { super(); // this.context = context; this.discoveryTracker = discoveryTracker; - // this.registry = registry; this.factories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.modelResolvers = registry.getExtensionPoint(ModelResolverExtensionPoint.class); // this.compositeProcessor = @@ -192,10 +188,10 @@ public class EndpointIntrospector { } } for (Map.Entry p : props.entrySet()) { - if (Constants.OBJECTCLASS.equalsIgnoreCase(p.getKey())) { - throw new IllegalArgumentException(Constants.OBJECTCLASS + " property cannot be overridden."); - } else if (Constants.SERVICE_ID.equalsIgnoreCase(p.getKey())) { - throw new IllegalArgumentException(Constants.SERVICE_ID + " property cannot be overridden."); + if (OBJECTCLASS.equalsIgnoreCase(p.getKey())) { + throw new IllegalArgumentException(OBJECTCLASS + " property cannot be overridden."); + } else if (SERVICE_ID.equalsIgnoreCase(p.getKey())) { + throw new IllegalArgumentException(SERVICE_ID + " property cannot be overridden."); } String key = nameMap.get(p.getKey().toLowerCase()); if (key != null) { @@ -238,11 +234,12 @@ public class EndpointIntrospector { public Contribution introspect(ServiceReference reference, Map props) throws Exception { Bundle bundle = reference.getBundle(); Map properties = getProperties(reference, props); + Collection osgiProps = implementationFactory.createOSGiProperties(reference); Long sid = (Long)reference.getProperty(SERVICE_ID); - String[] requiredIntents = getStringArray(properties.get(SERVICE_EXPORTED_INTENTS)); + String[] requiredIntents = getStringArray(properties.get(RemoteConstants.SERVICE_EXPORTED_INTENTS)); List intents = getIntents(requiredIntents); - String[] requiredIntentsExtra = getStringArray(properties.get(SERVICE_EXPORTED_INTENTS_EXTRA)); + String[] requiredIntentsExtra = getStringArray(properties.get(RemoteConstants.SERVICE_EXPORTED_INTENTS_EXTRA)); List extraIntents = getIntents(requiredIntentsExtra); Set allIntents = new HashSet(intents); allIntents.addAll(extraIntents); @@ -250,7 +247,7 @@ public class EndpointIntrospector { String[] bindingNames = getStringArray(properties.get(SCA_BINDINGS)); Collection bindings = loadBindings(bindingNames); - String[] remoteInterfaces = getStringArray(reference.getProperty(SERVICE_EXPORTED_INTERFACES)); + String[] remoteInterfaces = getStringArray(reference.getProperty(RemoteConstants.SERVICE_EXPORTED_INTERFACES)); if (remoteInterfaces == null || remoteInterfaces.length > 0 && "*".equals(remoteInterfaces[0])) { remoteInterfaces = getStringArray(reference.getProperty(OBJECTCLASS)); } else { @@ -263,7 +260,7 @@ public class EndpointIntrospector { } } - Contribution contribution = generateContribution(bundle, sid, remoteInterfaces, bindings, allIntents); + Contribution contribution = generateContribution(bundle, sid, remoteInterfaces, bindings, allIntents, osgiProps); return contribution; } @@ -282,7 +279,8 @@ public class EndpointIntrospector { Long sid, String[] remoteInterfaces, Collection bindings, - Set allIntents) throws ClassNotFoundException, + Set allIntents, + Collection osgiProps) throws ClassNotFoundException, InvalidInterfaceException { String id = "osgi.service." + UUID.randomUUID(); Composite composite = assemblyFactory.createComposite(); @@ -311,12 +309,13 @@ public class EndpointIntrospector { service.setName(name); service.setInterfaceContract(interfaceContract); - service.getExtensions().add(serviceID); - implementation.getServices().add(service); ComponentService componentService = assemblyFactory.createComponentService(); componentService.setName(service.getName()); + componentService.getExtensions().add(serviceID); + componentService.getExtensions().addAll(osgiProps); + component.getServices().add(componentService); componentService.setService(service); } @@ -355,28 +354,31 @@ public class EndpointIntrospector { Collection interfaces = Collections.emptyList(); Collection intents = Collections.emptyList(); Endpoint ep = (Endpoint)endpoint.getProperties().get(Endpoint.class.getName()); + Collection osgiProps = implementationFactory.createOSGiProperties(endpoint.getProperties()); if (ep != null) { bindings = Collections.singletonList(ep.getBinding()); interfaces = Collections.singletonList(((JavaInterface)ep.getComponentServiceInterfaceContract().getInterface()).getName()); - intents = ep.getRequiredIntents(); + // FIXME: [rfeng] We need to build the in-memory composite so that intents are calculated at the ep level + intents = ep.getService().getRequiredIntents(); } else { Map properties = endpoint.getProperties(); interfaces = endpoint.getInterfaces(); - String[] requiredIntents = getStringArray(properties.get(SERVICE_EXPORTED_INTENTS)); + String[] requiredIntents = getStringArray(properties.get(RemoteConstants.SERVICE_INTENTS)); intents = getIntents(requiredIntents); String[] bindingNames = getStringArray(properties.get(SCA_BINDINGS)); bindings = loadBindings(bindingNames); } - Contribution contribution = generateContribution(bundle, interfaces, bindings, intents); + Contribution contribution = generateContribution(bundle, interfaces, bindings, intents, osgiProps); return contribution; } private Contribution generateContribution(Bundle bundle, Collection remoteInterfaces, Collection bindings, - Collection intents) throws ClassNotFoundException, + Collection intents, + Collection osgiProps) throws ClassNotFoundException, InvalidInterfaceException, ContributionResolveException { String id = "osgi.reference." + UUID.randomUUID(); Composite composite = assemblyFactory.createComposite(); @@ -406,6 +408,7 @@ public class EndpointIntrospector { ComponentReference componentReference = assemblyFactory.createComponentReference(); componentReference.setName(reference.getName()); + componentReference.getExtensions().addAll(osgiProps); component.getReferences().add(componentReference); componentReference.setReference(reference); componentReference.setWiredByImpl(true); diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportReferenceImpl.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportReferenceImpl.java index 473e5742e9..13c7dd3cf3 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportReferenceImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportReferenceImpl.java @@ -19,23 +19,28 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; +import org.apache.tuscany.sca.node.Node; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.ExportReference; +import org.osgi.service.remoteserviceadmin.ExportRegistration; /** * */ public class ExportReferenceImpl implements ExportReference { + private Node node; private final ServiceReference exportedService; private final EndpointDescription endpointDescription; + private int count; /** * @param exportedService * @param endpointDescription */ - public ExportReferenceImpl(ServiceReference exportedService, EndpointDescription endpointDescription) { + public ExportReferenceImpl(Node node, ServiceReference exportedService, EndpointDescription endpointDescription) { super(); + this.node = node; this.exportedService = exportedService; this.endpointDescription = endpointDescription; } @@ -47,6 +52,22 @@ public class ExportReferenceImpl implements ExportReference { public EndpointDescription getExportedEndpoint() { return endpointDescription; } + + public synchronized ExportRegistration register() { + count++; + return new ExportRegistrationImpl(this); + } + public synchronized void unregister() { + if (count > 0) { + count--; + } + if (count == 0) { + if (node != null) { + node.stop(); + node = null; + } + } + } } diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportRegistrationImpl.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportRegistrationImpl.java index 2335d31020..39cbd7b9e6 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportRegistrationImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ExportRegistrationImpl.java @@ -19,7 +19,6 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; -import org.apache.tuscany.sca.node.Node; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.ExportReference; @@ -29,8 +28,7 @@ import org.osgi.service.remoteserviceadmin.ExportRegistration; * Implementation of {@link ExportRegistration} */ public class ExportRegistrationImpl implements ExportRegistration { - private Node node; - private ExportReference exportReference; + private ExportReferenceImpl exportReference; private Throwable exception; /** @@ -38,13 +36,9 @@ public class ExportRegistrationImpl implements ExportRegistration { * @param endpointDescription * @param exception */ - public ExportRegistrationImpl(Node node, - ServiceReference exportedService, - EndpointDescription endpointDescription, - Throwable exception) { + public ExportRegistrationImpl(ExportReferenceImpl exportReference, Throwable exception) { super(); - this.node = node; - this.exportReference = new ExportReferenceImpl(exportedService, endpointDescription); + this.exportReference = exportReference; this.exception = exception; } @@ -52,20 +46,19 @@ public class ExportRegistrationImpl implements ExportRegistration { * @param exportedService * @param endpointDescription */ - public ExportRegistrationImpl(Node node, ServiceReference exportedService, EndpointDescription endpointDescription) { - this(node, exportedService, endpointDescription, null); + public ExportRegistrationImpl(ExportReferenceImpl exportReference) { + this(exportReference, null); } /** * @see org.osgi.remoteserviceadmin.ExportRegistration#close() */ public void close() { - if (node != null) { - node.stop(); - node = null; + if (exportReference != null) { + exportReference.unregister(); } exception = null; - exportReference = new ExportReferenceImpl(null, null); + exportReference = null; } public ServiceReference getExportedService() { @@ -80,10 +73,6 @@ public class ExportRegistrationImpl implements ExportRegistration { return exception; } - public Node getNode() { - return node; - } - public ExportReference getExportReference() throws IllegalStateException { return exportReference; } diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportReferenceImpl.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportReferenceImpl.java index 8ecc696795..9c5b1818c8 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportReferenceImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportReferenceImpl.java @@ -19,24 +19,28 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; +import org.apache.tuscany.sca.node.Node; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.ImportReference; +import org.osgi.service.remoteserviceadmin.ImportRegistration; /** - * + * Implementation of ImportReference. It keeps a reference count of ImportRegistrations */ public class ImportReferenceImpl implements ImportReference { - + private Node node; private final ServiceReference importedService; private final EndpointDescription endpointDescription; + private int count = 0; /** * @param exportedService * @param endpointDescription */ - public ImportReferenceImpl(ServiceReference importedService, EndpointDescription endpointDescription) { + public ImportReferenceImpl(Node node, ServiceReference importedService, EndpointDescription endpointDescription) { super(); + this.node = node; this.importedService = importedService; this.endpointDescription = endpointDescription; } @@ -48,5 +52,21 @@ public class ImportReferenceImpl implements ImportReference { public EndpointDescription getImportedEndpoint() { return endpointDescription; } + + public synchronized ImportRegistration register() { + count++; + return new ImportRegistrationImpl(this); + } + public synchronized void unregister() { + if (count > 0) { + count--; + } + if (count == 0) { + if (node != null) { + node.stop(); + node = null; + } + } + } } diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportRegistrationImpl.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportRegistrationImpl.java index 8961d031cc..9506128286 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportRegistrationImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/ImportRegistrationImpl.java @@ -19,9 +19,6 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; -import org.apache.tuscany.sca.node.Node; -import org.osgi.framework.ServiceReference; -import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.ImportReference; import org.osgi.service.remoteserviceadmin.ImportRegistration; @@ -29,55 +26,43 @@ import org.osgi.service.remoteserviceadmin.ImportRegistration; * */ public class ImportRegistrationImpl implements ImportRegistration { - private Node node; - private ImportReference importReference; + private ImportReferenceImpl importReference; private Throwable exception; /** - * @param exportedService - * @param endpointDescription - * @param exception + * @param importReference */ - public ImportRegistrationImpl(Node node, - ServiceReference importedService, - EndpointDescription endpointDescription, - Throwable exception) { + public ImportRegistrationImpl(ImportReferenceImpl importReference) { super(); - this.node = node; - this.importReference = new ImportReferenceImpl(importedService, endpointDescription); - this.exception = exception; + this.importReference = importReference; } /** * @param exportedService * @param endpointDescription + * @param exception */ - public ImportRegistrationImpl(Node node, ServiceReference importedService, EndpointDescription endpointDescription) { + public ImportRegistrationImpl(ImportReferenceImpl importReference, Throwable exception) { super(); - this.node = node; - this.importReference = new ImportReferenceImpl(importedService, endpointDescription); + this.importReference = importReference; + this.exception = exception; } /** * @see org.osgi.remoteserviceadmin.ImportRegistration#close() */ public void close() { - if (node != null) { - node.stop(); - node = null; + if (importReference != null) { + importReference.unregister(); } exception = null; - importReference = new ImportReferenceImpl(null, null); + importReference = null; } public Throwable getException() { return exception; } - public Node getNode() { - return node; - } - public ImportReference getImportReference() { return importReference; } diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java index b644ca14c9..4466c04563 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java @@ -31,6 +31,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentService; @@ -48,6 +51,10 @@ import org.osgi.util.tracker.ServiceTrackerCustomizer; * Watching and exporting OSGi services */ public class OSGiServiceExporter extends AbstractOSGiServiceHandler implements ServiceTrackerCustomizer { + private final static Logger logger = Logger.getLogger(OSGiServiceExporter.class.getName()); + + private Map exportReferences = + new ConcurrentHashMap(); /** * @param context @@ -62,6 +69,11 @@ public class OSGiServiceExporter extends AbstractOSGiServiceHandler implements S init(); } + public void stop() { + exportReferences.clear(); + super.stop(); + } + public Object addingService(ServiceReference reference) { return exportService(reference, null); } @@ -97,17 +109,23 @@ public class OSGiServiceExporter extends AbstractOSGiServiceHandler implements S List exportedServices = new ArrayList(); for (Endpoint endpoint : service.getEndpoints()) { EndpointDescription endpointDescription = createEndpointDescription(context, endpoint); - ExportRegistration exportRegistration = - new ExportRegistrationImpl(node, reference, endpointDescription); - exportedServices.add(exportRegistration); + synchronized (this) { + ExportReferenceImpl exportReference = exportReferences.get(endpointDescription); + if (exportReference == null) { + exportReference = new ExportReferenceImpl(node, reference, endpointDescription); + } + ExportRegistration exportRegistration = exportReference.register(); + exportedServices.add(exportRegistration); + } } return exportedServices; } else { return null; } } catch (Exception e) { - e.printStackTrace(); - return null; + logger.log(Level.SEVERE, e.getMessage(), e); + ExportRegistration exportRegistration = new ExportRegistrationImpl(null, e); + return Collections.singletonList(exportRegistration); } } @@ -118,7 +136,7 @@ public class OSGiServiceExporter extends AbstractOSGiServiceHandler implements S public void removedService(ServiceReference reference, Object service) { List exportedServices = (List)service; - for(ExportRegistration exportRegistration: exportedServices) { + for (ExportRegistration exportRegistration : exportedServices) { exportRegistration.close(); } } diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java index d33fd5a5e6..f24e9af7c1 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java @@ -20,11 +20,14 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; import org.apache.tuscany.sca.node.impl.NodeImpl; import org.osgi.framework.Bundle; @@ -37,6 +40,9 @@ import org.osgi.service.remoteserviceadmin.ImportRegistration; * Watching and exporting OSGi services */ public class OSGiServiceImporter extends AbstractOSGiServiceHandler { + private final static Logger logger = Logger.getLogger(OSGiServiceImporter.class.getName()); + private Map importReferences = + new ConcurrentHashMap(); /** * @param context @@ -51,6 +57,11 @@ public class OSGiServiceImporter extends AbstractOSGiServiceHandler { // Defer init() to importService() } + public void stop() { + importReferences.clear(); + super.stop(); + } + public ImportRegistration importService(Bundle bundle, EndpointDescription endpointDescription) { init(); try { @@ -77,19 +88,27 @@ public class OSGiServiceImporter extends AbstractOSGiServiceHandler { + "#reference(" + componentReference.getName() + ")"); - return new ImportRegistrationImpl(node, serviceReference, endpointDescription); + synchronized (this) { + ImportReferenceImpl importReference = importReferences.get(endpointDescription); + if (importReference == null) { + importReference = new ImportReferenceImpl(node, serviceReference, endpointDescription); + importReferences.put(endpointDescription, importReference); + } + return importReference.register(); + } } else { return null; } } catch (Exception e) { - e.printStackTrace(); - return null; + logger.log(Level.SEVERE, e.getMessage(), e); + return new ImportRegistrationImpl(null, e); } } public void unimportService(ImportRegistration importRegistration) { - Node node = (Node)importRegistration.getImportReference().getImportedService().getProperty("sca.node"); - node.stop(); + if (importRegistration != null) { + importRegistration.close(); + } } } diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java index f4521cfbfc..b1ebeb9df0 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/RemoteServiceAdminImpl.java @@ -253,9 +253,9 @@ public class RemoteServiceAdminImpl implements RemoteServiceAdmin, ManagedServic } else { ep = rsaEvent.getExportReference().getExportedEndpoint(); } - props.put("endpoint.service.id", ep.getRemoteServiceID()); - props.put("endpoint.framework.uuid", ep.getRemoteFrameworkUUID()); - props.put("endpoint.id", ep.getRemoteID()); + props.put("endpoint.service.id", ep.getServiceId()); + props.put("endpoint.framework.uuid", ep.getFrameworkUUID()); + props.put("endpoint.id", ep.getId()); props.put("objectClass", ep.getInterfaces()); props.put("service.imported.configs", ep.getConfigurationTypes()); props.put("timestamp", new Long(System.currentTimeMillis())); diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java index 4bbf130c74..886e79197d 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java @@ -64,7 +64,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi private Map> filtersToListeners = new HashMap>(); // this is effectively a set which allows for multiple service descriptions with the // same interface name but different properties and takes care of itself with respect to concurrency - protected Map servicesInfo = new ConcurrentHashMap(); + protected Map endpointDescriptions = new ConcurrentHashMap(); private Map> listenersToFilters = new HashMap>(); private ServiceTracker trackerTracker; @@ -176,7 +176,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi } if (logger.isLoggable(Level.FINE)) { - if (servicesInfo.size() > 0) { + if (endpointDescriptions.size() > 0) { logger.fine("search for matches to trigger callbacks with delta: " + deltaInterest); } else { logger.fine("nothing to search for matches to trigger callbacks with delta: " + deltaInterest); @@ -185,7 +185,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi Iterator i = deltaInterest.iterator(); while (i.hasNext()) { String next = i.next(); - for (EndpointDescription sd : servicesInfo.keySet()) { + for (EndpointDescription sd : endpointDescriptions.keySet()) { triggerCallbacks(listener, next, sd, ADDED); } } @@ -267,7 +267,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi return collection; } - protected void endpointChanged(EndpointDescription sd, int type) { + protected synchronized void endpointChanged(EndpointDescription sd, int type) { for (Map.Entry> entry : listenersToFilters.entrySet()) { for (String filter : entry.getValue()) { if (filterMatches(filter, sd)) { diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java index a7c6d04ee9..a02be672a2 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java @@ -65,7 +65,7 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements thread.start(); } - private void startEndpointRegistry() { + private synchronized void startEndpointRegistry() { // The following code forced the start() of the domain registry in absense of services String domainRegistry = context.getProperty("org.osgi.sca.domain.registry"); if (domainRegistry == null) { @@ -80,7 +80,7 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements } } - public void endpointAdded(Endpoint endpoint) { + public synchronized void endpointAdded(Endpoint endpoint) { Implementation impl = endpoint.getComponent().getImplementation(); if (!(impl instanceof OSGiImplementation)) { return; @@ -94,59 +94,35 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements bundleContext = bundle != null ? bundle.getBundleContext() : null; } - /* - if (!endpoint.isRemote()) { - Interface intf = endpoint.getService().getInterfaceContract().getInterface(); - JavaInterface javaInterface = (JavaInterface)intf; - // String filter = getOSGiFilter(provider.getOSGiProperties(service)); - // FIXME: What is the filter? - String filter = "(!(sca.reference=*))"; - // "(sca.service=" + component.getURI() + "#service-name\\(" + service.getName() + "\\))"; - ServiceReference ref = null; - try { - ref = bundleContext.getServiceReferences(javaInterface.getName(), filter)[0]; - } catch (InvalidSyntaxException e) { - // Ignore - } - if (ref != null) { - - } - } else - */ - { // Notify the endpoint listeners EndpointDescription description = createEndpointDescription(bundleContext, endpoint); // Set the owning bundle to runtime bundle to avoid NPE - servicesInfo.put(description, context.getBundle()); + endpointDescriptions.put(description, context.getBundle()); endpointChanged(description, ADDED); - } } - public void endpointRemoved(Endpoint endpoint) { - /* - if (!endpoint.isRemote()) { - // export services - } else - */ - { + public synchronized void endpointRemoved(Endpoint endpoint) { EndpointDescription description = createEndpointDescription(context, endpoint); - servicesInfo.remove(description); + endpointDescriptions.remove(description); endpointChanged(description, REMOVED); - } } - public void endpointUpdated(Endpoint oldEndpoint, Endpoint newEndpoint) { + public synchronized void endpointUpdated(Endpoint oldEndpoint, Endpoint newEndpoint) { // FIXME: This is a quick and dirty way for the update endpointRemoved(oldEndpoint); endpointAdded(newEndpoint); } public void stop() { - domainRegistryFactory.removeListener(this); - if (endpointRegistry instanceof LifeCycleListener) { - ((LifeCycleListener)endpointRegistry).stop(); + if (domainRegistryFactory != null) { + domainRegistryFactory.removeListener(this); + if (endpointRegistry instanceof LifeCycleListener) { + ((LifeCycleListener)endpointRegistry).stop(); + } + domainRegistryFactory = null; + endpointRegistry = null; + super.stop(); } - super.stop(); } @Override diff --git a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java index a4c9414b92..56a830a1e6 100644 --- a/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java +++ b/sca-java-2.x/trunk/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java @@ -105,7 +105,7 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B } private void removeServicesDeclaredInBundle(Bundle bundle) { - for (Iterator> i = servicesInfo.entrySet().iterator(); i.hasNext();) { + for (Iterator> i = endpointDescriptions.entrySet().iterator(); i.hasNext();) { Entry entry = i.next(); if (entry.getValue().equals(bundle)) { serviceDescriptionRemoved(entry.getKey()); @@ -163,7 +163,7 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B for (ServiceDescriptions sds : extender.getRemoteServiceDescriptions()) { for (ServiceDescription sd : sds) { EndpointDescription sed = createEndpointDescription(sd); - servicesInfo.put(sed, bundle); + endpointDescriptions.put(sed, bundle); serviceDescriptionAdded(sed); } } -- cgit v1.2.3