diff options
Diffstat (limited to 'java/sca/modules/node-impl-osgi')
9 files changed, 243 insertions, 225 deletions
diff --git a/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF b/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF index cc9eb14f06..70ba184107 100644 --- a/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF +++ b/java/sca/modules/node-impl-osgi/META-INF/MANIFEST.MF @@ -24,6 +24,7 @@ Import-Package: javax.xml.namespace, org.apache.tuscany.sca.definitions;version="2.0.0",
org.apache.tuscany.sca.definitions.util;version="2.0.0",
org.apache.tuscany.sca.definitions.xml;version="2.0.0",
+ org.apache.tuscany.sca.deployment;version="2.0.0",
org.apache.tuscany.sca.extensibility.equinox;version="2.0.0",
org.apache.tuscany.sca.implementation.osgi;version="2.0.0",
org.apache.tuscany.sca.interfacedef;version="2.0.0",
diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java index f03f1e266a..8079ca430a 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeActivator.java @@ -21,8 +21,8 @@ package org.apache.tuscany.sca.node.osgi.impl; import static org.apache.tuscany.sca.node.osgi.impl.NodeManager.isSCABundle; -import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.TopologyManagerImpl; import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.RemoteServiceAdminImpl; +import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.TopologyManagerImpl; import org.apache.tuscany.sca.osgi.service.discovery.impl.DiscoveryActivator; import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; @@ -63,14 +63,11 @@ public class NodeActivator implements BundleActivator, SynchronousBundleListener remoteAdmin = new RemoteServiceAdminImpl(context); remoteAdmin.start(); + discoveryActivator.start(context); + controller = new TopologyManagerImpl(context); controller.start(); -// exporter = new OSGiServiceExporter(context); -// exporter.start(); - - discoveryActivator.start(context); - boolean found = false; for (Bundle b : context.getBundles()) { if (isSCABundle(b)) { diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java index 6adb1701c8..c88632407c 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/EndpointIntrospector.java @@ -28,10 +28,6 @@ import static org.osgi.framework.Constants.OBJECTCLASS; import static org.osgi.framework.Constants.SERVICE_ID; import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -44,10 +40,7 @@ import java.util.Set; import java.util.UUID; import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.Base; @@ -62,50 +55,48 @@ import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; -import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.ProcessorContext; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.implementation.osgi.BindingDescriptions; +import org.apache.tuscany.sca.deployment.Deployer; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementation; import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; -import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; +import org.apache.tuscany.sca.implementation.osgi.SCAConfig; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; -import org.apache.tuscany.sca.monitor.MonitorFactory; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; +import org.apache.tuscany.sca.osgi.service.discovery.impl.LocalDiscoveryService; +import org.apache.tuscany.sca.osgi.service.discovery.impl.LocalDiscoveryService.ExtenderConfiguration; import org.apache.tuscany.sca.policy.Intent; 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.util.tracker.ServiceTracker; /** * Introspect an OSGi Service to create an SCA composite that contains a single component with * implementation.osgi */ public class EndpointIntrospector { + private BundleContext context; private AssemblyFactory assemblyFactory; private ContributionFactory contributionFactory; private OSGiImplementationFactory implementationFactory; - private ServiceDescriptionsFactory serviceDescriptionsFactory; private PolicyFactory policyFactory; private ExtensionPointRegistry registry; private FactoryExtensionPoint factories; private ModelResolverExtensionPoint modelResolvers; - private XMLInputFactory xmlInputFactory; - private XMLOutputFactory xmlOutputFactory; private JavaInterfaceFactory javaInterfaceFactory; - private StAXArtifactProcessor processor; + private Deployer deployer; + private ServiceTracker discoveryTracker; /** * @param intentName @@ -131,8 +122,10 @@ public class EndpointIntrospector { * @param context TODO * @param registry */ - public EndpointIntrospector(BundleContext context, ExtensionPointRegistry registry) { + public EndpointIntrospector(BundleContext context, ExtensionPointRegistry registry, ServiceTracker discoveryTracker) { super(); + this.context = context; + this.discoveryTracker = discoveryTracker; this.registry = registry; this.factories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.modelResolvers = registry.getExtensionPoint(ModelResolverExtensionPoint.class); @@ -140,15 +133,8 @@ public class EndpointIntrospector { this.contributionFactory = factories.getFactory(ContributionFactory.class); this.policyFactory = factories.getFactory(PolicyFactory.class); this.implementationFactory = factories.getFactory(OSGiImplementationFactory.class); - this.serviceDescriptionsFactory = factories.getFactory(ServiceDescriptionsFactory.class); - this.xmlInputFactory = factories.getFactory(XMLInputFactory.class); - this.xmlOutputFactory = factories.getFactory(XMLOutputFactory.class); this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); - StAXArtifactProcessorExtensionPoint processors = - registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - UtilityExtensionPoint utilities = this.registry.getExtensionPoint(UtilityExtensionPoint.class); - MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); - processor = new ExtensibleStAXArtifactProcessor(processors, xmlInputFactory, xmlOutputFactory); + this.deployer = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Deployer.class); } private Intent getIntent(String intent) { @@ -278,8 +264,8 @@ public class EndpointIntrospector { String[] objectClasses = getStrings(reference.getProperty(OBJECTCLASS)); Set<String> objectClassSet = new HashSet<String>(Arrays.asList(objectClasses)); if (!objectClassSet.containsAll(Arrays.asList(remoteInterfaces))) { - throw new IllegalArgumentException("The exported interfaces are not a subset of the types" - + " listed in the objectClass service property from the Service Reference"); + throw new IllegalArgumentException( + "The exported interfaces are not a subset of the types" + " listed in the objectClass service property from the Service Reference"); } } for (String intf : remoteInterfaces) { @@ -311,8 +297,8 @@ public class EndpointIntrospector { String[] requiredIntentsExtra = getStrings(properties.get(SERVICE_EXPORTED_INTENTS_EXTRA)); List<Intent> extraIntents = getIntents(requiredIntentsExtra); - String[] bindingDocuments = getStrings(properties.get(SCA_BINDINGS)); - List<Binding> bindings = loadBindings(reference.getBundle(), bindingDocuments); + String[] bindingNames = getStrings(properties.get(SCA_BINDINGS)); + Collection<Binding> bindings = loadBindings(bindingNames); for (ComponentService componentService : component.getServices()) { componentService.getRequiredIntents().addAll(intents); @@ -332,7 +318,7 @@ public class EndpointIntrospector { } public Contribution introspect(Bundle bundle, EndpointDescription endpoint) throws Exception { - Endpoint ep = (Endpoint) endpoint.getProperties().get(Endpoint.class.getName()); + Endpoint ep = (Endpoint)endpoint.getProperties().get(Endpoint.class.getName()); if (ep != null) { return introspect(bundle, ep); } @@ -382,19 +368,12 @@ public class EndpointIntrospector { String[] requiredIntents = getStrings(properties.get(SERVICE_EXPORTED_INTENTS)); List<Intent> intents = getIntents(requiredIntents); - String[] bindingDocuments = getStrings(properties.get(SCA_BINDINGS)); - List<Binding> bindings = loadBindings(bundle, bindingDocuments); - - List<Binding> bindingList = new ArrayList<Binding>(bindings); - BindingDescriptions bindingDescriptions = - (BindingDescriptions)properties.get(BindingDescriptions.BINDINGS_QNAME.toString()); - if (bindingDescriptions != null) { - bindingList.addAll(bindingDescriptions); - } + String[] bindingNames = getStrings(properties.get(SCA_BINDINGS)); + Collection<Binding> bindings = loadBindings(bindingNames); for (ComponentReference componentReference : component.getReferences()) { componentReference.getRequiredIntents().addAll(intents); - componentReference.getBindings().addAll(bindingList); + componentReference.getBindings().addAll(bindings); } // FIXME: Should we scan the owning bundle to create the SCA contribution? Contribution contribution = contributionFactory.createContribution(); @@ -455,46 +434,42 @@ public class EndpointIntrospector { return contribution; } - private List<Binding> loadBindings(Bundle bundle, String[] bindingDocuments) throws IOException, - ContributionReadException { - if (bindingDocuments == null || bindingDocuments.length == 0) { + private Collection<Binding> loadBindings(String[] qnames) throws IOException, + ContributionReadException, XMLStreamException { + if (qnames == null || qnames.length == 0) { return Collections.emptyList(); } - List<Binding> bindings = new ArrayList<Binding>(); - for (String doc : bindingDocuments) { - URL url = locate(bundle, doc); - if (url == null) { - throw new IOException("Entry " + doc + " cannot be found in bundle " + bundle); - } - bindings.addAll(loadBindings(url)); + QName[] bindingNames = new QName[qnames.length]; + int index = 0; + for (String name : qnames) { + bindingNames[index++] = getQName(name); } - return bindings; - } - private List<Binding> loadBindings(URL url) throws ContributionReadException, IOException { - InputStream is = url.openStream(); - try { - XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(is); - reader.nextTag(); - Object model = processor.read(reader, new ProcessorContext(registry)); - if (model instanceof BindingDescriptions) { - return ((BindingDescriptions)model); - } else { - return Collections.emptyList(); + LocalDiscoveryService discoveryService = (LocalDiscoveryService)discoveryTracker.getService(); + + Map<QName, Binding> bindingMap = new HashMap<QName, Binding>(); + if (discoveryService != null) { + for (ExtenderConfiguration config : discoveryService.getConfigurations()) { + for (SCAConfig sc : config.getSCAConfigs()) { + for (QName bindingName : bindingNames) { + if (sc.getTargetNamespace().equals(bindingName.getNamespaceURI())) { + for (Binding binding : sc.getBindings()) { + if (bindingName.getLocalPart().equals(binding.getName())) { + bindingMap.put(bindingName, binding); + break; + } + } + } + } + } } - } catch (XMLStreamException e) { - throw new ContributionReadException(e); - } finally { - is.close(); } - } - - private URL locate(Bundle bundle, String location) throws MalformedURLException { - URI uri = URI.create(location); - if (uri.isAbsolute()) { - return uri.toURL(); + for (QName bindingName : bindingNames) { + if (!bindingMap.containsKey(bindingName)) { + throw new ServiceRuntimeException("Binding cannot be resolved: " + bindingName); + } } - return bundle.getEntry(location); + return bindingMap.values(); } /** diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java index 856cab3412..d5a2b7aa95 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiHelper.java @@ -19,9 +19,14 @@ package org.apache.tuscany.sca.osgi.remoteserviceadmin.impl; +import java.net.URL; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; @@ -83,4 +88,48 @@ public class OSGiHelper { } } + /** + * Get a collection of resources that are configured by the given header + * @param bundle The bundle + * @param header + * @param defaultValue + * @return + */ + public static Collection<URL> getConfiguration(Bundle bundle, String header, String defaultValue) { + String value = (String)bundle.getHeaders().get(header); + if (value == null) { + return Collections.emptyList(); + } + String paths[] = value.trim().split("( |\t|\n|\r|\f|,)+"); + if (paths.length == 0) { + if (defaultValue != null) { + paths = new String[] {defaultValue}; + } else { + paths = new String[0]; + } + } + Collection<URL> files = new HashSet<URL>(); + for (String path : paths) { + if (path.endsWith("/")) { + path = path + "*.xml"; + } + if (!path.startsWith("/")) { + path = "/" + path; + } + int lastIndex = path.lastIndexOf('/'); + String root = path.substring(0, lastIndex); + if ("".equals(root)) { + root = "/"; + } + String pattern = path.substring(lastIndex + 1); + Enumeration<URL> entries = bundle.findEntries(root, pattern, false); + if (entries != null) { + while (entries.hasMoreElements()) { + files.add(entries.nextElement()); + } + } + } + return files; + } + } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java index ab7aff34c0..d6cbe1a44b 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceExporter.java @@ -38,6 +38,7 @@ import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; import org.apache.tuscany.sca.node.impl.NodeImpl; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.osgi.remoteserviceadmin.ExportRegistration; +import org.apache.tuscany.sca.osgi.service.discovery.impl.LocalDiscoveryService; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; @@ -51,6 +52,7 @@ public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleL private BundleContext context; private NodeFactoryImpl nodeFactory; private EndpointIntrospector introspector; + private ServiceTracker discoveryTracker; /** * @param context @@ -65,7 +67,9 @@ public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleL if (nodeFactory == null) { this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(); this.nodeFactory.init(); - this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry()); + this.discoveryTracker = LocalDiscoveryService.getTracker(context); + discoveryTracker.open(); + this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry(), discoveryTracker); } } @@ -74,7 +78,12 @@ public class OSGiServiceExporter implements ServiceTrackerCustomizer, LifeCycleL } public void stop() { - this.introspector = null; + discoveryTracker.close(); + discoveryTracker = null; + introspector = null; + nodeFactory = null; + registry = null; + context = null; } public Object addingService(ServiceReference reference) { diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java index aeaaf005c2..582adb22ef 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/remoteserviceadmin/impl/OSGiServiceImporter.java @@ -33,6 +33,7 @@ import org.apache.tuscany.sca.node.impl.NodeFactoryImpl; import org.apache.tuscany.sca.node.impl.NodeImpl; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.osgi.remoteserviceadmin.ImportRegistration; +import org.apache.tuscany.sca.osgi.service.discovery.impl.LocalDiscoveryService; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -46,6 +47,7 @@ public class OSGiServiceImporter implements LifeCycleListener { private BundleContext context; private NodeFactoryImpl nodeFactory; private EndpointIntrospector introspector; + private ServiceTracker discoveryTracker; /** * @param context @@ -60,7 +62,9 @@ public class OSGiServiceImporter implements LifeCycleListener { if (nodeFactory == null) { this.nodeFactory = (NodeFactoryImpl)NodeFactory.newInstance(); this.nodeFactory.init(); - this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry()); + this.discoveryTracker = LocalDiscoveryService.getTracker(context); + discoveryTracker.open(); + this.introspector = new EndpointIntrospector(context, getExtensionPointRegistry(), discoveryTracker); } } @@ -68,6 +72,12 @@ public class OSGiServiceImporter implements LifeCycleListener { } public void stop() { + discoveryTracker.close(); + discoveryTracker = null; + introspector = null; + nodeFactory = null; + registry = null; + context = null; } public ImportRegistration importService(Bundle bundle, EndpointDescription endpointDescription) { diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java index e798ea7a38..5b0b311d7a 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/AbstractDiscoveryService.java @@ -118,7 +118,7 @@ public abstract class AbstractDiscoveryService implements Discovery, LifeCycleLi props.put(PRODUCT_NAME, "Apache Tuscany SCA"); props.put(PRODUCT_VERSION, headers.get(Constants.BUNDLE_VERSION)); props.put(VENDOR_NAME, headers.get(Constants.BUNDLE_VENDOR)); - props.put(SUPPORTED_PROTOCOLS, new String[] {"local", "org.osgi.sca"}); + // props.put(SUPPORTED_PROTOCOLS, new String[] {"local", "org.osgi.sca"}); return props; } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java index 3709c737ee..2577a68808 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/DomainDiscoveryService.java @@ -21,6 +21,8 @@ package org.apache.tuscany.sca.osgi.service.discovery.impl; import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper.createEndpointDescription; +import java.util.Dictionary; + import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.core.UtilityExtensionPoint; @@ -105,5 +107,11 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements domainRegistryFactory.removeListener(this); super.stop(); } - + + @Override + protected Dictionary<String, Object> getProperties() { + Dictionary<String, Object> props = super.getProperties(); + props.put(SUPPORTED_PROTOCOLS, new String[] {"org.osgi.sca"}); + return props; + } } diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java index 07e0ff07a2..594fe32e54 100644 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java +++ b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/osgi/service/discovery/impl/LocalDiscoveryService.java @@ -18,51 +18,42 @@ */ package org.apache.tuscany.sca.osgi.service.discovery.impl; -import static org.osgi.framework.Bundle.ACTIVE; -import static org.osgi.framework.BundleEvent.STARTED; -import static org.osgi.framework.BundleEvent.STOPPING; +import static org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.OSGiHelper.getConfiguration; -import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; +import java.util.Collection; +import java.util.Dictionary; import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.UUID; import java.util.Map.Entry; import java.util.logging.Level; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.sca.assembly.AssemblyFactory; -import org.apache.tuscany.sca.common.java.io.IOHelper; -import org.apache.tuscany.sca.common.xml.stax.StAXHelper; -import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.ProcessorContext; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; -import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; -import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.deployment.Deployer; +import org.apache.tuscany.sca.implementation.osgi.SCAConfig; import org.apache.tuscany.sca.implementation.osgi.ServiceDescription; import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; -import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.MonitorFactory; import org.apache.tuscany.sca.osgi.remoteserviceadmin.EndpointDescription; import org.apache.tuscany.sca.osgi.remoteserviceadmin.RemoteConstants; import org.apache.tuscany.sca.osgi.remoteserviceadmin.impl.EndpointHelper; +import org.oasisopen.sca.ServiceRuntimeException; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; -import org.osgi.framework.BundleListener; import org.osgi.framework.Constants; +import org.osgi.framework.Filter; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.util.tracker.BundleTracker; +import org.osgi.util.tracker.BundleTrackerCustomizer; +import org.osgi.util.tracker.ServiceTracker; -public class LocalDiscoveryService extends AbstractDiscoveryService implements BundleListener { - private StAXHelper staxHelper; - private AssemblyFactory assemblyFactory; - private StAXArtifactProcessor processor; +public class LocalDiscoveryService extends AbstractDiscoveryService implements BundleTrackerCustomizer { + private Deployer deployer; + private BundleTracker bundleTracker; + private Collection<ExtenderConfiguration> extenders = new ArrayList<ExtenderConfiguration>(); public LocalDiscoveryService(BundleContext context) { super(context); @@ -70,87 +61,28 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B public void start() { super.start(); - context.addBundleListener(this); getExtensionPointRegistry(); - FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); - this.assemblyFactory = factories.getFactory(AssemblyFactory.class); - this.staxHelper = StAXHelper.getInstance(registry); - StAXArtifactProcessorExtensionPoint processors = - registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); UtilityExtensionPoint utilities = this.registry.getExtensionPoint(UtilityExtensionPoint.class); - MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); - Monitor monitor = null; - if (monitorFactory != null) { - monitor = monitorFactory.createMonitor(); - } - processor = - new ExtensibleStAXArtifactProcessor(processors, staxHelper.getInputFactory(), - staxHelper.getOutputFactory()); - processExistingBundles(); + this.deployer = utilities.getUtility(Deployer.class); + bundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STARTING, this); + bundleTracker.open(); } - public void bundleChanged(BundleEvent event) { + public static ServiceTracker getTracker(BundleContext context) { + Filter filter = null; try { - switch (event.getType()) { - case STARTED: - discover(event.getBundle()); - break; - case STOPPING: - removeServicesDeclaredInBundle(event.getBundle()); - break; - } - } catch (Throwable e) { - logger.log(Level.SEVERE, e.getMessage(), e); - if (e instanceof Error) { - throw (Error)e; - } else if (e instanceof RuntimeException) { - throw (RuntimeException)e; - } else { - // Should not happen - throw new RuntimeException(e); - } - } - } - - private void processExistingBundles() { - Bundle[] bundles = context.getBundles(); - if (bundles == null) { - return; - } - - for (Bundle b : bundles) { - if (b.getState() == ACTIVE) { - discover(b); - } - } - } - - private void discover(Bundle b) { - List<URL> urls = findServiceDescriptionsDocuments(b); - if (urls == null || urls.isEmpty()) { - return; - } - - List<ServiceDescription> serviceDescriptions = new ArrayList<ServiceDescription>(); - - for (URL url : urls) { - ServiceDescriptions descriptions = null; - try { - descriptions = loadServiceDescriptions(url); - } catch (Exception e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - if (descriptions != null) { - serviceDescriptions.addAll(descriptions); - } - } - - for (ServiceDescription sd : serviceDescriptions) { - EndpointDescription sed = createEndpointDescription(sd); - servicesInfo.put(sed, b); - serviceDescriptionAdded(sed); + filter = + context.createFilter("(& (" + Discovery.SUPPORTED_PROTOCOLS + + "=local) (" + + Constants.OBJECTCLASS + + "=" + + Discovery.class.getName() + + "))"); + } catch (InvalidSyntaxException e) { + throw new IllegalArgumentException(e); } + return new ServiceTracker(context, filter, null); } private EndpointDescription createEndpointDescription(ServiceDescription sd) { @@ -170,46 +102,6 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B return sed; } - private List<URL> findServiceDescriptionsDocuments(Bundle b) { - List<URL> urls = null; - String path = (String)b.getHeaders().get(ServiceDescriptions.REMOTE_SERVICE_HEADER); - if (path == null) { - Enumeration<URL> files = b.findEntries(ServiceDescriptions.REMOTE_SERVICE_FOLDER, "*.xml", false); - if (files == null || !files.hasMoreElements()) { - return Collections.emptyList(); - } else { - urls = new ArrayList<URL>(); - while (files.hasMoreElements()) { - urls.add(files.nextElement()); - } - } - } else { - URL url = b.getEntry(path); - if (url != null) { - urls = Collections.singletonList(url); - } else { - urls = Collections.emptyList(); - } - } - return urls; - } - - private ServiceDescriptions loadServiceDescriptions(URL url) throws Exception { - InputStream is = IOHelper.openStream(url); - try { - XMLStreamReader reader = staxHelper.createXMLStreamReader(is); - reader.nextTag(); - Object model = processor.read(reader, new ProcessorContext(registry)); - if (model instanceof ServiceDescriptions) { - return (ServiceDescriptions)model; - } else { - return null; - } - } finally { - is.close(); - } - } - private void removeServicesDeclaredInBundle(Bundle bundle) { for (Iterator<Map.Entry<EndpointDescription, Bundle>> i = servicesInfo.entrySet().iterator(); i.hasNext();) { Entry<EndpointDescription, Bundle> entry = i.next(); @@ -229,8 +121,85 @@ public class LocalDiscoveryService extends AbstractDiscoveryService implements B } public void stop() { - context.removeBundleListener(this); + if (bundleTracker != null) { + bundleTracker.close(); + } super.stop(); } + public Object addingBundle(Bundle bundle, BundleEvent event) { + Collection<URL> scaConfigs = getConfiguration(bundle, "SCA-Configuration", "OSGI-INF/sca-config/*.xml"); + Collection<URL> descriptions = getConfiguration(bundle, "Remote-Service", null); + if (scaConfigs.isEmpty() && descriptions.isEmpty()) { + return null; + } + ExtenderConfiguration extender = new ExtenderConfiguration(); + for (URL url : scaConfigs) { + try { + SCAConfig scaConfig = deployer.loadXMLDocument(url, deployer.createMonitor()); + extender.scaConfigs.add(scaConfig); + } catch (Exception e) { + logger.log(Level.SEVERE, e.getMessage(), e); + throw new ServiceRuntimeException(e); + } + } + for (URL url : descriptions) { + try { + ServiceDescriptions sds = deployer.loadXMLDocument(url, deployer.createMonitor()); + extender.remoteServiceDescriptions.add(sds); + } catch (Exception e) { + logger.log(Level.SEVERE, e.getMessage(), e); + // throw new ServiceRuntimeException(e); + } + } + + // Notify + for (ServiceDescriptions sds : extender.getRemoteServiceDescriptions()) { + for (ServiceDescription sd : sds) { + EndpointDescription sed = createEndpointDescription(sd); + servicesInfo.put(sed, bundle); + serviceDescriptionAdded(sed); + } + } + + this.extenders.add(extender); + return extender; + } + + public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) { + // STARTING --> ACTIVE + } + + public void removedBundle(Bundle bundle, BundleEvent event, Object object) { + if (object instanceof ExtenderConfiguration) { + extenders.remove((ExtenderConfiguration)object); + removeServicesDeclaredInBundle(bundle); + } + } + + public Collection<ExtenderConfiguration> getConfigurations() { + return extenders; + } + + public static class ExtenderConfiguration { + private Collection<SCAConfig> scaConfigs = new ArrayList<SCAConfig>(); + private Collection<ServiceDescriptions> remoteServiceDescriptions = new ArrayList<ServiceDescriptions>(); + + public Collection<ServiceDescriptions> getRemoteServiceDescriptions() { + return remoteServiceDescriptions; + } + + public Collection<SCAConfig> getSCAConfigs() { + return scaConfigs; + } + + } + + @Override + protected Dictionary<String, Object> getProperties() { + Dictionary<String, Object> props = super.getProperties(); + props.put(SUPPORTED_PROTOCOLS, new String[] {"local"}); + return props; + } + } |