summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/samples/implementation-pojo-extension/src/main/java/pojo/impl/POJOImplementationProcessor.java
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-1.x/branches/sca-java-1.6.2/samples/implementation-pojo-extension/src/main/java/pojo/impl/POJOImplementationProcessor.java')
-rw-r--r--sca-java-1.x/branches/sca-java-1.6.2/samples/implementation-pojo-extension/src/main/java/pojo/impl/POJOImplementationProcessor.java173
1 files changed, 173 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-1.6.2/samples/implementation-pojo-extension/src/main/java/pojo/impl/POJOImplementationProcessor.java b/sca-java-1.x/branches/sca-java-1.6.2/samples/implementation-pojo-extension/src/main/java/pojo/impl/POJOImplementationProcessor.java
new file mode 100644
index 0000000000..36bce82e09
--- /dev/null
+++ b/sca-java-1.x/branches/sca-java-1.6.2/samples/implementation-pojo-extension/src/main/java/pojo/impl/POJOImplementationProcessor.java
@@ -0,0 +1,173 @@
+/*
+ * 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 pojo.impl;
+
+import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+
+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.Service;
+import org.apache.tuscany.sca.assembly.xml.PolicyAttachPointProcessor;
+import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ClassReference;
+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.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.policy.PolicyFactory;
+
+import pojo.POJOImplementation;
+import pojo.POJOImplementationFactory;
+
+/**
+ * Implements a STAX based artifact processor for POJO implementations.
+ *
+ * The artifact processor is responsible for processing <implementation.pojo>
+ * elements in SCA assembly XML composite files and populating the POJO
+ * implementation model, resolving its references to other artifacts in the SCA
+ * contribution, and optionally write the model back to SCA assembly XML.
+ */
+public class POJOImplementationProcessor implements StAXArtifactProcessor<POJOImplementation> {
+ private static final QName IMPLEMENTATION_POJO = new QName("http://pojo", "implementation.pojo");
+
+ private AssemblyFactory assemblyFactory;
+ private JavaInterfaceFactory javaFactory;
+ private POJOImplementationFactory pojoImplementationFactory;
+ private PolicyFactory policyFactory;
+ private PolicyAttachPointProcessor policyProcessor;
+
+ public POJOImplementationProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) {
+
+ // Get the assembly and Java interface factories as we'll need them to
+ // create model objects
+ assemblyFactory = modelFactories.getFactory(AssemblyFactory.class);
+ javaFactory = modelFactories.getFactory(JavaInterfaceFactory.class);
+ policyFactory = modelFactories.getFactory(PolicyFactory.class);
+ pojoImplementationFactory = modelFactories.getFactory(POJOImplementationFactory.class);
+ policyProcessor = new PolicyAttachPointProcessor(policyFactory);
+ }
+
+ public QName getArtifactType() {
+ // Returns the qname of the XML element processed by this processor
+ return IMPLEMENTATION_POJO;
+ }
+
+ public Class<POJOImplementation> getModelType() {
+ // Returns the type of model processed by this processor
+ return POJOImplementation.class;
+ }
+
+ public POJOImplementation read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+
+ // Read an <implementation.pojo> element
+ POJOImplementation implementation = pojoImplementationFactory.createPOJOImplementation();
+
+ // Read policies
+ policyProcessor.readPolicies(implementation, reader);
+
+ // Read the POJO class attribute.
+ String className = reader.getAttributeValue(null, "class");
+ implementation.setPOJOName(className);
+
+ // Mark the POJO model unresolved to track the fact that it's not
+ // completely initialized, its class is not loaded yet and services
+ // and references not initialized either
+ implementation.setUnresolved(true);
+
+ // Skip to end element
+ while (reader.hasNext()) {
+ if (reader.next() == END_ELEMENT && IMPLEMENTATION_POJO.equals(reader.getName())) {
+ break;
+ }
+ }
+
+ return implementation;
+ }
+
+ public void resolve(POJOImplementation implementation, ModelResolver resolver) throws ContributionResolveException {
+
+ // Resolve the POJO implementation
+
+ // First resolve its class
+ ClassReference classReference = new ClassReference(implementation.getPOJOName());
+ classReference = resolver.resolveModel(ClassReference.class, classReference);
+ Class<?> pojoClass = classReference.getJavaClass();
+ if (pojoClass == null) {
+ throw new ContributionResolveException("Class could not be resolved: " + implementation.getPOJOName());
+ }
+ implementation.setPOJOClass(pojoClass);
+
+ // Check to see if we have a .componentType file describing the POJO class
+ ComponentType componentType = assemblyFactory.createComponentType();
+ componentType.setUnresolved(true);
+ componentType.setURI(implementation.getURI() + ".componentType");
+ componentType = resolver.resolveModel(ComponentType.class, componentType);
+ if (!componentType.isUnresolved()) {
+
+ // We have a component type description, merge it into the POJO model
+ implementation.getServices().addAll(componentType.getServices());
+ implementation.getReferences().addAll(componentType.getReferences());
+ implementation.getProperties().addAll(componentType.getProperties());
+
+ } else {
+
+ // We have no component type description, simply introspect the POJO and
+ // create a single Service for it
+ Service service = assemblyFactory.createService();
+ service.setName(pojoClass.getSimpleName());
+ JavaInterface javaInterface;
+ try {
+ javaInterface = javaFactory.createJavaInterface(pojoClass);
+ } catch (InvalidInterfaceException e) {
+ throw new ContributionResolveException(e);
+ }
+ JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract();
+ interfaceContract.setInterface(javaInterface);
+ service.setInterfaceContract(interfaceContract);
+ implementation.getServices().add(service);
+ }
+
+ // Mark the implementation resolved now
+ implementation.setUnresolved(false);
+ }
+
+ public void write(POJOImplementation implementation, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {
+
+ // Write <implementation.pojo> element
+ writer.writeStartElement(IMPLEMENTATION_POJO.getNamespaceURI(), IMPLEMENTATION_POJO.getLocalPart());
+ policyProcessor.writePolicyAttributes(implementation, writer);
+
+ if (implementation.getPOJOName() != null) {
+ writer.writeAttribute("class", implementation.getPOJOName());
+ }
+
+ writer.writeEndElement();
+ }
+}