From b264fae6da998eb032462b7287711d891498cd3e Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 16 Jul 2009 06:52:55 +0000 Subject: Refactor the OSGi discovery service into node-implementation-osgi git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@794553 13f79535-47bb-0310-9956-ffa450edef68 --- .../implementation/osgi/BindingDescriptions.java | 34 ++ .../osgi/ServiceDescriptionsFactory.java | 2 + .../osgi/impl/BindingDescriptionsImpl.java | 36 ++ .../osgi/impl/ServiceDescriptionsFactoryImpl.java | 5 + .../introspection/ExportedServiceIntrospector.java | 422 +++++++++++++++++++++ .../osgi/xml/BindingDescriptionsProcessor.java | 117 ++++++ ...ca.contribution.processor.StAXArtifactProcessor | 1 + 7 files changed, 617 insertions(+) create mode 100644 java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/BindingDescriptions.java create mode 100644 java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/BindingDescriptionsImpl.java create mode 100644 java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/introspection/ExportedServiceIntrospector.java create mode 100644 java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BindingDescriptionsProcessor.java (limited to 'java/sca/modules/implementation-osgi/src') diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/BindingDescriptions.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/BindingDescriptions.java new file mode 100644 index 0000000000..068374c7cf --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/BindingDescriptions.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.osgi; + +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Binding; + +/** + * OSGi RFC 119 SCA binding descriptions + */ +public interface BindingDescriptions extends List { + String OSGI_SD_NS = ServiceDescriptions.OSGI_SD_NS; + QName BINDINGS_QNAME = new QName(OSGI_SD_NS, "bindings"); +} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/ServiceDescriptionsFactory.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/ServiceDescriptionsFactory.java index 0e4c51b1f9..249377e3ea 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/ServiceDescriptionsFactory.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/ServiceDescriptionsFactory.java @@ -26,4 +26,6 @@ public interface ServiceDescriptionsFactory { ServiceDescriptions createServiceDescriptions(); ServiceDescription createServiceDescription(); + + BindingDescriptions createBindingDescriptions(); } diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/BindingDescriptionsImpl.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/BindingDescriptionsImpl.java new file mode 100644 index 0000000000..67a724877d --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/BindingDescriptionsImpl.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.osgi.impl; + +import java.util.ArrayList; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.implementation.osgi.BindingDescriptions; + +/** + * + */ +public class BindingDescriptionsImpl extends ArrayList implements BindingDescriptions { + private static final long serialVersionUID = 7647814680087128002L; + + public BindingDescriptionsImpl() { + super(); + } +} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionsFactoryImpl.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionsFactoryImpl.java index 8cd3867610..625f1e7cfb 100644 --- a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionsFactoryImpl.java +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/impl/ServiceDescriptionsFactoryImpl.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.implementation.osgi.impl; +import org.apache.tuscany.sca.implementation.osgi.BindingDescriptions; import org.apache.tuscany.sca.implementation.osgi.ServiceDescription; import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; @@ -40,4 +41,8 @@ public class ServiceDescriptionsFactoryImpl implements ServiceDescriptionsFactor return new ServiceDescriptionsImpl(); } + public BindingDescriptions createBindingDescriptions() { + return new BindingDescriptionsImpl(); + } + } diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/introspection/ExportedServiceIntrospector.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/introspection/ExportedServiceIntrospector.java new file mode 100644 index 0000000000..f6abf3b2b4 --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/introspection/ExportedServiceIntrospector.java @@ -0,0 +1,422 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.osgi.introspection; + +import static org.apache.tuscany.sca.assembly.Base.SCA11_TUSCANY_NS; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.OSGI_REMOTE_CONFIGURATION_TYPE; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.OSGI_REMOTE_CONFIGURATION_TYPE_SCA; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.OSGI_REMOTE_INTENTS; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.OSGI_REMOTE_INTERFACES; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.SCA_BINDINGS; +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.Collections; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +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; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Reference; +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.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.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.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.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.osgi.framework.Bundle; +import org.osgi.framework.Filter; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; + +/** + * Introspect an OSGi Service to create an SCA composite that contains a single component with + * implementation.osgi + */ +public class ExportedServiceIntrospector { + 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; + + /** + * @param intentName + * @return + */ + private static QName getQName(String intentName) { + QName qname; + if (intentName.startsWith("{")) { + int i = intentName.indexOf('}'); + if (i != -1) { + qname = new QName(intentName.substring(1, i), intentName.substring(i + 1)); + } else { + throw new IllegalArgumentException("Invalid intent: " + intentName); + } + } else { + // Default to SCA namespace + qname = new QName(Base.SCA11_NS, intentName); + } + return qname; + } + + /** + * @param registry + */ + public ExportedServiceIntrospector(ExtensionPointRegistry registry) { + super(); + this.registry = registry; + this.factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.modelResolvers = registry.getExtensionPoint(ModelResolverExtensionPoint.class); + this.assemblyFactory = factories.getFactory(AssemblyFactory.class); + 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); + Monitor monitor = null; + if (monitorFactory != null) { + monitor = monitorFactory.createMonitor(); + } + processor = new ExtensibleStAXArtifactProcessor(processors, xmlInputFactory, xmlOutputFactory, monitor); + } + + private Intent getIntent(String intent) { + QName name = getQName(intent); + Intent i = policyFactory.createIntent(); + i.setName(name); + return i; + } + + private List getIntents(String[] intents) { + if (intents == null || intents.length == 0) { + return Collections.emptyList(); + } + List intentList = new ArrayList(); + for (String i : intents) { + Intent intent = getIntent(i); + if (intent != null) { + intentList.add(intent); + } + } + return intentList; + } + + private Map getProperties(ServiceReference reference) { + String[] names = reference.getPropertyKeys(); + if (names != null) { + Map properties = new HashMap(); + for (String name : names) { + properties.put(name, reference.getProperty(name)); + } + return properties; + } else { + return Collections.emptyMap(); + } + } + + /** + * Parse the Stringp[] to support values that are separated by comma + * @param interfaces + * @return + */ + private String[] parse(String[] interfaces) { + if (interfaces == null) { + return null; + } + List names = new ArrayList(); + for (String i : interfaces) { + String[] parts = i.split(","); + for (String p : parts) { + names.add(p.trim()); + } + } + return names.toArray(new String[names.size()]); + } + + /** + * Introspect a local OSGi Service represented by the ServiceReference to create + * an SCA service with the required intents and bindings + * @param reference The service reference for a local OSGi service + * @return An SCA contribution with a deployable composite for the SCA service + * @throws Exception + */ + public Contribution introspect(ServiceReference reference) throws Exception { + Map properties = getProperties(reference); + + OSGiProperty serviceID = implementationFactory.createOSGiProperty(); + serviceID.setName(SERVICE_ID); + // The service.id is Long + serviceID.setValue(String.valueOf(reference.getProperty(SERVICE_ID))); + + String id = "osgi.service." + serviceID.getValue(); + Composite composite = assemblyFactory.createComposite(); + composite.setName(new QName(SCA11_TUSCANY_NS, id)); + + Component component = assemblyFactory.createComponent(); + component.setName(id); + component.setAutowire(Boolean.TRUE); + + composite.getComponents().add(component); + + Bundle bundle = reference.getBundle(); + OSGiImplementation implementation = implementationFactory.createOSGiImplementation(); + + implementation.setBundle(bundle); + component.setImplementation(implementation); + implementation.setUnresolved(false); + + String[] remoteInterfaces = (String[])reference.getProperty(OSGI_REMOTE_INTERFACES); + if (remoteInterfaces == null || remoteInterfaces.length > 0 && "*".equals(remoteInterfaces[0])) { + remoteInterfaces = (String[])reference.getProperty(OBJECTCLASS); + } else { + remoteInterfaces = parse(remoteInterfaces); + } + for (String intf : remoteInterfaces) { + Service service = assemblyFactory.createService(); + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + Class interfaceClass = bundle.loadClass(intf); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(interfaceClass); + interfaceContract.setInterface(javaInterface); + if (javaInterface.getCallbackClass() != null) { + interfaceContract.setCallbackInterface(javaInterfaceFactory.createJavaInterface(javaInterface + .getCallbackClass())); + } + + service.setName(interfaceClass.getSimpleName()); + service.setInterfaceContract(interfaceContract); + + service.getExtensions().add(serviceID); + + implementation.getServices().add(service); + + ComponentService componentService = assemblyFactory.createComponentService(); + component.getServices().add(componentService); + componentService.setService(service); + } + + String[] requiredIntents = (String[])properties.get(OSGI_REMOTE_INTENTS); + List intents = getIntents(requiredIntents); + + String[] bindingDocuments = (String[])properties.get(SCA_BINDINGS); + List bindings = loadBindings(reference.getBundle(), bindingDocuments); + + for (ComponentService componentService : component.getServices()) { + componentService.getRequiredIntents().addAll(intents); + componentService.getBindings().addAll(bindings); + } + + // FIXME: Should we scan the owning bundle to create the SCA contribution? + Contribution contribution = contributionFactory.createContribution(); + contribution.setURI("urn:" + id); + contribution.setLocation(bundle.getEntry("/").toString()); + contribution.getDeployables().add(composite); + ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, factories); + contribution.setModelResolver(modelResolver); + contribution.setUnresolved(true); + return contribution; + } + + /** + * Introspect an OSGi filter to create an SCA reference + * + * @param bundle + * @param filterStr + * @param properties + * @return + * @throws Exception + */ + public Contribution introspect(Bundle bundle, String filterStr, Map properties) throws Exception { + Filter filter = null; + try { + filter = bundle.getBundleContext().createFilter(filterStr); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + return null; + } + Dictionary props = new Hashtable(); + props.put(OSGI_REMOTE_CONFIGURATION_TYPE, OSGI_REMOTE_CONFIGURATION_TYPE_SCA); + if(!filter.match(props)) { + return null; + } + String id = "osgi.reference." + UUID.randomUUID(); + Composite composite = assemblyFactory.createComposite(); + composite.setName(new QName(Base.SCA11_TUSCANY_NS, id)); + + Component component = assemblyFactory.createComponent(); + component.setName(id); + component.setAutowire(Boolean.TRUE); + + composite.getComponents().add(component); + + OSGiImplementation implementation = implementationFactory.createOSGiImplementation(); + + implementation.setBundle(bundle); + component.setImplementation(implementation); + implementation.setUnresolved(false); + + String[] remoteInterfaces = (String[])properties.get(OSGiProperty.OSGI_REMOTE_INTERFACES); + if (remoteInterfaces == null || remoteInterfaces.length > 0 && "*".equals(remoteInterfaces[0])) { + remoteInterfaces = (String[])properties.get(OBJECTCLASS); + } else { + remoteInterfaces = parse(remoteInterfaces); + } + for (String intf : remoteInterfaces) { + Reference reference = assemblyFactory.createReference(); + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + Class interfaceClass = bundle.loadClass(intf); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(interfaceClass); + interfaceContract.setInterface(javaInterface); + if (javaInterface.getCallbackClass() != null) { + interfaceContract.setCallbackInterface(javaInterfaceFactory.createJavaInterface(javaInterface + .getCallbackClass())); + } + + reference.setName(id); + reference.setInterfaceContract(interfaceContract); + + reference.getExtensions().add(filter); + + implementation.getReferences().add(reference); + + ComponentReference componentReference = assemblyFactory.createComponentReference(); + component.getReferences().add(componentReference); + componentReference.setReference(reference); + componentReference.setWiredByImpl(true); + } + + String[] requiredIntents = (String[])properties.get(OSGI_REMOTE_INTENTS); + List intents = getIntents(requiredIntents); + + String[] bindingDocuments = (String[])properties.get(SCA_BINDINGS); + List bindings = loadBindings(bundle, bindingDocuments); + + for (ComponentReference componentReference : component.getReferences()) { + componentReference.getRequiredIntents().addAll(intents); + componentReference.getBindings().addAll(bindings); + } + + // FIXME: Should we scan the owning bundle to create the SCA contribution? + Contribution contribution = contributionFactory.createContribution(); + contribution.setURI("urn:" + id); + contribution.setLocation(bundle.getEntry("/").toString()); + contribution.getDeployables().add(composite); + ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, factories); + contribution.setModelResolver(modelResolver); + contribution.setUnresolved(true); + return contribution; + } + + private List loadBindings(Bundle bundle, String[] bindingDocuments) throws IOException, + ContributionReadException { + if (bindingDocuments == null || bindingDocuments.length == 0) { + return Collections.emptyList(); + } + List bindings = new ArrayList(); + 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)); + } + return bindings; + } + + private List loadBindings(URL url) throws ContributionReadException, IOException { + InputStream is = url.openStream(); + try { + XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(is); + reader.nextTag(); + Object model = processor.read(reader); + if (model instanceof BindingDescriptions) { + return ((BindingDescriptions)model); + } else { + return Collections.emptyList(); + } + } 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(); + } + return bundle.getEntry(location); + } + +} diff --git a/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BindingDescriptionsProcessor.java b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BindingDescriptionsProcessor.java new file mode 100644 index 0000000000..0a94c6ae0a --- /dev/null +++ b/java/sca/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BindingDescriptionsProcessor.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.osgi.xml; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.osgi.BindingDescriptions; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; +import org.apache.tuscany.sca.monitor.Monitor; + +/* + + + + + +*/ +public class BindingDescriptionsProcessor implements StAXArtifactProcessor { + private Monitor monitor; + private StAXArtifactProcessor processor; + private ServiceDescriptionsFactory factory; + + public BindingDescriptionsProcessor(ExtensionPointRegistry registry, + StAXArtifactProcessor processor, + Monitor monitor) { + this.monitor = monitor; + this.processor = processor; + this.factory = + registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(ServiceDescriptionsFactory.class); + } + + public BindingDescriptions read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + BindingDescriptions bindings = factory.createBindingDescriptions(); + boolean exit = false; + while (!exit) { + int event = reader.getEventType(); + switch (event) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + if (!"bindings".equals(name.getLocalPart())) { + Object element = null; + try { + element = processor.read(reader); + } catch (ContributionReadException e) { + throw e; + } + if (element instanceof Binding) { + bindings.add((Binding)element); + } + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getName(); + if ("bindings".equals(name.getLocalPart())) { + exit = true; + } + break; + } + if (reader.hasNext()) { + reader.next(); + } else { + exit = true; + } + } + return bindings; + } + + public QName getArtifactType() { + return BindingDescriptions.BINDINGS_QNAME; + } + + public void write(BindingDescriptions model, XMLStreamWriter writer) throws ContributionWriteException, + XMLStreamException { + writer.writeStartElement(BindingDescriptions.OSGI_SD_NS, "bindings"); + for (Binding binding : model) { + processor.write(model, writer); + } + writer.writeEndElement(); + } + + public Class getModelType() { + return BindingDescriptions.class; + } + + public void resolve(BindingDescriptions model, ModelResolver resolver) throws ContributionResolveException { + // TODO: To be implemented + } +} diff --git a/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor index 6407305a77..045159deae 100644 --- a/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor +++ b/java/sca/modules/implementation-osgi/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -19,3 +19,4 @@ org.apache.tuscany.sca.implementation.osgi.xml.OSGiImplementationProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#implementation.osgi,model=org.apache.tuscany.sca.implementation.osgi.OSGiImplementation org.apache.tuscany.sca.implementation.osgi.xml.ServiceDescriptionsProcessor;qname=http://www.osgi.org/xmlns/sd/v1.0.0#service-descriptions,model=org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions org.apache.tuscany.sca.implementation.osgi.xml.OSGiPropertyProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#osgi.property,model=org.apache.tuscany.sca.implementation.osgi.OSGiProperty +org.apache.tuscany.sca.implementation.osgi.xml.BindingDescriptionsProcessor;qname=http://www.osgi.org/xmlns/sd/v1.0.0#bindings,model=org.apache.tuscany.sca.implementation.osgi.BindingDescriptions \ No newline at end of file -- cgit v1.2.3