diff options
Diffstat (limited to 'branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml')
9 files changed, 1350 insertions, 0 deletions
diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BluePrintComponentsProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BluePrintComponentsProcessor.java new file mode 100644 index 0000000000..ca6b0b70e1 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/BluePrintComponentsProcessor.java @@ -0,0 +1,144 @@ +/* + * 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.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +/* +<!-- A sample module-context.xml for OSGI RFC 124 (BluePrint Service) --> +<components xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> + <component id="CalculatorComponent" class="calculator.dosgi.impl.CalculatorServiceImpl"> + <property name="addService" ref="AddService" /> + <property name="subtractService" ref="SubtractService" /> + <property name="multiplyService" ref="MultiplyService" /> + <property name="divideService" ref="DivideService" /> + </component> + + <!-- We can derive the SCA services for the implementation.osgi --> + <service id="CalculatorService" ref="CalculatorComponent" interface="calculator.dosgi.CalculatorService"> + </service> + + <!-- We can derive the SCA references for the implementation.osgi --> + <reference id="AddService" interface="calculator.dosgi.operations.AddService"> + </reference> + <reference id="SubtractService" interface="calculator.dosgi.operations.SubtractService"> + </reference> + <reference id="MultiplyService" interface="calculator.dosgi.operations.MultiplyService"> + </reference> + <reference id="DivideService" interface="calculator.dosgi.operations.DivideService"> + </reference> + +</components> +*/ +public class BluePrintComponentsProcessor implements StAXArtifactProcessor<ComponentType> { + private static final String BLUE_PRINT_NS = "http://www.osgi.org/xmlns/blueprint/v1.0.0"; + private static final String COMPOMENTS = "components"; + private static final QName COMPONENTS_QNAME = new QName(BLUE_PRINT_NS, COMPOMENTS); + + private AssemblyFactory factory; + private JavaInterfaceFactory javaInterfaceFactory; + + public BluePrintComponentsProcessor(FactoryExtensionPoint modelFactories) { + this.factory = modelFactories.getFactory(AssemblyFactory.class); + this.javaInterfaceFactory = modelFactories.getFactory(JavaInterfaceFactory.class); + } + + public ComponentType read(XMLStreamReader reader, ProcessorContext context) throws XMLStreamException { + int event = reader.getEventType(); + ComponentType componentType = factory.createComponentType(); + while (true) { + switch (event) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + if (COMPONENTS_QNAME.equals(name)) { + // + } else if ("reference".equals(name.getLocalPart())) { + Reference ref = factory.createReference(); + ref.setName(reader.getAttributeValue(null, "id")); + String interfaceName = reader.getAttributeValue(null, "interface"); + InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + ref.setInterfaceContract(interfaceContract); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(); + javaInterface.setUnresolved(true); + javaInterface.setName(interfaceName); + interfaceContract.setInterface(javaInterface); + componentType.getReferences().add(ref); + } else if ("service".equals(name.getLocalPart())) { + Service service = factory.createService(); + String interfaceName = reader.getAttributeValue(null, "interface"); + InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(); + javaInterface.setUnresolved(true); + javaInterface.setName(interfaceName); + interfaceContract.setInterface(javaInterface); + componentType.getServices().add(service); + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getName(); + if (COMPONENTS_QNAME.equals(name)) { + return componentType; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + return componentType; + } + } + } + + public QName getArtifactType() { + return COMPONENTS_QNAME; + } + + public void write(ComponentType model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + // TODO: To be implemented + } + + public Class<ComponentType> getModelType() { + return ComponentType.class; + } + + public void resolve(ComponentType model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + // TODO: To be implemented + } +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiComponentProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiComponentProcessor.java new file mode 100644 index 0000000000..860d3f6f45 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiComponentProcessor.java @@ -0,0 +1,174 @@ +/* + * 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.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +/* +<scr:component name="CalculatorComponent" + xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0"> + <implementation class="calculator.dosgi.CalculatorServiceDSImpl" /> + <service> + <provide interface="calculator.dosgi.CalculatorService" /> + </service> + + <reference name="addService" interface="calculator.dosgi.operations.AddService" bind="setAddService" unbind="unsetAddService" + policy="dynamic" /> + <reference name="subtractService" interface="calculator.dosgi.operations.SubtractService" bind="setSubtractService" + unbind="unsetSubtractService" policy="dynamic" /> + <reference name="multiplyService" interface="calculator.dosgi.operations.MultiplyService" bind="setMultiplyService" + unbind="unsetMultiplyService" policy="dynamic" /> + <reference name="divideService" interface="calculator.dosgi.operations.DivideService" bind="setDivideService" + unbind="unsetDivideService" policy="dynamic" /> + +</scr:component> +*/ +public class OSGiComponentProcessor implements StAXArtifactProcessor<ComponentType> { + private static final String OSGI_SCR_NS = "http://www.osgi.org/xmlns/scr/v1.0.0"; + private static final String COMPOMENT = "component"; + private static final QName COMPONENT_QNAME = new QName(OSGI_SCR_NS, COMPOMENT); + + private AssemblyFactory factory; + private JavaInterfaceFactory javaInterfaceFactory; + + + public OSGiComponentProcessor(FactoryExtensionPoint modelFactories) { + this.factory = modelFactories.getFactory(AssemblyFactory.class); + this.javaInterfaceFactory = modelFactories.getFactory(JavaInterfaceFactory.class); + } + + public ComponentType read(XMLStreamReader reader, ProcessorContext context) throws XMLStreamException { + int event = reader.getEventType(); + ComponentType componentType = factory.createComponentType(); + while (true) { + switch (event) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + if (COMPONENT_QNAME.equals(name)) { + // + } else if ("reference".equals(name.getLocalPart())) { + Reference ref = factory.createReference(); + ref.setName(reader.getAttributeValue(null, "name")); + String interfaceName = reader.getAttributeValue(null, "interface"); + InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + ref.setInterfaceContract(interfaceContract); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(); + javaInterface.setUnresolved(true); + javaInterface.setName(interfaceName); + interfaceContract.setInterface(javaInterface); + componentType.getReferences().add(ref); + } else if ("provide".equals(name.getLocalPart())) { + Service service = factory.createService(); + String interfaceName = reader.getAttributeValue(null, "interface"); + InterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + JavaInterface javaInterface = javaInterfaceFactory.createJavaInterface(); + javaInterface.setUnresolved(true); + javaInterface.setName(interfaceName); + interfaceContract.setInterface(javaInterface); + componentType.getServices().add(service); + } else if ("property".equals(name.getLocalPart())) { + String propName = reader.getAttributeValue(null, "name"); + String propValue = reader.getAttributeValue(null, "value"); + String propType = reader.getAttributeValue(null, "type"); + if (propType == null) { + propType = "String"; + } + if (propValue == null) { + propValue = reader.getElementText(); + } + if (propValue != null) { + propValue = propValue.trim(); + } + Object prop = propValue; + if ("Integer".equals(propType)) { + prop = Integer.valueOf(propValue); + } else if ("Long".equals(propType)) { + prop = Long.valueOf(propValue); + } else if ("Float".equals(propType)) { + prop = Float.valueOf(propValue); + } else if ("Double".equals(propType)) { + prop = Double.valueOf(propValue); + } else if ("Short".equals(propType)) { + prop = Short.valueOf(propValue); + } else if ("Character".equals(propType)) { + prop = propValue.charAt(0); + } else if ("Byte".equals(propType)) { + prop = Byte.valueOf(propValue); + } else if ("Boolean".equals(propType)) { + prop = Boolean.valueOf(propValue); + } + Property property = factory.createProperty(); + property.setName(propName); + property.setValue(prop); + componentType.getProperties().add(property); + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getName(); + if (COMPONENT_QNAME.equals(name)) { + return componentType; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + return componentType; + } + } + } + + public QName getArtifactType() { + return COMPONENT_QNAME; + } + + public void write(ComponentType model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + // TODO: To be implemented + } + + public Class<ComponentType> getModelType() { + return ComponentType.class; + } + + public void resolve(ComponentType model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + // TODO: To be implemented + } +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationActivator.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationActivator.java new file mode 100644 index 0000000000..b604447696 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationActivator.java @@ -0,0 +1,47 @@ +/* + * 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 org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +/** + * Bundle activator to receive the BundleContext + */ +public class OSGiImplementationActivator implements BundleActivator { + private static BundleContext bundleContext; + + public void start(BundleContext context) throws Exception { + bundleContext = context; + } + + public void stop(BundleContext context) throws Exception { + bundleContext = null; + } + + public static BundleContext getBundleContext() { + return bundleContext; + } + + static void setBundleContext(BundleContext bundleContext) { + OSGiImplementationActivator.bundleContext = bundleContext; + } + +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java new file mode 100644 index 0000000000..d98616cbc2 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiImplementationProcessor.java @@ -0,0 +1,342 @@ +/* + * 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 static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.BUNDLE_SYMBOLICNAME; +import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.BUNDLE_VERSION; +import static org.apache.tuscany.sca.implementation.osgi.OSGiImplementation.IMPLEMENTATION_OSGI; + +import java.net.URI; +import java.net.URL; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +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.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +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.OSGiImplementation; +import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; +import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescription; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +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.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.Version; + +/** + * + * Process an <implementation.osgi/> element in a component definition. An instance of + * OSGiImplementation is created. + * Also associates the component type file with the implementation. + * + * @version $Rev$ $Date$ + */ +public class OSGiImplementationProcessor implements StAXArtifactProcessor<OSGiImplementation> { + private static final String BUNDLE_COMPONENT_TYPE = "OSGI-INF/sca/bundle.componentType"; + private static final String COMPONENT_TYPE_HEADER = "SCA-ComponentType"; + + private AssemblyFactory assemblyFactory; + private ServiceDescriptionsFactory serviceDescriptionsFactory; + private OSGiImplementationFactory osgiImplementationFactory; + private JavaInterfaceFactory javaInterfaceFactory; + + private ExtensionPointRegistry registry; + private StAXArtifactProcessor artifactProcessor; + + protected OSGiImplementationProcessor(FactoryExtensionPoint modelFactories) { + this.serviceDescriptionsFactory = modelFactories.getFactory(ServiceDescriptionsFactory.class); + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + this.osgiImplementationFactory = modelFactories.getFactory(OSGiImplementationFactory.class); + this.javaInterfaceFactory = modelFactories.getFactory(JavaInterfaceFactory.class); + } + + public OSGiImplementationProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor processor) { + this(registry.getExtensionPoint(FactoryExtensionPoint.class)); + this.artifactProcessor = processor; + this.registry = registry; + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + "impl-osgi-validation-messages", + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return IMPLEMENTATION_OSGI; + } + + public Class<OSGiImplementation> getModelType() { + return OSGiImplementation.class; + } + + public OSGiImplementation read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + assert IMPLEMENTATION_OSGI.equals(reader.getName()); + + String bundleSymbolicName = reader.getAttributeValue(null, BUNDLE_SYMBOLICNAME); + String bundleVersion = reader.getAttributeValue(null, BUNDLE_VERSION); + + OSGiImplementation implementation = osgiImplementationFactory.createOSGiImplementation(); + implementation.setBundleSymbolicName(bundleSymbolicName); + implementation.setBundleVersion(bundleVersion); + + implementation.setUnresolved(true); + + // Skip to the end of <implementation.osgi> + while (reader.hasNext()) { + int next = reader.next(); + switch (next) { + case START_ELEMENT: + break; + case END_ELEMENT: + if (IMPLEMENTATION_OSGI.equals(reader.getName())) { + return implementation; + } + break; + } + } + return implementation; + } + + public void resolve(OSGiImplementation impl, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + + if (impl == null || !impl.isUnresolved()) + return; + Monitor monitor = context.getMonitor(); + impl.setUnresolved(false); + + BundleContext bundleContext = OSGiImplementationActivator.getBundleContext(); + if (bundleContext == null) { + // FIXME: What if the OSGi is not started + return; + } + Bundle bundle = null; + for (Bundle b : bundleContext.getBundles()) { + String sn = b.getSymbolicName(); + String ver = (String)b.getHeaders().get(Constants.BUNDLE_VERSION); + if (!impl.getBundleSymbolicName().equals(sn)) { + continue; + } + Version v1 = Version.parseVersion(ver); + Version v2 = Version.parseVersion(impl.getBundleVersion()); + if (v1.equals(v2)) { + bundle = b; + break; + } + } + if (bundle != null) { + impl.setBundle(bundle); + } else { + error(monitor, "CouldNotLocateOSGiBundle", impl, impl.getBundleSymbolicName()); + //throw new ContributionResolveException("Could not locate OSGi bundle " + + //impl.getBundleSymbolicName()); + return; + } + + try { + if (introspect(impl, resolver, context, bundle)) { + return; + } + } catch (ContributionReadException e) { + throw new ContributionResolveException(e); + } + + // The bundle may be different from the current contribution + ComponentType componentType = assemblyFactory.createComponentType(); + // Try to find a bundle.componentType for the target bundle + componentType.setURI("OSGI-INF/sca/" + bundle.getSymbolicName() + "/bundle.componentType"); + componentType.setUnresolved(true); + componentType = resolver.resolveModel(ComponentType.class, componentType, context); + if (componentType.isUnresolved()) { + // Create a new instance to prevent it being treated as reentry + // See org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver.resolveModel(Class<T>, T) + componentType = assemblyFactory.createComponentType(); + // Try a generic one + componentType.setURI(BUNDLE_COMPONENT_TYPE); + componentType = resolver.resolveModel(ComponentType.class, componentType, context); + } + if (componentType.isUnresolved()) { + // Try to derive it from the service descriptions + if (!deriveFromServiceDescriptions(impl, resolver, context)) { + error(monitor, "MissingComponentTypeFile", impl, componentType.getURI()); + } + //throw new ContributionResolveException("missing .componentType side file " + ctURI); + return; + } else { + mergeFromComponentType(impl, componentType, resolver, context); + } + } + + private boolean deriveFromServiceDescriptions(OSGiImplementation impl, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + // FIXME: How to find the RFC 119 service descriptions in the contribution and + // derive the SCA component type from them? + ServiceDescriptions descriptions = serviceDescriptionsFactory.createServiceDescriptions(); + descriptions = resolver.resolveModel(ServiceDescriptions.class, descriptions, context); + if (descriptions != null && !descriptions.isEmpty()) { + ComponentType ct = assemblyFactory.createComponentType(); + int index = 0; + for (ServiceDescription ds : descriptions) { + for (String i : ds.getInterfaces()) { + Class<?> cls = getJavaClass(resolver, i, context); + JavaInterface javaInterface; + try { + javaInterface = javaInterfaceFactory.createJavaInterface(cls); + } catch (InvalidInterfaceException e) { + throw new ContributionResolveException(e); + } + Reference reference = assemblyFactory.createReference(); + JavaInterfaceContract contract = javaInterfaceFactory.createJavaInterfaceContract(); + contract.setInterface(javaInterface); + reference.setInterfaceContract(contract); + String refName = (String)ds.getProperties().get(OSGiProperty.SCA_REFERENCE); + if (refName == null) { + refName = "ref" + (index++); + } + reference.setName(refName); + reference.setUnresolved(false); + ct.getReferences().add(reference); + } + } + mergeFromComponentType(impl, ct, resolver, context); + return true; + } + return false; + } + + private void mergeFromComponentType(OSGiImplementation impl, ComponentType componentType, ModelResolver resolver, ProcessorContext context) { + List<Service> services = componentType.getServices(); + for (Service service : services) { + Interface interfaze = service.getInterfaceContract().getInterface(); + if (interfaze instanceof JavaInterface) { + JavaInterface javaInterface = (JavaInterface)interfaze; + if (javaInterface.getJavaClass() == null) { + javaInterface.setJavaClass(getJavaClass(resolver, javaInterface.getName(), context)); + } + if (service.getInterfaceContract().getCallbackInterface() instanceof JavaInterface) { + JavaInterface callbackInterface = + (JavaInterface)service.getInterfaceContract().getCallbackInterface(); + if (callbackInterface.getJavaClass() == null) { + callbackInterface.setJavaClass(getJavaClass(resolver, callbackInterface.getName(), context)); + } + } + + impl.getServices().add(service); + } + } + + List<Reference> references = componentType.getReferences(); + for (Reference reference : references) { + Interface interfaze = reference.getInterfaceContract().getInterface(); + if (interfaze instanceof JavaInterface) { + JavaInterface javaInterface = (JavaInterface)interfaze; + if (javaInterface.getJavaClass() == null) { + javaInterface.setJavaClass(getJavaClass(resolver, javaInterface.getName(), context)); + } + impl.getReferences().add(reference); + } else + impl.getReferences().add(reference); + } + + List<Property> properties = componentType.getProperties(); + for (Property property : properties) { + impl.getProperties().add(property); + } + } + + private Class<?> getJavaClass(ModelResolver resolver, String className, ProcessorContext context) { + ClassReference ref = new ClassReference(className); + ref = resolver.resolveModel(ClassReference.class, ref, context); + return ref.getJavaClass(); + } + + public void write(OSGiImplementation model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + String ns = IMPLEMENTATION_OSGI.getNamespaceURI(); + writer.writeStartElement(ns, IMPLEMENTATION_OSGI.getLocalPart()); + writer.writeAttribute(BUNDLE_SYMBOLICNAME, model.getBundleSymbolicName()); + if (model.getBundleVersion() != null) { + writer.writeAttribute(BUNDLE_VERSION, model.getBundleVersion()); + } + writer.writeEndElement(); + } + + private boolean introspect(OSGiImplementation implementation, ModelResolver resolver, ProcessorContext context, Bundle bundle) + throws ContributionReadException, ContributionResolveException { + String componentTypeFile = (String)bundle.getHeaders().get(COMPONENT_TYPE_HEADER); + if (componentTypeFile == null) { + componentTypeFile = BUNDLE_COMPONENT_TYPE; + } + URL url = bundle.getEntry(componentTypeFile); + if (url != null) { + URLArtifactProcessorExtensionPoint processors = + registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + URLArtifactProcessor<ComponentType> processor = processors.getProcessor(ComponentType.class); + ComponentType componentType = processor.read(null, URI.create(BUNDLE_COMPONENT_TYPE), url, context); + artifactProcessor.resolve(componentType, resolver, context); + mergeFromComponentType(implementation, componentType, resolver, context); + return true; + } + return false; + } + +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java new file mode 100644 index 0000000000..9bd469df06 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/OSGiPropertyProcessor.java @@ -0,0 +1,104 @@ +/* + * 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 static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.NAME; +import static org.apache.tuscany.sca.implementation.osgi.OSGiProperty.PROPERTY_QNAME; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.osgi.OSGiImplementationFactory; +import org.apache.tuscany.sca.implementation.osgi.OSGiProperty; + +/** + * A processor for <tuscany:osgi.property> + */ +public class OSGiPropertyProcessor implements StAXArtifactProcessor<OSGiProperty> { + private OSGiImplementationFactory factory; + + + public OSGiPropertyProcessor(FactoryExtensionPoint modelFactories) { + this.factory = modelFactories.getFactory(OSGiImplementationFactory.class); + } + + public OSGiProperty read(XMLStreamReader reader, ProcessorContext context) throws XMLStreamException { + int event = reader.getEventType(); + OSGiProperty prop = null; + while (true) { + switch (event) { + case START_ELEMENT: + QName name = reader.getName(); + if (PROPERTY_QNAME.equals(name)) { + prop = factory.createOSGiProperty(); + prop.setName(reader.getAttributeValue(null, NAME)); + // After the following call, the reader will be positioned at END_ELEMENT + String text = reader.getElementText(); + if (text != null) { + text = text.trim(); + } + prop.setValue(text); + return prop; + } + break; + case END_ELEMENT: + name = reader.getName(); + if (PROPERTY_QNAME.equals(name)) { + return prop; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + return prop; + } + } + } + + public QName getArtifactType() { + return PROPERTY_QNAME; + } + + public void write(OSGiProperty model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + writer.writeStartElement(PROPERTY_QNAME.getNamespaceURI(), PROPERTY_QNAME.getLocalPart()); + writer.writeAttribute(NAME, model.getName()); + writer.writeCharacters(model.getValue()); + writer.writeEndElement(); + } + + public Class<OSGiProperty> getModelType() { + return OSGiProperty.class; + } + + public void resolve(OSGiProperty model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + // TODO: To be implemented + } +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java new file mode 100644 index 0000000000..9c529eecfc --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java @@ -0,0 +1,190 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +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.BaseStAXArtifactProcessor; +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.ProcessorContext; +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.SCAConfig; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySet; + +/* +<?xml version="1.0" encoding="UTF-8"?> +<scact:sca-config targetNamespace="http://www.foocorp.com/definitions" + xmlns:scact="http://www.osgi.org/xmlns/scact/v1.0.0" + xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903" + xmlns:foocorp="http://www.foocorp.com/definitions" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.osgi.org/xmlns/scact/v1.0.0 http://www.osgi.org/xmlns/scact/v1.0.0/sca-config.xsd"> + + <sca:binding.ws name="com.foocorp.FooOrderWebServiceBinding" + uri="http://www.foocorp.com/FooOrderService" requires="sca:soap.1_2" /> + <foocorp:binding.rmi name="com.foocorp.FooOrderRMIBinding" + host="www.foocorp.com" port="8099" serviceName="FooOrderService" /> +</scact:sca-config> +*/ +public class SCAConfigProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<SCAConfig> { + private static final QName SCA_CONFIG_QNAME = new QName("http://www.osgi.org/xmlns/scact/v1.0.0", "sca-config"); + private ServiceDescriptionsFactory factory; + private StAXArtifactProcessor processor; + + public SCAConfigProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor processor) { + this.processor = processor; + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.factory = modelFactories.getFactory(ServiceDescriptionsFactory.class); + } + + public SCAConfig read(XMLStreamReader reader, ProcessorContext context) throws XMLStreamException, + ContributionReadException { + int event = reader.getEventType(); + SCAConfig definitions = factory.createSCAConfig(); + String targetNamespace = null; + while (true) { + switch (event) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + if (SCA_CONFIG_QNAME.equals(name)) { + targetNamespace = reader.getAttributeValue(null, "targetNamespace"); + definitions.setTargetNamespace(targetNamespace); + } else { + Object extension = processor.read(reader, context); + if (extension != null) { + if (extension instanceof Intent) { + Intent intent = (Intent)extension; + intent.setName(new QName(targetNamespace, intent.getName().getLocalPart())); + definitions.getIntents().add(intent); + for (Intent i : intent.getQualifiedIntents()) { + i.setName(new QName(targetNamespace, i.getName().getLocalPart())); + } + } else if (extension instanceof PolicySet) { + PolicySet policySet = (PolicySet)extension; + policySet.setName(new QName(targetNamespace, policySet.getName().getLocalPart())); + definitions.getPolicySets().add(policySet); + } else if (extension instanceof Binding) { + Binding binding = (Binding)extension; + definitions.getBindings().add(binding); + } + } + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getName(); + if (SCA_CONFIG_QNAME.equals(name)) { + return definitions; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + return definitions; + } + } + } + + public QName getArtifactType() { + return SCA_CONFIG_QNAME; + } + + public void write(SCAConfig definitions, XMLStreamWriter writer, ProcessorContext context) + throws ContributionWriteException, XMLStreamException { + + writeStartDocument(writer, + SCA_CONFIG_QNAME.getNamespaceURI(), + SCA_CONFIG_QNAME.getLocalPart(), + new XAttr("targetNamespace", definitions.getTargetNamespace())); + + for (Intent policyIntent : definitions.getIntents()) { + processor.write(policyIntent, writer, context); + } + + for (PolicySet policySet : definitions.getPolicySets()) { + processor.write(policySet, writer, context); + } + + writeEndDocument(writer); + } + + public void resolve(SCAConfig scaDefns, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + // start by adding all of the top level artifacts into the resolver as there + // are many cross artifact references in a definitions file and we don't want + // to be dependent on the order things appear + + List<Intent> intents = new ArrayList<Intent>(); + List<PolicySet> policySets = new ArrayList<PolicySet>(); + List<PolicySet> referredPolicySets = new ArrayList<PolicySet>(); + + for (Intent intent : scaDefns.getIntents()) { + intents.add(intent); + resolver.addModel(intent, context); + for (Intent i : intent.getQualifiedIntents()) { + intents.add(i); + resolver.addModel(i, context); + } + } + + for (PolicySet policySet : scaDefns.getPolicySets()) { + if (policySet.getReferencedPolicySets().isEmpty()) { + policySets.add(policySet); + } else { + referredPolicySets.add(policySet); + } + + resolver.addModel(policySet, context); + } + + // now resolve everything to ensure that any references between + // artifacts are satisfied + + for (Intent policyIntent : intents) + processor.resolve(policyIntent, resolver, context); + + for (PolicySet policySet : policySets) + processor.resolve(policySet, resolver, context); + + for (PolicySet policySet : referredPolicySets) + processor.resolve(policySet, resolver, context); + + } + + public Class<SCAConfig> getModelType() { + return SCAConfig.class; + } + +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsDocumentProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsDocumentProcessor.java new file mode 100644 index 0000000000..ad175e009c --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsDocumentProcessor.java @@ -0,0 +1,99 @@ +/* + * 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 java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; + +/** + * The service descriptions document processor + */ +public class ServiceDescriptionsDocumentProcessor implements URLArtifactProcessor<ServiceDescriptions> { + private XMLInputFactory inputFactory; + private StAXArtifactProcessor extensionProcessor; + + public ServiceDescriptionsDocumentProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor staxProcessor) { + super(); + this.extensionProcessor = staxProcessor; + this.inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class); + } + + public String getArtifactType() { + return "/OSGI-INF/remote-service/*.xml"; + } + + public ServiceDescriptions read(URL contributionURL, URI artifactURI, URL artifactURL, ProcessorContext context) + throws ContributionReadException { + InputStream is = null; + try { + URLConnection connection = artifactURL.openConnection(); + connection.setUseCaches(false); + is = connection.getInputStream(); + } catch (IOException e) { + ContributionReadException ce = new ContributionReadException(e); + throw ce; + } + try { + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + ValidatingXMLInputFactory.setMonitor(reader, context.getMonitor()); + Object result = extensionProcessor.read(reader, context); + return (ServiceDescriptions)result; + } catch (XMLStreamException e) { + ContributionReadException ce = new ContributionReadException(e); + throw ce; + } finally { + try { + if (is != null) { + is.close(); + is = null; + } + } catch (IOException ioe) { + //ignore + } + } + + } + + public Class<ServiceDescriptions> getModelType() { + return ServiceDescriptions.class; + } + + public void resolve(ServiceDescriptions model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + } + +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsModelResolver.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsModelResolver.java new file mode 100644 index 0000000000..cd62d5cfaf --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsModelResolver.java @@ -0,0 +1,59 @@ +/* + * 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 org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; + +/** + * + */ +public class ServiceDescriptionsModelResolver implements ModelResolver { + private ServiceDescriptions serviceDescriptions; + + public ServiceDescriptionsModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) { + ServiceDescriptionsFactory factory = modelFactories.getFactory(ServiceDescriptionsFactory.class); + this.serviceDescriptions = factory.createServiceDescriptions(); + } + + public void addModel(Object resolved, ProcessorContext context) { + // Merge the service descriptions + if (resolved instanceof ServiceDescriptions) { + serviceDescriptions.addAll((ServiceDescriptions)resolved); + } + } + + public Object removeModel(Object resolved, ProcessorContext context) { + // Remove the service descriptions + if (resolved instanceof ServiceDescriptions) { + serviceDescriptions.removeAll((ServiceDescriptions)resolved); + } + return resolved; + } + + public <T> T resolveModel(Class<T> modelClass, T unresolved, ProcessorContext context) { + // Always return the aggregated service descriptions + return modelClass.cast(serviceDescriptions); + } +} diff --git a/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsProcessor.java b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsProcessor.java new file mode 100644 index 0000000000..fc6e44d8d6 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/ServiceDescriptionsProcessor.java @@ -0,0 +1,191 @@ +/* + * 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 java.util.StringTokenizer; + +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.contribution.processor.BaseStAXArtifactProcessor; +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.ProcessorContext; +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.ServiceDescription; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptions; +import org.apache.tuscany.sca.implementation.osgi.ServiceDescriptionsFactory; + +/* +<?xml version="1.0" encoding="UTF-8"?> +<service-descriptions xmlns="http://www.osgi.org/xmlns/sd/v1.0.0"> + <service-description> + <provide interface="com.iona.soa.pojo.hello.HelloService"/> + <property name="service.intents">SOAP HTTP</property> + <property name="osgi.remote.configuration.type">pojo</property> + <property name="osgi.remote.configuration.pojo.address"> + http://localhost:9000/hello + </property> + </service-description> + <service-description> + <provide interface="com.iona.soa.pojo.hello.GreeterService"/> + <property name="service.intents">SOAP HTTP</property> + <property name="osgi.remote.configuration.type">pojo</property> + <property name="osgi.remote.configuration.pojo.address"> + http://localhost:9005/greeter + </property> + </service-description> +</service-descriptions> +*/ +public class ServiceDescriptionsProcessor extends BaseStAXArtifactProcessor implements + StAXArtifactProcessor<ServiceDescriptions> { + private ServiceDescriptionsFactory factory; + private StAXArtifactProcessor processor; + + + public ServiceDescriptionsProcessor(ExtensionPointRegistry registry, + StAXArtifactProcessor processor) { + this.processor = processor; + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.factory = modelFactories.getFactory(ServiceDescriptionsFactory.class); + } + + public ServiceDescriptions read(XMLStreamReader reader, ProcessorContext context) throws XMLStreamException, ContributionReadException { + int event = reader.getEventType(); + ServiceDescriptions sds = factory.createServiceDescriptions(); + ServiceDescription sd = null; + while (true) { + switch (event) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + if (ServiceDescriptions.SERVICE_DESCRIPTION_QNAME.equals(name)) { + sd = factory.createServiceDescription(); + sds.add(sd); + } else if ("provide".equals(name.getLocalPart())) { + String interfaceName = reader.getAttributeValue(null, "interface"); + if (interfaceName != null) { + sd.getInterfaces().add(interfaceName); + } + } else if ("property".equals(name.getLocalPart())) { + String propName = reader.getAttributeValue(null, "name"); + String propValue = reader.getAttributeValue(null, "value"); + String propType = reader.getAttributeValue(null, "type"); + if (propType == null) { + propType = "String"; + } + if (propValue == null) { + propValue = reader.getElementText(); + } + if (propValue != null) { + propValue = propValue.trim(); + } + Object prop = propValue; + if ("Integer".equals(propType)) { + prop = Integer.valueOf(propValue); + } else if ("Long".equals(propType)) { + prop = Long.valueOf(propValue); + } else if ("Float".equals(propType)) { + prop = Float.valueOf(propValue); + } else if ("Double".equals(propType)) { + prop = Double.valueOf(propValue); + } else if ("Short".equals(propType)) { + prop = Short.valueOf(propValue); + } else if ("Character".equals(propType)) { + prop = propValue.charAt(0); + } else if ("Byte".equals(propType)) { + prop = Byte.valueOf(propValue); + } else if ("Boolean".equals(propType)) { + prop = Boolean.valueOf(propValue); + } + if (propName.endsWith(".intents")) { + prop = toQNames(reader, propValue); + } + sd.getProperties().put(propName, prop); + } else { + name = reader.getName(); + if (!ServiceDescriptions.SERVICE_DESCRIPTIONS_QNAME.equals(name)) { + Object ext = processor.read(reader, context); + if (sd != null) { + sd.getProperties().put(name.toString(), ext); + } + } + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getName(); + if (ServiceDescriptions.SERVICE_DESCRIPTION_QNAME.equals(name)) { + // Reset the sd + sd = null; + } + if (ServiceDescriptions.SERVICE_DESCRIPTIONS_QNAME.equals(name)) { + return sds; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + return sds; + } + } + } + + /** + * Convert ns1:e1 ns2:e2 to {http://ns1}e1 {http://ns2}e2 + * @param reader + * @param value + * @return + */ + private String toQNames(XMLStreamReader reader, String value) { + if (value == null) { + return null; + } + StringBuffer sb = new StringBuffer(); + for (StringTokenizer tokens = new StringTokenizer(value, " \t\n\r\f,"); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + sb.append(qname.toString()).append(' '); + } + return sb.toString().trim(); + } + + public QName getArtifactType() { + return ServiceDescriptions.SERVICE_DESCRIPTIONS_QNAME; + } + + public void write(ServiceDescriptions model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + // TODO: To be implemented + } + + public Class<ServiceDescriptions> getModelType() { + return ServiceDescriptions.class; + } + + public void resolve(ServiceDescriptions model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + // TODO: To be implemented + } +} |