From 56ae68a329e4333c753e4cb5df7cd49e550ddc43 Mon Sep 17 00:00:00 2001 From: lresende Date: Mon, 15 Sep 2008 17:57:26 +0000 Subject: TUSCANY-2463 - Merging attribute extension support from trunk git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@695565 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/assembly/xml/BaseAssemblyProcessor.java | 42 +++ .../sca/assembly/xml/ComponentTypeProcessor.java | 6 +- .../sca/assembly/xml/CompositeProcessor.java | 131 ++++++++- .../assembly/xml/ReadWriteAttributeTestCase.java | 106 +++++++ .../sca/assembly/xml/TestAttributeProcessor.java | 61 ++++ .../sca/assembly/xml/CalculatorExtended.composite | 54 ++++ .../apache/tuscany/sca/contribution/Constants.java | 30 ++ ...DefaultStAXArtifactProcessorExtensionPoint.java | 116 +++++--- ...efaultStAXAttributeProcessorExtensionPoint.java | 327 +++++++++++++++++++++ .../ExtensibleStAXAttributeProcessor.java | 224 ++++++++++++++ .../processor/StAXAttributeProcessor.java | 63 ++++ .../StAXAttributeProcessorExtensionPoint.java | 29 ++ ....processor.StAXAttributeProcessorExtensionPoint | 18 ++ 13 files changed, 1143 insertions(+), 64 deletions(-) create mode 100644 branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java create mode 100644 branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java create mode 100644 branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite create mode 100644 branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java create mode 100644 branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXAttributeProcessorExtensionPoint.java create mode 100644 branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java create mode 100644 branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessor.java create mode 100644 branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessorExtensionPoint.java create mode 100644 branches/sca-java-1.3.2/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java index ae33a4da16..06b208540a 100644 --- a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java @@ -49,6 +49,7 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.ConfiguredOperation; import org.apache.tuscany.sca.assembly.ConstrainingType; import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Extensible; import org.apache.tuscany.sca.assembly.Implementation; import org.apache.tuscany.sca.assembly.Multiplicity; import org.apache.tuscany.sca.assembly.OperationsConfigurator; @@ -58,9 +59,11 @@ import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.service.ContributionReadException; import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Problem; @@ -832,6 +835,45 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor implement } } + /** + * + * @param reader + * @param elementName + * @param estensibleElement + * @param extensionAttributeProcessor + * @throws ContributionReadException + * @throws XMLStreamException + */ + protected void readExtendedAttributes(XMLStreamReader reader, QName elementName, Extensible estensibleElement, StAXAttributeProcessor extensionAttributeProcessor) throws ContributionReadException, XMLStreamException { + for (int a = 0; a < reader.getAttributeCount(); a++) { + QName attributeName = reader.getAttributeName(a); + if( attributeName.getNamespaceURI() != null && attributeName.getNamespaceURI().length() > 0) { + if( ! elementName.getNamespaceURI().equals(attributeName.getNamespaceURI()) ) { + String attributeExtension = (String) extensionAttributeProcessor.read(attributeName, reader); + estensibleElement.getExtensions().add(attributeExtension); + } + } + } + } + + + /** + * + * @param attributeModel + * @param writer + * @param extensibleElement + * @param extensionAttributeProcessor + * @throws ContributionWriteException + * @throws XMLStreamException + */ + protected void writeExtendedAttributes(XMLStreamWriter writer, Extensible extensibleElement, StAXAttributeProcessor extensionAttributeProcessor) throws ContributionWriteException, XMLStreamException { + for(Object o : extensibleElement.getExtensions()) { + //FIXME How to identify it's a extended attribute ? + if(o instanceof String) { + extensionAttributeProcessor.write(o, writer); + } + } + } /*protected void validatePolicySets(PolicySetAttachPoint policySetAttachPoint) throws ContributionResolveException { diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java index 73a64702e4..9727b89aab 100644 --- a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java @@ -38,6 +38,7 @@ import org.apache.tuscany.sca.assembly.Reference; import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.service.ContributionReadException; import org.apache.tuscany.sca.contribution.service.ContributionResolveException; @@ -45,10 +46,10 @@ import org.apache.tuscany.sca.contribution.service.ContributionWriteException; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.OperationImpl; +import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.policy.IntentAttachPoint; import org.apache.tuscany.sca.policy.PolicyFactory; import org.apache.tuscany.sca.policy.PolicySetAttachPoint; -import org.apache.tuscany.sca.monitor.Monitor; import org.w3c.dom.Document; /** @@ -65,7 +66,7 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA * @param registry */ public ComponentTypeProcessor(AssemblyFactory factory, PolicyFactory policyFactory, - StAXArtifactProcessor extensionProcessor, Monitor monitor) { + StAXArtifactProcessor extensionProcessor, StAXAttributeProcessor extensionAttributeProcessor, Monitor monitor) { super(factory, policyFactory, extensionProcessor, monitor); } @@ -77,6 +78,7 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA */ public ComponentTypeProcessor(ModelFactoryExtensionPoint modelFactories, StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, Monitor monitor) { super(modelFactories.getFactory(AssemblyFactory.class), modelFactories.getFactory(PolicyFactory.class), extensionProcessor, monitor); diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java index c1a8bb5b86..09ec7664ce 100644 --- a/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java @@ -59,6 +59,7 @@ import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.resolver.ResolverExtension; import org.apache.tuscany.sca.contribution.service.ContributionReadException; @@ -88,16 +89,25 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt // FIXME: to be refactored private XPathFactory xPathFactory = XPathFactory.newInstance(); + protected StAXAttributeProcessor extensionAttributeProcessor; + /** * Construct a new composite processor * * @param extensionPoints * @param extensionProcessor */ - public CompositeProcessor(ExtensionPointRegistry extensionPoints, - StAXArtifactProcessor extensionProcessor, - Monitor monitor) { - this(modelFactories(extensionPoints), extensionProcessor, monitor(extensionPoints)); + public CompositeProcessor(ExtensionPointRegistry extensionPoints, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, + Monitor monitor) { + + this(modelFactories(extensionPoints), + extensionProcessor, + extensionAttributeProcessor, + monitor(extensionPoints)); + + this.extensionAttributeProcessor = extensionAttributeProcessor; } /** @@ -108,12 +118,18 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt * @param monitor */ private CompositeProcessor(ModelFactoryExtensionPoint modelFactories, - StAXArtifactProcessor extensionProcessor, - Monitor monitor) { - super(modelFactories.getFactory(ContributionFactory.class), + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, + Monitor monitor) { + + super(modelFactories.getFactory(ContributionFactory.class), modelFactories.getFactory(AssemblyFactory.class), modelFactories.getFactory(PolicyFactory.class), - extensionProcessor, monitor); + extensionProcessor, + monitor); + + this.extensionAttributeProcessor = extensionAttributeProcessor; + } /** @@ -125,10 +141,11 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt * @param extensionProcessor */ public CompositeProcessor(ContributionFactory contributionFactory, - AssemblyFactory assemblyFactory, - PolicyFactory policyFactory, - StAXArtifactProcessor extensionProcessor, - Monitor monitor) { + AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, + Monitor monitor) { super(contributionFactory, assemblyFactory, policyFactory, extensionProcessor, monitor); } @@ -169,6 +186,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt if(isSet(reader, AUTOWIRE)) { composite.setAutowire(getBoolean(reader, AUTOWIRE)); } + + //handle extension attributes + this.readExtendedAttributes(reader, name, composite, extensionAttributeProcessor); + composite.setLocal(getBoolean(reader, LOCAL)); composite.setConstrainingType(readConstrainingType(reader)); policyProcessor.readPolicies(composite, reader); @@ -189,6 +210,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt componentService = assemblyFactory.createComponentService(); contract = componentService; componentService.setName(getString(reader, NAME)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentService, extensionAttributeProcessor); + component.getServices().add(componentService); policyProcessor.readPolicies(contract, reader); } else { @@ -222,6 +247,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt compositeService.setPromotedService(promotedService); } + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeService, extensionAttributeProcessor); + composite.getServices().add(compositeService); policyProcessor.readPolicies(contract, reader); } @@ -238,6 +266,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } readTargets(componentReference, reader); componentReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentReference, extensionAttributeProcessor); + component.getReferences().add(componentReference); policyProcessor.readPolicies(contract, reader); } else { @@ -258,7 +290,11 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } } compositeReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); - composite.getReferences().add(compositeReference); + + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeReference, extensionAttributeProcessor); + + composite.getReferences().add(compositeReference); policyProcessor.readPolicies(contract, reader); } @@ -298,6 +334,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } } componentProperty.setFile(getString(reader, FILE)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentProperty, extensionAttributeProcessor); + policyProcessor.readPolicies(property, reader); readAbstractProperty(componentProperty, reader); @@ -338,6 +378,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt if (isSet(reader, URI)) { component.setURI(getString(reader, URI)); } + + //handle extension attributes + this.readExtendedAttributes(reader, name, component, extensionAttributeProcessor); + component.setConstrainingType(readConstrainingType(reader)); composite.getComponents().add(component); policyProcessor.readPolicies(component, reader); @@ -356,6 +400,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt target.setName(getString(reader, TARGET)); wire.setTarget(target); + //handle extension attributes + this.readExtendedAttributes(reader, name, wire, extensionAttributeProcessor); + composite.getWires().add(wire); policyProcessor.readPolicies(wire, reader); @@ -364,6 +411,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt // Read a callback = assemblyFactory.createCallback(); contract.setCallback(callback); + + //handle extension attributes + this.readExtendedAttributes(reader, name, callback, extensionAttributeProcessor); + policyProcessor.readPolicies(callback, reader); } else if (OPERATION_QNAME.equals(name)) { @@ -396,6 +447,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Composite implementation = assemblyFactory.createComposite(); implementation.setName(getQName(reader, NAME)); implementation.setUnresolved(true); + + //handle extension attributes + this.readExtendedAttributes(reader, name, implementation, extensionAttributeProcessor); + component.setImplementation(implementation); policyProcessor.readPolicies(implementation, reader); } else { @@ -527,6 +582,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(NAME, composite.getName().getLocalPart()), new XAttr(AUTOWIRE, composite.getAutowire()), policyProcessor.writePolicies(composite)); + + //write extended attributes + this.writeExtendedAttributes(writer, composite, extensionAttributeProcessor); // Write elements for (Composite include : composite.getIncludes()) { @@ -535,6 +593,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt INCLUDE, new XAttr(NAME, include.getName()), new XAttr(URI, uri)); + + //write extended attributes + this.writeExtendedAttributes(writer, include, extensionAttributeProcessor); + writeEnd(writer); } @@ -556,6 +618,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), new XAttr(PROMOTE, promote), policyProcessor.writePolicies(service)); + //write extended attributes + this.writeExtendedAttributes(writer, service, extensionAttributeProcessor); + + // Write service interface extensionProcessor.write(service.getInterfaceContract(), writer); @@ -570,6 +636,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write callback bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -598,10 +667,17 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(AUTOWIRE, component.getAutowire()), policyProcessor.writePolicies(component)); + //write extended attributes + this.writeExtendedAttributes(writer, component, extensionAttributeProcessor); + // Write the component implementation Implementation implementation = component.getImplementation(); if (implementation instanceof Composite) { writeStart(writer, IMPLEMENTATION_COMPOSITE, new XAttr(NAME, ((Composite)implementation).getName())); + + //write extended attributes + this.writeExtendedAttributes(writer, (Composite)implementation, extensionAttributeProcessor); + writeEnd(writer); } else { extensionProcessor.write(component.getImplementation(), writer); @@ -612,6 +688,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), policyProcessor.writePolicies(service)); + //write extended attributes + this.writeExtendedAttributes(writer, service, extensionAttributeProcessor); + // Write service interface extensionProcessor.write(service.getInterfaceContract(), writer); @@ -624,7 +703,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt if (service.getCallback() != null) { Callback callback = service.getCallback(); writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); - + + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -653,6 +735,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeTargets(reference), policyProcessor.writePolicies(reference)); + //write extended attributes + this.writeExtendedAttributes(writer, reference, extensionAttributeProcessor); + // Write reference interface extensionProcessor.write(reference.getInterfaceContract(), writer); @@ -666,6 +751,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Callback callback = reference.getCallback(); writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write callback bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -700,6 +788,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(FILE, property.getFile()), policyProcessor.writePolicies(property)); + //write extended attributes + this.writeExtendedAttributes(writer, property, extensionAttributeProcessor); + // Write property value writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); @@ -729,6 +820,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(PROMOTE, promote), policyProcessor.writePolicies(reference)); + //write extended attributes + this.writeExtendedAttributes(writer, reference, extensionAttributeProcessor); + // Write reference interface extensionProcessor.write(reference.getInterfaceContract(), writer); @@ -742,6 +836,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Callback callback = reference.getCallback(); writeStart(writer, CALLBACK); + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor); + // Write callback bindings for (Binding binding : callback.getBindings()) { extensionProcessor.write(binding, writer); @@ -774,6 +871,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt new XAttr(ELEMENT, property.getXSDElement()), policyProcessor.writePolicies(property)); + //write extended attributes + this.writeExtendedAttributes(writer, property, extensionAttributeProcessor); + // Write property value writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); @@ -790,6 +890,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeStart(writer, WIRE, new XAttr(SOURCE, wire.getSource().getName()), new XAttr(TARGET, wire .getTarget().getName())); + //write extended attributes + this.writeExtendedAttributes(writer, wire, extensionAttributeProcessor); + // Write extensions for (Object extension : wire.getExtensions()) { extensionProcessor.write(extension, writer); diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java new file mode 100644 index 0000000000..437b8928b1 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java @@ -0,0 +1,106 @@ +/* + * 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.assembly.xml; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.assembly.Composite; +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.processor.StAXAttributeProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * Test reading SCA XML assemblies. + * + * @version $Rev$ $Date$ + */ +public class ReadWriteAttributeTestCase extends TestCase { + + private XMLInputFactory inputFactory; + private ExtensibleStAXArtifactProcessor staxProcessor; + + private static final QName ATTRIBUTE = new QName("http://test", "customAttribute"); + + private static final String XML = + "" + + "" + + "" + + "" + + "" + + ""+ + "" + + "" + + ""+ + "" + + "" + + "" + + "" + + ""; + + @Override + public void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + + StAXAttributeProcessorExtensionPoint staxAttributeProcessors = extensionPoints.getExtensionPoint(StAXAttributeProcessorExtensionPoint.class); + staxAttributeProcessors.addArtifactProcessor(new TestAttributeProcessor()); + + + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance(), null); + } + + @Override + public void tearDown() throws Exception { + + } + + public void testReadComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("CalculatorExtended.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + is.close(); + } + + public void testWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("CalculatorExtended.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + is.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + assertEquals(XML, bos.toString()); + } +} diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java new file mode 100644 index 0000000000..691145a30e --- /dev/null +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/TestAttributeProcessor.java @@ -0,0 +1,61 @@ +/* + * 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.assembly.xml; + +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.BaseStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; + +/** + * A Policy Processor used for testing. + * + * @version $Rev$ $Date$ + */ +public class TestAttributeProcessor extends BaseStAXArtifactProcessor implements StAXAttributeProcessor { + private static final QName ATTRIBUTE = new QName("http://test", "customAttribute"); + + public QName getArtifactType() { + return ATTRIBUTE; + } + + public String read(QName attributeName, XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + return reader.getAttributeValue(attributeName.getNamespaceURI(), attributeName.getLocalPart()); + } + + public void write(String value, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + writer.setPrefix(ATTRIBUTE.getPrefix(), ATTRIBUTE.getNamespaceURI()); + writer.writeAttribute(ATTRIBUTE.getLocalPart(), value); + } + + public Class getModelType() { + return String.class; + } + + public void resolve(String arg0, ModelResolver arg1) throws ContributionResolveException { + + } +} diff --git a/branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite new file mode 100644 index 0000000000..3c69d0ed67 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorExtended.composite @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java new file mode 100644 index 0000000000..4cf20125b9 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Constants.java @@ -0,0 +1,30 @@ +/* + * 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.contribution; + +/** + * Constants used in SCA contribution. + * + * @version $Rev$ $Date$ + */ +public interface Constants { + String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0"; + String SCA10_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.0"; +} diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXArtifactProcessorExtensionPoint.java b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXArtifactProcessorExtensionPoint.java index bfd0be4541..bb9f07d946 100644 --- a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXArtifactProcessorExtensionPoint.java +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXArtifactProcessorExtensionPoint.java @@ -60,6 +60,7 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends private MonitorFactory monitorFactory; private boolean loaded; private StAXArtifactProcessor extensibleStAXProcessor; + private StAXAttributeProcessor extensibleStAXAttributeProcessor; private Monitor monitor = null; /** @@ -75,6 +76,9 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends if (monitorFactory != null) this.monitor = monitorFactory.createMonitor(); this.extensibleStAXProcessor = new ExtensibleStAXArtifactProcessor(this, inputFactory, outputFactory, this.monitor); + + StAXAttributeProcessorExtensionPoint attributeExtensionPoint = extensionPoints.getExtensionPoint(StAXAttributeProcessorExtensionPoint.class); + this.extensibleStAXAttributeProcessor = new ExtensibleStAXAttributeProcessor(attributeExtensionPoint ,inputFactory, outputFactory, this.monitor); } /** @@ -182,7 +186,7 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends StAXArtifactProcessor processor = new LazyStAXArtifactProcessor(artifactType, modelTypeName, factoryName, processorDeclaration, extensionPoints, modelFactories, - extensibleStAXProcessor, monitor); + extensibleStAXProcessor,extensibleStAXAttributeProcessor, monitor); addArtifactProcessor(processor); } @@ -203,6 +207,7 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends private StAXArtifactProcessor processor; private Class modelType; private StAXArtifactProcessor extensionProcessor; + private StAXAttributeProcessor extensionAttributeProcessor; private Monitor monitor; LazyStAXArtifactProcessor(QName artifactType, @@ -212,6 +217,7 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends ExtensionPointRegistry extensionPoints, ModelFactoryExtensionPoint modelFactories, StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor, Monitor monitor) { this.extensionPoints = extensionPoints; @@ -220,6 +226,7 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends this.factoryName = factoryName; this.processorDeclaration = processorDeclaration; this.extensionProcessor = extensionProcessor; + this.extensionAttributeProcessor = extensionAttributeProcessor; this.monitor = monitor; } @@ -279,53 +286,66 @@ public class DefaultStAXArtifactProcessorExtensionPoint extends // Load and instantiate the processor class try { - Class processorClass = - (Class)processorDeclaration.loadClass(); - try { - Constructor constructor = - processorClass.getConstructor(ModelFactoryExtensionPoint.class, Monitor.class); - processor = constructor.newInstance(modelFactories, monitor); - } catch (NoSuchMethodException e) { - try { - Constructor constructor = - processorClass.getConstructor(ExtensionPointRegistry.class, Monitor.class); - processor = constructor.newInstance(extensionPoints, monitor); - } catch (NoSuchMethodException e1) { - try { - Constructor constructor = - processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class, Monitor.class); - processor = constructor.newInstance(modelFactories, extensionProcessor, monitor); - } catch (NoSuchMethodException e2) { - try { - Constructor constructor = - processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class, Monitor.class); - processor = constructor.newInstance(extensionPoints, extensionProcessor, monitor); - } catch (NoSuchMethodException e3) { - try { - Constructor constructor = - processorClass.getConstructor(ModelFactoryExtensionPoint.class); - processor = constructor.newInstance(modelFactories); - } catch (NoSuchMethodException e4) { - try { - Constructor constructor = - processorClass.getConstructor(ExtensionPointRegistry.class); - processor = constructor.newInstance(extensionPoints); - } catch (NoSuchMethodException e4a) { - try { - Constructor constructor = - processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class); - processor = constructor.newInstance(modelFactories, extensionProcessor); - } catch (NoSuchMethodException e5) { - Constructor constructor = - processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class); - processor = constructor.newInstance(extensionPoints, extensionProcessor); - } - } - } - } - } - } - } + Class processorClass = + (Class)processorDeclaration.loadClass(); + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, Monitor.class); + processor = constructor.newInstance(modelFactories, monitor); + } catch (NoSuchMethodException e) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, Monitor.class); + processor = constructor.newInstance(extensionPoints, monitor); + } catch (NoSuchMethodException e1) { + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class, Monitor.class); + processor = constructor.newInstance(modelFactories, extensionProcessor, monitor); + } catch (NoSuchMethodException e2) { + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class, StAXAttributeProcessor.class, Monitor.class); + processor = constructor.newInstance(modelFactories, extensionProcessor, extensionAttributeProcessor, monitor); + } catch (NoSuchMethodException e2a) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class, Monitor.class); + processor = constructor.newInstance(extensionPoints, extensionProcessor, monitor); + } catch (NoSuchMethodException e3) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class, StAXAttributeProcessor.class, Monitor.class); + processor = constructor.newInstance(extensionPoints, extensionProcessor, extensionAttributeProcessor, monitor); + } catch (NoSuchMethodException e3a) { + + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class); + processor = constructor.newInstance(modelFactories); + } catch (NoSuchMethodException e4) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class); + processor = constructor.newInstance(extensionPoints); + } catch (NoSuchMethodException e4a) { + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class); + processor = constructor.newInstance(modelFactories, extensionProcessor); + } catch (NoSuchMethodException e5) { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class); + processor = constructor.newInstance(extensionPoints, extensionProcessor); + } + } + } + } + } + } + } + } + } } catch (Exception e) { IllegalStateException ie = new IllegalStateException(e); error("IllegalStateException", processor, ie); diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXAttributeProcessorExtensionPoint.java b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXAttributeProcessorExtensionPoint.java new file mode 100644 index 0000000000..d81e236b40 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultStAXAttributeProcessorExtensionPoint.java @@ -0,0 +1,327 @@ +/* + * 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.contribution.processor; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.util.Map; +import java.util.Set; + +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 javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * The default implementation of an extension point for StAX artifact processors. + * + * @version $Rev$ $Date$ + */ +public class DefaultStAXAttributeProcessorExtensionPoint extends + DefaultArtifactProcessorExtensionPoint implements StAXAttributeProcessorExtensionPoint { + + private ExtensionPointRegistry extensionPoints; + private ModelFactoryExtensionPoint modelFactories; + private StAXAttributeProcessor extensibleStAXAttributeProcessor; + private boolean loaded; + private Monitor monitor = null; + + /** + * Constructs a new extension point. + */ + public DefaultStAXAttributeProcessorExtensionPoint(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + this.modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + XMLInputFactory inputFactory = modelFactories.getFactory(XMLInputFactory.class); + XMLOutputFactory outputFactory = modelFactories.getFactory(XMLOutputFactory.class); + UtilityExtensionPoint utilities = this.extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); + if (monitorFactory != null) { + this.monitor = monitorFactory.createMonitor(); + } + this.extensibleStAXAttributeProcessor = new ExtensibleStAXAttributeProcessor(this, inputFactory, outputFactory, this.monitor); + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + public void addArtifactProcessor(StAXAttributeProcessor artifactProcessor) { + if (artifactProcessor.getArtifactType() != null) { + processorsByArtifactType.put((Object)artifactProcessor.getArtifactType(), artifactProcessor); + } + if (artifactProcessor.getModelType() != null) { + processorsByModelType.put(artifactProcessor.getModelType(), artifactProcessor); + } + } + + public void removeArtifactProcessor(StAXAttributeProcessor artifactProcessor) { + if (artifactProcessor.getArtifactType() != null) { + processorsByArtifactType.remove((Object)artifactProcessor.getArtifactType()); + } + if (artifactProcessor.getModelType() != null) { + processorsByModelType.remove(artifactProcessor.getModelType()); + } + } + + @Override + public StAXAttributeProcessor getProcessor(Class modelType) { + loadArtifactProcessors(); + return super.getProcessor(modelType); + } + + @Override + public StAXAttributeProcessor getProcessor(Object artifactType) { + loadArtifactProcessors(); + return super.getProcessor(artifactType); + } + + /** + * Returns a QName object from a QName expressed as {ns}name + * or ns#name. + * + * @param qname + * @return + */ + private static QName getQName(String qname) { + if (qname == null) { + return null; + } + qname = qname.trim(); + if (qname.startsWith("{")) { + int h = qname.indexOf('}'); + if (h != -1) { + return new QName(qname.substring(1, h), qname.substring(h + 1)); + } + } else { + int h = qname.indexOf('#'); + if (h != -1) { + return new QName(qname.substring(0, h), qname.substring(h + 1)); + } + } + throw new IllegalArgumentException("Invalid qname: "+qname); + } + + /** + * Lazily load artifact processors registered in the extension point. + */ + private void loadArtifactProcessors() { + if (loaded) + return; + + // Get the processor service declarations + Set processorDeclarations; + try { + processorDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(StAXAttributeProcessor.class); + } catch (IOException e) { + IllegalStateException ie = new IllegalStateException(e); + error("IllegalStateException", extensibleStAXAttributeProcessor, ie); + throw ie; + } + + for (ServiceDeclaration processorDeclaration : processorDeclarations) { + Map attributes = processorDeclaration.getAttributes(); + + // Load a StAX artifact processor + + // Get the model QName + QName artifactType = getQName(attributes.get("qname")); + + // Get the model class name + String modelTypeName = attributes.get("model"); + + // Get the model factory class name + String factoryName = attributes.get("factory"); + + // Create a processor wrapper and register it + StAXAttributeProcessor processor = + new LazyStAXAttributeProcessor(artifactType, modelTypeName, factoryName, + processorDeclaration, extensionPoints, modelFactories, + extensibleStAXAttributeProcessor, monitor); + addArtifactProcessor(processor); + } + + loaded = true; + } + + /** + * A wrapper around an Artifact processor class allowing lazy loading and + * initialization of artifact processors. + */ + private static class LazyStAXAttributeProcessor implements StAXAttributeProcessor { + + private ExtensionPointRegistry extensionPoints; + private QName artifactType; + private String modelTypeName; + private String factoryName; + private ServiceDeclaration processorDeclaration; + private StAXAttributeProcessor processor; + private Class modelType; + private StAXAttributeProcessor extensionProcessor; + private Monitor monitor; + + LazyStAXAttributeProcessor(QName artifactType, + String modelTypeName, + String factoryName, + ServiceDeclaration processorDeclaration, + ExtensionPointRegistry extensionPoints, + ModelFactoryExtensionPoint modelFactories, + StAXAttributeProcessor extensionProcessor, + Monitor monitor) { + + this.extensionPoints = extensionPoints; + this.artifactType = artifactType; + this.modelTypeName = modelTypeName; + this.factoryName = factoryName; + this.processorDeclaration = processorDeclaration; + this.extensionProcessor = extensionProcessor; + this.monitor = monitor; + } + + public QName getArtifactType() { + return artifactType; + } + + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + @SuppressWarnings("unchecked") + private StAXAttributeProcessor getProcessor() { + if (processor == null) { + ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + + // Load and instantiate the processor class + try { + Class processorClass = + (Class)processorDeclaration.loadClass(); + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, Monitor.class); + processor = constructor.newInstance(modelFactories, monitor); + } catch (NoSuchMethodException e) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, Monitor.class); + processor = constructor.newInstance(extensionPoints, monitor); + } catch (NoSuchMethodException e1) { + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class, Monitor.class); + processor = constructor.newInstance(modelFactories, extensionProcessor, monitor); + } catch (NoSuchMethodException e2) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class, Monitor.class); + processor = constructor.newInstance(extensionPoints, extensionProcessor, monitor); + } catch (NoSuchMethodException e3) { + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class); + processor = constructor.newInstance(modelFactories); + } catch (NoSuchMethodException e4) { + try { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class); + processor = constructor.newInstance(extensionPoints); + } catch (NoSuchMethodException e4a) { + try { + Constructor constructor = + processorClass.getConstructor(ModelFactoryExtensionPoint.class, StAXArtifactProcessor.class); + processor = constructor.newInstance(modelFactories, extensionProcessor); + } catch (NoSuchMethodException e5) { + Constructor constructor = + processorClass.getConstructor(ExtensionPointRegistry.class, StAXArtifactProcessor.class); + processor = constructor.newInstance(extensionPoints, extensionProcessor); + } + } + } + } + } + } + } + } catch (Exception e) { + IllegalStateException ie = new IllegalStateException(e); + error("IllegalStateException", processor, ie); + throw ie; + } + + } + return processor; + } + + public Object read(QName attributeName, XMLStreamReader inputSource) throws ContributionReadException, XMLStreamException { + return getProcessor().read(attributeName, inputSource); + } + + @SuppressWarnings("unchecked") + public void write(Object model, XMLStreamWriter outputSource) throws ContributionWriteException, XMLStreamException { + getProcessor().write(model, outputSource); + } + + public Class getModelType() { + if (modelTypeName != null && modelType == null) { + try { + modelType = processorDeclaration.loadClass(modelTypeName); + } catch (Exception e) { + IllegalStateException ie = new IllegalStateException(e); + error("IllegalStateException", processorDeclaration, ie); + throw ie; + } + } + return modelType; + } + + @SuppressWarnings("unchecked") + public void resolve(Object model, ModelResolver resolver) throws ContributionResolveException { + getProcessor().resolve(model, resolver); + } + + } +} diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java new file mode 100644 index 0000000000..3329f0bf1c --- /dev/null +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java @@ -0,0 +1,224 @@ +/* + * 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.contribution.processor; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.xml.namespace.QName; +import javax.xml.stream.Location; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +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.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.contribution.Constants; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * Implementation of an extensible StAX attribute processor. + * + * Takes a StAXAttributeProcessorExtensionPoint and delegates to the proper + * StAXAttributeProcessor by attribute QName + * + * @version $Rev$ $Date$ + */ +public class ExtensibleStAXAttributeProcessor + implements StAXAttributeProcessor { + + private static final Logger logger = Logger.getLogger(ExtensibleStAXAttributeProcessor.class.getName()); + + private static final QName UNKNOWN_ATTRIBUTE = new QName(Constants.SCA10_TUSCANY_NS, "unknown"); + + private XMLInputFactory inputFactory; + private XMLOutputFactory outputFactory; + private StAXAttributeProcessorExtensionPoint processors; + private Monitor monitor; + + /** + * Constructs a new ExtensibleStAXArtifactProcessor. + * @param processors + * @param inputFactory + * @param outputFactory + */ + public ExtensibleStAXAttributeProcessor(StAXAttributeProcessorExtensionPoint processors, + XMLInputFactory inputFactory, + XMLOutputFactory outputFactory, + Monitor monitor) { + super(); + this.processors = processors; + this.inputFactory = inputFactory; + this.outputFactory = outputFactory; + if (this.outputFactory != null) { + this.outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE); + } + this.monitor = monitor; + } + + /** + * Report a warning. + * + * @param problems + * @param message + * @param model + */ + private void warning(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + + public Object read(QName attributeName, XMLStreamReader source) throws ContributionReadException, XMLStreamException { + + // Delegate to the processor associated with the attribute QName + int event = source.getEventType(); + if (event == XMLStreamConstants.START_DOCUMENT) { + source.nextTag(); + } + + StAXAttributeProcessor processor = null; + + //lookup for registered attribute processors + processor = (StAXAttributeProcessor)processors.getProcessor(attributeName); + if (processor == null) { + Location location = source.getLocation(); + if (logger.isLoggable(Level.WARNING)) { + logger.warning("Attribute " + attributeName + " cannot be processed. (" + location + ")"); + } + warning("AttributeCannotBeProcessed", processors, attributeName, location); + } else { + return processor.read(attributeName, source); + } + + return processor == null ? null : processor.read(attributeName, source); + } + + @SuppressWarnings("unchecked") + public void write(Object model, XMLStreamWriter outputSource) throws ContributionWriteException, XMLStreamException { + + if(model == null) { + return; + } + + // Delegate to the processor associated with the model type + StAXAttributeProcessor processor = processors.getProcessor(model.getClass()); + if(processor == null) { + if (logger.isLoggable(Level.WARNING)) { + logger.warning("No StAX processor is configured to handle " + model.getClass()); + } + warning("NoStaxProcessor", processors, model.getClass()); + } else { + processor.write(model, outputSource); + return; + } + } + + + + @SuppressWarnings("unchecked") + public void resolve(Object model, ModelResolver resolver) throws ContributionResolveException { + + // Delegate to the processor associated with the model type + if (model != null) { + StAXAttributeProcessor processor = processors.getProcessor(model.getClass()); + if (processor != null) { + processor.resolve(model, resolver); + } + } + } + + /** + * Read a model from an InputStream. + * @param is The artifact InputStream + * @param type Model type + * @return The model + * @throws ContributionReadException + */ + public M read(InputStream is, Class type) throws ContributionReadException { + return null; + } + + /** + * Write a model to an OutputStream. + * @param model + * @param os + * @throws ContributionWriteException + */ + public void write(Object model, OutputStream os) throws ContributionWriteException { + try { + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os); + write(model, writer); + writer.flush(); + writer.close(); + } catch (XMLStreamException e) { + ContributionWriteException cw = new ContributionWriteException(e); + error("ContributionWriteException", outputFactory, cw); + throw cw; + } + } + + public QName getArtifactType() { + return null; + } + + public Class getModelType() { + return null; + } +} diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessor.java b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessor.java new file mode 100644 index 0000000000..e8edb743a4 --- /dev/null +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessor.java @@ -0,0 +1,63 @@ +/* + * 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.contribution.processor; + +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.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; + + +/** + * An artifact processor that can read attributes from a StAX XMLStreamReader. + * + * @version $Rev$ $Date$ + */ +public interface StAXAttributeProcessor extends ArtifactProcessor{ + + /** + * Reads a model from an XMLStreamReader. + * + * @param reader The XMLStreamReader + * @return A model representation of the input. + */ + M read(QName attributeName, XMLStreamReader reader) throws ContributionReadException, XMLStreamException; + + /** + * Writes a model to an XMLStreamWriter. + * + * @param model A model representing the source + * @param writer The XML stream writer + * @throws ContributionWriteException + * @throws XMLStreamException + */ + void write(M model, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException; + + /** + * Returns the type of artifact handled by this artifact processor. + * + * @return The type of artifact handled by this artifact processor + */ + QName getArtifactType(); + +} diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessorExtensionPoint.java b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessorExtensionPoint.java new file mode 100644 index 0000000000..ebad243c1c --- /dev/null +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/StAXAttributeProcessorExtensionPoint.java @@ -0,0 +1,29 @@ +/* + * 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.contribution.processor; + +/** + * An extension point for StAX artifact processors. + * + * @version $Rev$ $Date$ + */ +public interface StAXAttributeProcessorExtensionPoint extends + ArtifactProcessorExtensionPoint { + +} diff --git a/branches/sca-java-1.3.2/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint b/branches/sca-java-1.3.2/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint new file mode 100644 index 0000000000..607725bcfe --- /dev/null +++ b/branches/sca-java-1.3.2/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint @@ -0,0 +1,18 @@ +# 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. + +org.apache.tuscany.sca.contribution.processor.DefaultStAXAttributeProcessorExtensionPoint -- cgit v1.2.3