From 132aa8a77685ec92bc90c03f987650d275a7b639 Mon Sep 17 00:00:00 2001 From: lresende Date: Mon, 30 Sep 2013 06:59:11 +0000 Subject: 2.0.1 RC1 release tag git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1527464 13f79535-47bb-0310-9956-ffa450edef68 --- .../osgi/xml/SCAConfigProcessor.java | 194 +++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java (limited to 'sca-java-2.x/tags/2.0.1-RC1/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java') diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java new file mode 100644 index 0000000000..86ff968d0e --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/implementation-osgi/src/main/java/org/apache/tuscany/sca/implementation/osgi/xml/SCAConfigProcessor.java @@ -0,0 +1,194 @@ +/* + * 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; + +/* + + + + + + +*/ +public class SCAConfigProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor { + 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 (Binding binding : definitions.getBindings()) { + processor.write(binding, writer, context); + } + + 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 intents = new ArrayList(); + List policySets = new ArrayList(); + List referredPolicySets = new ArrayList(); + + 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 getModelType() { + return SCAConfig.class; + } + +} -- cgit v1.2.3