You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ad...@apache.org on 2008/05/03 22:52:58 UTC

svn commit: r653133 [11/33] - in /incubator/tuscany/sandbox/mobile-android: android-jdk-classes/ android-jdk-classes/src/ android-jdk-classes/src/javax/ android-jdk-classes/src/javax/xml/ android-jdk-classes/src/javax/xml/namespace/ android-jdk-classes...

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java Sat May  3 13:52:41 2008
@@ -0,0 +1,95 @@
+/*
+ * 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;
+
+/**
+ * Constants used in SCA assembly XML files.
+ */
+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";
+    
+    String COMPONENT_TYPE = "componentType";
+    QName COMPONENT_TYPE_QNAME = new QName(SCA10_NS, COMPONENT_TYPE);
+    
+    String SERVICE = "service";
+    QName SERVICE_QNAME = new QName(SCA10_NS, SERVICE);
+    
+    String REFERENCE = "reference";
+    QName REFERENCE_QNAME = new QName(SCA10_NS, REFERENCE);
+    
+    String PROPERTY = "property";
+    QName PROPERTY_QNAME = new QName(SCA10_NS, PROPERTY);
+    
+    String CONSTRAINING_TYPE = "constrainingType";
+    QName CONSTRAINING_TYPE_QNAME = new QName(SCA10_NS, CONSTRAINING_TYPE);
+    
+    String COMPOSITE = "composite";
+    QName COMPOSITE_QNAME = new QName(SCA10_NS, COMPOSITE);
+    
+    String INCLUDE = "include";
+    QName INCLUDE_QNAME = new QName(SCA10_NS, INCLUDE); 
+    
+    String COMPONENT = "component";
+    QName COMPONENT_QNAME = new QName(SCA10_NS, COMPONENT);
+    
+    String WIRE = "wire";
+    QName WIRE_QNAME = new QName(SCA10_NS, WIRE);
+
+    String OPERATION = "operation";
+    QName OPERATION_QNAME = new QName(SCA10_NS, OPERATION);
+    
+    String CALLBACK = "callback";
+    QName CALLBACK_QNAME = new QName(SCA10_NS, CALLBACK);
+
+    String IMPLEMENTATION_COMPOSITE = "implementation.composite";
+    QName IMPLEMENTATION_COMPOSITE_QNAME = new QName(SCA10_NS, IMPLEMENTATION_COMPOSITE);
+    
+    String IMPLEMENTATION = "implementation";
+    QName IMPLEMENTATION_QNAME = new QName(SCA10_NS, IMPLEMENTATION);
+    
+    String BINDING_SCA = "binding.sca";
+    QName BINDING_SCA_QNAME = new QName(Constants.SCA10_NS, BINDING_SCA);
+    
+    String NAME = "name";
+    String TARGET_NAMESPACE = "targetNamespace";
+    String LOCAL = "local";
+    String AUTOWIRE = "autowire";
+    String REQUIRES = "requires";
+    String POLICY_SETS = "policySets"; 
+    String APPLICABLE_POLICY_SETS = "applicablePolicySets";
+    String PROMOTE = "promote";
+    String TARGET = "target";
+    String WIRED_BY_IMPL = "wiredByImpl";
+    String MULTIPLICITY = "multiplicity";
+    String TYPE = "type";
+    String ELEMENT = "element";
+    String MANY = "many";
+    String MUST_SUPPLY = "mustSupply";
+    String SOURCE = "source";
+    String FILE = "file";
+    String URI = "uri";
+    String ZERO_ONE = "0..1";
+    String ZERO_N = "0..n";
+    String ONE_ONE = "1..1";
+    String ONE_N = "1..n";
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java Sat May  3 13:52:41 2008
@@ -0,0 +1,121 @@
+/*
+ * 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.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 javax.xml.validation.Schema;
+
+import org.apache.tuscany.sca.assembly.ConstrainingType;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.contribution.service.ContributionReadException;
+import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
+
+/**
+ * A contrainingType content handler.
+ * 
+ * @version $Rev: 635373 $ $Date: 2008-03-09 14:51:51 -0700 (Sun, 09 Mar 2008) $
+ */
+public class ConstrainingTypeDocumentProcessor extends BaseAssemblyProcessor implements URLArtifactProcessor<ConstrainingType> {
+    private XMLInputFactory inputFactory;
+    private Schema schema;
+
+    /**
+     * Construct a new constrainingType processor.
+     * @param factory
+     * @param policyFactory
+     * @param staxProcessor
+     */
+    public ConstrainingTypeDocumentProcessor(StAXArtifactProcessor staxProcessor, XMLInputFactory inputFactory) {
+        super(null, null, staxProcessor);
+        this.inputFactory = inputFactory;
+    }
+
+    public ConstrainingType read(URL contributionURL, URI uri, URL url) throws ContributionReadException {
+        InputStream urlStream = null;
+        try {
+            
+            // Create a stream reader
+            URLConnection connection = url.openConnection();
+            connection.setUseCaches(false);
+            urlStream = connection.getInputStream();
+            XMLStreamReader reader = inputFactory.createXMLStreamReader(url.toString(), urlStream);
+            reader.nextTag();
+            
+            // Read the constrainingType model 
+            ConstrainingType constrainingType = (ConstrainingType)extensionProcessor.read(reader);
+
+            // For debugging purposes, write it back to XML
+//            if (constrainingType != null) {
+//                try {
+//                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+//                    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+//                    outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
+//                    extensionProcessor.write(constrainingType, outputFactory.createXMLStreamWriter(bos));
+//                    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(bos.toByteArray()));
+//                    OutputFormat format = new OutputFormat();
+//                    format.setIndenting(true);
+//                    format.setIndent(2);
+//                    XMLSerializer serializer = new XMLSerializer(System.out, format);
+//                    serializer.serialize(document);
+//                } catch (Exception e) {
+//                    e.printStackTrace();
+//                }
+//            }
+            
+            return constrainingType;
+            
+        } catch (XMLStreamException e) {
+            throw new ContributionReadException(e);
+        } catch (IOException e) {
+            throw new ContributionReadException(e);
+        } finally {
+            try {
+                if (urlStream != null) {
+                    urlStream.close();
+                    urlStream = null;
+                }
+            } catch (IOException ioe) {
+                //ignore
+            }
+        }
+    }
+    
+    public void resolve(ConstrainingType constrainingType, ModelResolver resolver) throws ContributionResolveException {
+        extensionProcessor.resolve(constrainingType, resolver);
+    }
+    
+    public String getArtifactType() {
+        return ".constrainingType";
+    }
+    
+    public Class<ConstrainingType> getModelType() {
+        return ConstrainingType.class;
+    }
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeModelResolver.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeModelResolver.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeModelResolver.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeModelResolver.java Sat May  3 13:52:41 2008
@@ -0,0 +1,83 @@
+/*
+ * 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.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.sca.assembly.ConstrainingType;
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.Import;
+import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.namespace.NamespaceImport;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+
+/**
+ * A Model Resolver for ConstrainingType models.
+ *
+ * @version $Rev: 572482 $ $Date: 2007-09-03 18:28:31 -0700 (Mon, 03 Sep 2007) $
+ */
+public class ConstrainingTypeModelResolver implements ModelResolver {
+
+    private Contribution contribution;
+    private Map<QName, ConstrainingType> map = new HashMap<QName, ConstrainingType>();
+    
+    public ConstrainingTypeModelResolver(Contribution contribution, ModelFactoryExtensionPoint modelFactories) {
+        this.contribution = contribution;
+    }
+
+    public void addModel(Object resolved) {
+        ConstrainingType composite = (ConstrainingType)resolved;
+        map.put(composite.getName(), composite);
+    }
+    
+    public Object removeModel(Object resolved) {
+        return map.remove(((ConstrainingType)resolved).getName());
+    }
+    
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        
+        // Lookup a definition for the given namespace
+        QName qname = ((ConstrainingType)unresolved).getName();
+        ConstrainingType resolved = (ConstrainingType) map.get(qname);
+        if (resolved != null) {
+            return modelClass.cast(resolved);
+        }
+        
+        // No definition found, delegate the resolution to the imports
+        for (Import import_ : this.contribution.getImports()) {
+            if (import_ instanceof NamespaceImport) {
+                NamespaceImport namespaceImport = (NamespaceImport)import_;
+                if (namespaceImport.getNamespace().equals(qname.getNamespaceURI())) {
+                    
+                    // Delegate the resolution to the import resolver
+                    resolved = namespaceImport.getModelResolver().resolveModel(ConstrainingType.class, (ConstrainingType)unresolved);
+                    if (!resolved.isUnresolved()) {
+                        return modelClass.cast(resolved);
+                    }
+                }
+            }
+        }
+        return (T)unresolved;
+    }
+    
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java Sat May  3 13:52:41 2008
@@ -0,0 +1,257 @@
+/*
+ * 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 static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+import static javax.xml.stream.XMLStreamConstants.START_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.AbstractContract;
+import org.apache.tuscany.sca.assembly.AbstractProperty;
+import org.apache.tuscany.sca.assembly.AbstractReference;
+import org.apache.tuscany.sca.assembly.AbstractService;
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.ConstrainingType;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+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.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.impl.OperationImpl;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.w3c.dom.Document;
+
+/**
+ * A constrainingType processor.
+ * 
+ * @version $Rev: 637192 $ $Date: 2008-03-14 11:13:01 -0700 (Fri, 14 Mar 2008) $
+ */
+public class ConstrainingTypeProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor<ConstrainingType> {
+
+    /**
+     * Construct a new constrainingType processor.
+     * @param factory
+     * @param policyFactory
+     * @param extensionProcessor
+     */
+    public ConstrainingTypeProcessor(AssemblyFactory factory, PolicyFactory policyFactory, StAXArtifactProcessor extensionProcessor) {
+        super(factory, policyFactory, extensionProcessor);
+    }
+
+    public ConstrainingType read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+        ConstrainingType constrainingType = null;
+        AbstractService abstractService = null;
+        AbstractReference abstractReference = null;
+        AbstractProperty abstractProperty = null;
+        AbstractContract abstractContract = null;
+        QName name = null;
+        
+        // Read the constrainingType document
+        while (reader.hasNext()) {
+            int event = reader.getEventType();
+            switch (event) {
+
+                case START_ELEMENT:
+                    name = reader.getName();
+                    
+                    // Read a <constrainingType>
+                    if (Constants.CONSTRAINING_TYPE_QNAME.equals(name)) {
+                        constrainingType = assemblyFactory.createConstrainingType();
+                        constrainingType.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME)));
+                        policyProcessor.readPolicies(constrainingType, reader);
+
+                    } else if (Constants.SERVICE_QNAME.equals(name)) {
+                        
+                        // Read a <service>
+                        abstractService = assemblyFactory.createAbstractService();
+                        abstractContract = abstractService;
+                        abstractService.setName(getString(reader, Constants.NAME));
+                        constrainingType.getServices().add(abstractService);
+                        policyProcessor.readPolicies(abstractService, reader);
+
+                    } else if (Constants.REFERENCE_QNAME.equals(name)) {
+                        
+                        // Read a <reference>
+                        abstractReference = assemblyFactory.createAbstractReference();
+                        abstractContract = abstractReference;
+                        abstractReference.setName(getString(reader, Constants.NAME));
+                        readMultiplicity(abstractReference, reader);
+                        constrainingType.getReferences().add(abstractReference);
+                        policyProcessor.readPolicies(abstractReference, reader);
+
+                    } else if (Constants.PROPERTY_QNAME.equals(name)) {
+                        
+                        // Read a <property>
+                        abstractProperty = assemblyFactory.createAbstractProperty();
+                        readAbstractProperty(abstractProperty, reader);
+                        
+                        // Read the property value
+                        Document value = readPropertyValue(abstractProperty.getXSDElement(), abstractProperty.getXSDType(), reader);
+                        abstractProperty.setValue(value);
+                        
+                        constrainingType.getProperties().add(abstractProperty);
+                        policyProcessor.readPolicies(abstractProperty, reader);
+                        
+                    } else if (OPERATION.equals(name)) {
+
+                        // Read an <operation>
+                        Operation operation = new OperationImpl();
+                        operation.setName(getString(reader, NAME));
+                        operation.setUnresolved(true);
+                        policyProcessor.readPolicies(abstractContract, operation, reader);
+                        
+                    } else {
+
+                        // Read an extension element
+                        Object extension = extensionProcessor.read(reader);
+                        if (extension instanceof InterfaceContract) {
+                            
+                            // <service><interface> and <reference><interface>
+                            abstractContract.setInterfaceContract((InterfaceContract)extension);
+                        } else {
+
+                            // Add the extension element to the current element
+                            if (abstractContract != null) {
+                                abstractContract.getExtensions().add(extension);
+                            } else {
+                                constrainingType.getExtensions().add(extension);
+                            }
+                            
+                        }
+                    }
+                    break;
+
+                case END_ELEMENT:
+                    name = reader.getName();
+
+                    // Clear current state when reading reaching end element
+                    if (SERVICE_QNAME.equals(name)) {
+                        abstractService = null;
+                        abstractContract = null;
+                    } else if (REFERENCE_QNAME.equals(name)) {
+                        abstractReference = null;
+                        abstractContract = null;
+                    } else if (PROPERTY_QNAME.equals(name)) {
+                        abstractProperty = null;
+                    }
+                    break;
+            }
+            if (reader.hasNext()) {
+                reader.next();
+            }
+        }
+        return constrainingType;
+    }
+    
+    public void write(ConstrainingType constrainingType, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {
+
+        // Write <constrainingType> element
+        writeStartDocument(writer, CONSTRAINING_TYPE,
+           new XAttr(TARGET_NAMESPACE, constrainingType.getName().getNamespaceURI()),
+           new XAttr(NAME, constrainingType.getName().getLocalPart()),
+           policyProcessor.writePolicies(constrainingType));
+
+        // Write <service> elements 
+        for (AbstractService service : constrainingType.getServices()) {
+            writeStart(writer, SERVICE, new XAttr(NAME, service.getName()),
+                       policyProcessor.writePolicies(service));
+            
+            extensionProcessor.write(service.getInterfaceContract(), writer);
+
+            for (Object extension: service.getExtensions()) {
+                extensionProcessor.write(extension, writer);
+            }
+            
+            writeEnd(writer);
+        }
+
+        // Write <reference> elements
+        for (AbstractReference reference : constrainingType.getReferences()) {
+            writeStart(writer, REFERENCE, new XAttr(NAME, reference.getName()),
+                       policyProcessor.writePolicies(reference));
+            
+            extensionProcessor.write(reference.getInterfaceContract(), writer);
+
+            for (Object extension: reference.getExtensions()) {
+                extensionProcessor.write(extension, writer);
+            }
+            
+            writeEnd(writer);
+        }
+
+        // Write <property> elements
+        for (AbstractProperty abstractProperty : constrainingType.getProperties()) {
+            writeStart(writer,
+                       PROPERTY,
+                       new XAttr(NAME, abstractProperty.getName()),
+                       new XAttr(MUST_SUPPLY, abstractProperty.isMustSupply()),
+                       new XAttr(MANY, abstractProperty.isMany()),
+                       new XAttr(TYPE, abstractProperty.getXSDType()),
+                       new XAttr(ELEMENT, abstractProperty.getXSDElement()),
+                       policyProcessor.writePolicies(abstractProperty));
+
+            // Write property value
+            writePropertyValue(abstractProperty.getValue(), abstractProperty.getXSDElement(), abstractProperty.getXSDType(), writer);
+
+            // Write extensions
+            for (Object extension : abstractProperty.getExtensions()) {
+                extensionProcessor.write(extension, writer);
+            }
+
+            writeEnd(writer);
+        }
+
+        // Write extension elements
+        for (Object extension: constrainingType.getExtensions()) {
+            extensionProcessor.write(extension, writer);
+        }
+        
+        writeEndDocument(writer);
+    }
+    
+    public void resolve(ConstrainingType constrainingType, ModelResolver resolver) throws ContributionResolveException {
+        // Resolve component type services and references
+        resolveAbstractContracts(constrainingType.getServices(), resolver);
+        resolveAbstractContracts(constrainingType.getReferences(), resolver);
+        
+        resolveIntents(constrainingType.getRequiredIntents(), resolver);
+        for ( AbstractService service  : constrainingType.getServices() ) {
+            resolveIntents(service.getRequiredIntents(), resolver);
+        }
+        
+        for ( AbstractReference reference : constrainingType.getReferences() ) {
+            resolveIntents(reference.getRequiredIntents(), resolver);
+        }
+    }
+    
+    public QName getArtifactType() {
+        return CONSTRAINING_TYPE_QNAME;
+    }
+    
+    public Class<ConstrainingType> getModelType() {
+        return ConstrainingType.class;
+    }
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java Sat May  3 13:52:41 2008
@@ -0,0 +1,246 @@
+/*
+ * 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 static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.Base;
+import org.apache.tuscany.sca.assembly.ComponentType;
+import org.apache.tuscany.sca.assembly.Implementation;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+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.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+
+public class DefaultBeanModelProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor {
+
+    private QName artifactType;
+    private Class<Implementation> modelClass;
+    private Object modelFactory;
+    private Method factoryMethod;
+    private Map<String, Method> setterMethods = new HashMap<String, Method>();
+    private Map<String, Method> getterMethods = new HashMap<String, Method>();
+
+    public DefaultBeanModelProcessor(AssemblyFactory assemblyFactory,
+                                       PolicyFactory policyFactory,
+                                       QName artifactType,
+                                       Class<Implementation> modelClass,
+                                       Object modelFactory) {
+        super(assemblyFactory, policyFactory, null);
+        this.artifactType = artifactType;
+        this.modelClass = modelClass;
+        this.modelFactory = modelFactory;
+        
+        // Introspect the factory class and bean model class
+        if (modelFactory != null) {
+            
+            // Find the model create method
+            for (Method method: modelFactory.getClass().getMethods()) {
+                if (method.getName().startsWith("create") && method.getReturnType() == modelClass) {
+                    factoryMethod = method;
+                    break;
+                }
+            }
+        }
+        
+        // Index the bean's setter methods
+        for (Method method: modelClass.getMethods()) {
+            Method getter;
+            String name = method.getName();
+            if (name.startsWith("set") && name.length() > 3) {
+                
+                // Get the corresponding getter method
+                try {
+                    getter = modelClass.getMethod("get" + name.substring(3));
+                } catch (Exception e) {
+                    getter = null;
+                    continue;
+                }
+                
+                // Get the property name
+                name = name.substring(3);
+                if (name.length() > 1) {
+                    if (!name.toUpperCase().equals(name)) {
+                        name = name.substring(0, 1).toLowerCase() + name.substring(1);
+                    }
+                }
+            } else {
+                continue;
+            }
+            
+            // Map an uppercase property name to a lowercase attribute name 
+            if (name.toUpperCase().equals(name)) {
+                name = name.toLowerCase();
+            }
+            
+            // Trim trailing _ from property names
+            if (name.endsWith("_")) {
+                name = name.substring(0, name.length()-1);
+            }
+            setterMethods.put(name, method);
+            getterMethods.put(name, getter);
+        }
+    }
+
+    public Object read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+
+        // Read an element
+        try {
+            
+            // Create a new instance of the model
+            Object model;
+            if (modelFactory != null) {
+                // Invoke the factory create method
+                model = factoryMethod.invoke(modelFactory);
+            } else {
+                // Invoke the model bean class default constructor
+                model = modelClass.newInstance();
+            }
+
+            // Initialize the bean properties with the attributes found in the
+            // XML element
+            for (int i = 0, n = reader.getAttributeCount(); i < n; i++) {
+                String attributeName = reader.getAttributeLocalName(i);
+                Method setter = setterMethods.get(attributeName);
+                if (setter != null) {
+                    String value = reader.getAttributeValue(i);
+                    setter.invoke(model, value);
+                }
+            }
+
+            // Read policies
+            policyProcessor.readPolicies(model, reader);
+
+            // FIXME read extension elements
+            
+            // By default mark the model object unresolved
+            if (model instanceof Base) {
+                ((Base)model).setUnresolved(true);
+            }
+            
+            // Skip to end element
+            while (reader.hasNext()) {
+                if (reader.next() == END_ELEMENT && artifactType.equals(reader.getName())) {
+                    break;
+                }
+            }
+            return model;
+
+        } catch (Exception e) {
+            throw new ContributionReadException(e);
+        }
+    }
+
+    public void write(Object bean, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {
+        try {
+            // Write the bean properties as attributes
+            List<XAttr> attrs = new ArrayList<XAttr>();
+            for (Map.Entry<String, Method> entry: getterMethods.entrySet()) {
+                if (entry.getValue().getReturnType() == String.class) {
+                    String value = (String)entry.getValue().invoke(bean);
+                    attrs.add(new XAttr(entry.getKey(), value));
+                }
+            }
+            
+            // Write element
+            writeStart(writer, artifactType.getNamespaceURI(), artifactType.getLocalPart(),
+                       policyProcessor.writePolicies(bean), new XAttr(null, attrs));
+
+            writeEnd(writer);
+
+        } catch (Exception e) {
+            throw new ContributionWriteException(e);
+        }
+    }
+
+    public void resolve(Object bean, ModelResolver resolver) throws ContributionResolveException {
+        
+        // Resolve and merge the component type associated with an
+        // implementation model
+        if (bean instanceof Implementation) {
+            Implementation implementation = (Implementation)bean;
+            String uri = implementation.getURI();
+            if (uri != null) {
+                int d = uri.lastIndexOf('.');
+                if (d != -1) {
+                    uri = uri.substring(0, d) + ".componentType";
+                    
+                    // Resolve the component type
+                    ComponentType componentType = assemblyFactory.createComponentType();
+                    componentType.setURI(uri);
+                    componentType.setUnresolved(true);
+                    
+                    componentType = resolver.resolveModel(ComponentType.class, componentType);
+                    if (componentType != null && !componentType.isUnresolved()) {
+                        
+                        // We found a component type, merge it into the implementation model
+                        implementation.getServices().addAll(componentType.getServices());
+                        implementation.getReferences().addAll(componentType.getReferences());
+                        implementation.getProperties().addAll(componentType.getProperties());
+                        implementation.setConstrainingType(componentType.getConstrainingType());
+                        
+                        if (implementation instanceof PolicySetAttachPoint &&
+                            componentType instanceof PolicySetAttachPoint )
+                        {
+                            PolicySetAttachPoint policiedImpl = (PolicySetAttachPoint)implementation;
+                            PolicySetAttachPoint policiedCompType = (PolicySetAttachPoint)componentType;
+                            
+                            if ( policiedImpl.getPolicySets() != null) {
+                                policiedImpl.getPolicySets().addAll(policiedCompType.getPolicySets());
+                            }
+                            if (policiedImpl.getRequiredIntents() != null) {
+                                policiedImpl.getRequiredIntents().addAll(policiedCompType.getRequiredIntents());
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        
+        // Mark the model resolved
+        if (bean instanceof Base) {
+            ((Base)bean).setUnresolved(false);
+        }
+    }
+
+    public QName getArtifactType() {
+        return artifactType;
+    }
+
+    public Class<?> getModelType() {
+        return modelClass;
+    }
+
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicyAttachPointProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicyAttachPointProcessor.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicyAttachPointProcessor.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicyAttachPointProcessor.java Sat May  3 13:52:41 2008
@@ -0,0 +1,263 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+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.Binding;
+import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.policy.Intent;
+import org.apache.tuscany.sca.policy.IntentAttachPoint;
+import org.apache.tuscany.sca.policy.IntentAttachPointType;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+
+public class PolicyAttachPointProcessor extends BaseStAXArtifactProcessor implements Constants {
+    
+    private PolicyFactory policyFactory;
+    
+    public PolicyAttachPointProcessor(PolicyFactory policyFactory) {
+        this.policyFactory = policyFactory;
+    }
+
+    /**
+     * Read policy intents associated with an operation.
+     * @param attachPoint
+     * @param operation
+     * @param reader
+     */
+    private void readIntents(Object attachPoint, Operation operation, XMLStreamReader reader) {
+        if (!(attachPoint instanceof IntentAttachPoint))
+            return;
+        IntentAttachPoint intentAttachPoint = (IntentAttachPoint)attachPoint;
+        String value = reader.getAttributeValue(null, REQUIRES);
+        if (value != null) {
+            List<Intent> requiredIntents = intentAttachPoint.getRequiredIntents();
+            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
+                QName qname = getQNameValue(reader, tokens.nextToken());
+                Intent intent = policyFactory.createIntent();
+                intent.setName(qname);
+                if (operation != null) {
+                    //FIXME Don't we need to handle intent specification
+                    // on an operation basis?
+                    //intent.getOperations().add(operation);
+                }
+                requiredIntents.add(intent);
+            }
+        }
+    }
+
+    /**
+     * Reads policy intents and policy sets associated with an operation.
+     * @param attachPoint
+     * @param operation
+     * @param reader
+     */
+    public void readPolicies(Object attachPoint, Operation operation, XMLStreamReader reader) {
+        readIntents(attachPoint, operation, reader);
+        readPolicySets(attachPoint, operation, reader);
+    }
+
+    /**
+     * Reads policy intents and policy sets.
+     * @param attachPoint
+     * @param reader
+     */
+    public void readPolicies(Object attachPoint, XMLStreamReader reader) {
+        readPolicies(attachPoint, null, reader);
+    }
+
+    /**
+     * Reads policy sets associated with an operation.
+     * @param attachPoint
+     * @param operation
+     * @param reader
+     */
+    private void readPolicySets(Object attachPoint, Operation operation, XMLStreamReader reader) {
+        if (!(attachPoint instanceof PolicySetAttachPoint)) {
+            return;
+        }
+        PolicySetAttachPoint policySetAttachPoint = (PolicySetAttachPoint)attachPoint;
+        String value = reader.getAttributeValue(null, POLICY_SETS);
+        if (value != null) {
+            List<PolicySet> policySets = policySetAttachPoint.getPolicySets();
+            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
+                QName qname = getQNameValue(reader, tokens.nextToken());
+                PolicySet policySet = policyFactory.createPolicySet();
+                policySet.setName(qname);
+                if (operation != null) {
+                    //FIXME Don't we need to handle policySet specification
+                    // on an operation basis?
+                    //policySet.getOperations().add(operation);
+                }
+                policySets.add(policySet);
+            }
+        }
+        
+        value = reader.getAttributeValue(SCA10_TUSCANY_NS, APPLICABLE_POLICY_SETS);
+        if (value != null) {
+            List<PolicySet> applicablePolicySets = policySetAttachPoint.getApplicablePolicySets();
+            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
+                QName qname = getQNameValue(reader, tokens.nextToken());
+                PolicySet policySet = policyFactory.createPolicySet();
+                policySet.setName(qname);
+                if (operation != null) {
+                    //FIXME Don't we need to handle policySet specification
+                    // on an operation basis?
+                    //policySet.getOperations().add(operation);
+                }
+                applicablePolicySets.add(policySet);
+            }
+        }
+    }
+    
+    /**
+     * Write policies
+     * @param attachPoint
+     * @return
+     */
+    XAttr writePolicies(Object attachPoint) throws XMLStreamException {
+        return writePolicies(attachPoint, (Operation)null);
+    }
+
+    /**
+     * Write policies
+     * @param attachPoint
+     * @return
+     */
+    public void writePolicyAttributes(Object attachPoint, XMLStreamWriter writer) throws XMLStreamException {
+        writePolicyAttributes(attachPoint, (Operation)null, writer);
+    }
+
+    /**
+     * Write policies
+     * @param attachPoint
+     * @return
+     */
+    public void writePolicyPrefixes(Object attachPoint, XMLStreamWriter writer) throws XMLStreamException {
+        writePolicyPrefixes(attachPoint, (Operation)null, writer);
+    }
+
+    /**
+     * Write policies associated with an operation
+     * @param attachPoint
+     * @param operation
+     * @return
+     */
+    XAttr writePolicies(Object attachPoint, Operation operation) {
+        List<XAttr> attrs =new ArrayList<XAttr>();
+        attrs.add(writeIntents(attachPoint, operation));
+        attrs.add(writePolicySets(attachPoint, operation));
+        return new XAttr(null, attrs);
+    }
+
+    /**
+     * Write policies
+     * @param attachPoint
+     * @return
+     */
+    public void writePolicyAttributes(Object attachPoint, Operation operation, XMLStreamWriter writer) throws XMLStreamException {
+        XAttr attr = writePolicies(attachPoint, operation);
+        attr.write(writer);
+    }
+
+    /**
+     * Write policies
+     * @param attachPoint
+     * @return
+     */
+    public void writePolicyPrefixes(Object attachPoint, Operation operation, XMLStreamWriter writer) throws XMLStreamException {
+        XAttr attr = writePolicies(attachPoint, operation);
+        attr.writePrefix(writer);
+    }
+
+    /**
+     * Write policy intents associated with an operation.
+     * @param attachPoint
+     * @param operation
+     */
+    private XAttr writeIntents(Object attachPoint, Operation operation) {
+        if (!(attachPoint instanceof IntentAttachPoint)) {
+            return null;
+        }
+        IntentAttachPoint intentAttachPoint = (IntentAttachPoint)attachPoint;
+        List<QName> qnames = new ArrayList<QName>();
+        for (Intent intent: intentAttachPoint.getRequiredIntents()) {
+            qnames.add(intent.getName());
+        }
+        return new XAttr(Constants.REQUIRES, qnames);
+    }
+
+    /**
+     * Write policy sets associated with an operation.
+     * @param attachPoint
+     * @param operation
+     */
+    private XAttr writePolicySets(Object attachPoint, Operation operation) {
+        if (!(attachPoint instanceof PolicySetAttachPoint)) {
+            return null;
+        }
+        PolicySetAttachPoint policySetAttachPoint = (PolicySetAttachPoint)attachPoint;
+        List<QName> qnames = new ArrayList<QName>();
+        for (PolicySet policySet: policySetAttachPoint.getPolicySets()) {
+            qnames.add(policySet.getName());
+        }
+        return new XAttr(Constants.POLICY_SETS, qnames);
+    }
+    
+    public void resolvePolicies(Object attachPoint, ModelResolver resolver) {
+        if ( attachPoint instanceof PolicySetAttachPoint ) {
+            PolicySetAttachPoint policySetAttachPoint = (PolicySetAttachPoint)attachPoint;
+            
+            List<Intent> requiredIntents = new ArrayList<Intent>();
+            Intent resolvedIntent = null;
+            
+            if ( policySetAttachPoint.getRequiredIntents() != null && policySetAttachPoint.getRequiredIntents().size() > 0 ) {
+                for ( Intent intent : policySetAttachPoint.getRequiredIntents() ) {
+                    resolvedIntent = resolver.resolveModel(Intent.class, intent);
+                    requiredIntents.add(resolvedIntent);
+                }
+                policySetAttachPoint.getRequiredIntents().clear();
+                policySetAttachPoint.getRequiredIntents().addAll(requiredIntents);
+            }
+            
+            if ( policySetAttachPoint.getPolicySets() != null && policySetAttachPoint.getPolicySets().size() > 0 ) {
+                List<PolicySet> resolvedPolicySets = new ArrayList<PolicySet>();
+                PolicySet resolvedPolicySet = null;
+                for ( PolicySet policySet : policySetAttachPoint.getPolicySets() ) {
+                    resolvedPolicySet = resolver.resolveModel(PolicySet.class, policySet);
+                    resolvedPolicySets.add(resolvedPolicySet);
+                }
+                policySetAttachPoint.getPolicySets().clear();
+                policySetAttachPoint.getPolicySets().addAll(resolvedPolicySets);
+            }
+        }
+    }
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver Sat May  3 13:52:41 2008
@@ -0,0 +1,20 @@
+# 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.assembly.xml.CompositeModelResolver;model=org.apache.tuscany.sca.assembly.Composite
+org.apache.tuscany.sca.assembly.xml.ConstrainingTypeModelResolver;model=org.apache.tuscany.sca.assembly.ConstrainingType
+org.apache.tuscany.sca.assembly.xml.ComponentTypeModelResolver;model=org.apache.tuscany.sca.assembly.ComponentType

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/BuildPolicyTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/BuildPolicyTestCase.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/BuildPolicyTestCase.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/BuildPolicyTestCase.java Sat May  3 13:52:41 2008
@@ -0,0 +1,166 @@
+/*
+ * 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.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.ConstrainingType;
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+import org.apache.tuscany.sca.assembly.OperationsConfigurator;
+import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
+import org.apache.tuscany.sca.assembly.builder.impl.CompositeBuilderImpl;
+import org.apache.tuscany.sca.contribution.DefaultContributionFactory;
+import org.apache.tuscany.sca.contribution.DefaultModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.DefaultURLArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.definitions.SCADefinitions;
+import org.apache.tuscany.sca.definitions.xml.SCADefinitionsDocumentProcessor;
+import org.apache.tuscany.sca.definitions.xml.SCADefinitionsProcessor;
+import org.apache.tuscany.sca.interfacedef.impl.InterfaceContractMapperImpl;
+import org.apache.tuscany.sca.policy.DefaultIntentAttachPointTypeFactory;
+import org.apache.tuscany.sca.policy.DefaultPolicyFactory;
+import org.apache.tuscany.sca.policy.IntentAttachPoint;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.xml.PolicySetProcessor;
+import org.apache.tuscany.sca.policy.xml.ProfileIntentProcessor;
+import org.apache.tuscany.sca.policy.xml.QualifiedIntentProcessor;
+import org.apache.tuscany.sca.policy.xml.SimpleIntentProcessor;
+
+/**
+ * Test reading SCA XML assembly documents.
+ * 
+ * @version $Rev: 561254 $ $Date: 2007-07-31 13:16:27 +0530 (Tue, 31 Jul 2007) $
+ */
+public class BuildPolicyTestCase extends TestCase { 
+    private ExtensibleURLArtifactProcessor documentProcessor;
+    private TestModelResolver resolver; 
+    private SCADefinitionsDocumentProcessor scaDefnDocProcessor;
+    private CompositeBuilder compositeBuilder;
+    private Composite composite;
+
+    @Override
+    public void setUp() throws Exception {
+        List scaDefnSink = new ArrayList();
+        AssemblyFactory factory = new DefaultAssemblyFactory();
+        PolicyFactory policyFactory = new DefaultPolicyFactory();
+        resolver = new TestModelResolver();
+        compositeBuilder = new CompositeBuilderImpl(factory, new TestSCABindingFactoryImpl(), new DefaultIntentAttachPointTypeFactory(), new InterfaceContractMapperImpl(), null);
+        URLArtifactProcessorExtensionPoint documentProcessors = new DefaultURLArtifactProcessorExtensionPoint(new DefaultModelFactoryExtensionPoint());
+        documentProcessor = new ExtensibleURLArtifactProcessor(documentProcessors); 
+        
+        // Create StAX processors 
+        DefaultStAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint(new DefaultModelFactoryExtensionPoint());
+        ExtensibleStAXArtifactProcessor staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance());
+        staxProcessors.addArtifactProcessor(new CompositeProcessor(new DefaultContributionFactory(), factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ComponentTypeProcessor(factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ConstrainingTypeProcessor(factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new SCADefinitionsProcessor(policyFactory, staxProcessor, resolver));
+        staxProcessors.addArtifactProcessor(new SimpleIntentProcessor(policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ProfileIntentProcessor(policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new QualifiedIntentProcessor(policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new PolicySetProcessor(policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new MockPolicyProcessor());
+        
+        XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 
+        
+        // Create document processors
+        documentProcessors.addArtifactProcessor(new CompositeDocumentProcessor(staxProcessor, inputFactory, scaDefnSink));
+        documentProcessors.addArtifactProcessor(new ComponentTypeDocumentProcessor(staxProcessor, inputFactory));
+        documentProcessors.addArtifactProcessor(new ConstrainingTypeDocumentProcessor(staxProcessor, inputFactory));
+        scaDefnDocProcessor = new SCADefinitionsDocumentProcessor(staxProcessors, staxProcessor, inputFactory, policyFactory);
+        documentProcessors.addArtifactProcessor(scaDefnDocProcessor);
+        
+        URL url = getClass().getResource("CalculatorComponent.constrainingType");
+        URI uri = URI.create("CalculatorComponent.constrainingType");
+        ConstrainingType constrainingType = (ConstrainingType)documentProcessor.read(null, uri, url);
+        assertNotNull(constrainingType);
+        resolver.addModel(constrainingType);
+
+        url = getClass().getResource("TestAllPolicyCalculator.composite");
+        uri = URI.create("TestAllCalculator.constrainingType");
+        composite = (Composite)documentProcessor.read(null, uri, url);
+        assertNotNull(composite);
+        
+        url = getClass().getResource("another_test_definitions.xml");
+        uri = URI.create("another_test_definitions.xml");
+        SCADefinitions scaDefns = (SCADefinitions)scaDefnDocProcessor.read(null, uri, url);
+        assertNotNull(scaDefns);
+        scaDefnSink.add(scaDefns);
+        
+        //preResolvePolicyTests(composite);
+        documentProcessor.resolve(scaDefns, resolver);
+        documentProcessor.resolve(composite, resolver);
+        //postResolvePolicyTests(composite);
+        
+        compositeBuilder.build(composite);
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        documentProcessor = null;
+        resolver = null;
+    }
+
+    public void testPolicyIntentInheritance() throws Exception {
+        String namespaceUri = "http://test";
+        
+        IntentAttachPoint policiedComposite = (IntentAttachPoint)composite;
+        assertEquals(policiedComposite.getRequiredIntents().size(), 1);
+        assertEquals(policiedComposite.getRequiredIntents().get(0).getName(), new QName(namespaceUri, "tuscanyIntent_1"));
+        
+        //1 defined for composite, 2 defined for the service, 1 defined and 3 inherited for the promoted service (4)
+        assertEquals(composite.getServices().get(0).getRequiredIntents().size(), 7);
+        //1 from the operation defined in this service and 2 from the operation defined in the promoted service 
+        assertEquals(composite.getServices().get(0).getConfiguredOperations().get(0).getRequiredIntents().size(), 5);
+        assertEquals(composite.getServices().get(0).getRequiredIntents().get(3).getName(), new QName(namespaceUri, "tuscanyIntent_3"));
+        //bindings will have only 2 intents since duplications will be cut out
+        assertEquals(((IntentAttachPoint)composite.getServices().get(0).getBindings().get(0)).getRequiredIntents().size(), 3);
+        assertEquals(((OperationsConfigurator)composite.getServices().get(0).getBindings().get(0)).getConfiguredOperations().size(), 1);
+        assertEquals(((OperationsConfigurator)composite.getServices().get(0).getBindings().get(0)).getConfiguredOperations().get(0).getRequiredIntents().size(), 5);
+        
+        assertEquals(composite.getReferences().get(0).getRequiredIntents().size(), 5);
+        assertEquals(composite.getReferences().get(0).getConfiguredOperations().size(), 1);
+        assertEquals(composite.getReferences().get(0).getConfiguredOperations().get(0).getRequiredIntents().size(), 4);
+        assertEquals(composite.getReferences().get(0).getRequiredIntents().get(1).getName(), new QName(namespaceUri, "tuscanyIntent_1"));
+        assertEquals(((IntentAttachPoint)composite.getReferences().get(0).getBindings().get(0)).getRequiredIntents().size(), 3);
+
+        assertEquals(composite.getComponents().get(0).getRequiredIntents().size(), 3);
+        assertEquals(composite.getComponents().get(0).getRequiredIntents().get(2).getName(), new QName(namespaceUri, "tuscanyIntent_1"));
+        assertEquals(composite.getComponents().get(0).getServices().get(0).getRequiredIntents().size(), 4);
+        assertEquals(composite.getComponents().get(0).getServices().get(0).getCallback().getRequiredIntents().size(), 4);
+        assertEquals(composite.getComponents().get(0).getServices().get(0).getConfiguredOperations().get(0).getRequiredIntents().size(), 5);
+        assertEquals(composite.getComponents().get(0).getReferences().get(0).getRequiredIntents().size(), 5);
+        assertEquals(composite.getComponents().get(0).getReferences().get(0).getConfiguredOperations().get(0).getRequiredIntents().size(), 5);
+    }
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MockPolicyProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MockPolicyProcessor.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MockPolicyProcessor.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MockPolicyProcessor.java Sat May  3 13:52:41 2008
@@ -0,0 +1,68 @@
+/*
+ * 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.StAXArtifactProcessor;
+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.policy.Policy;
+
+public class MockPolicyProcessor implements StAXArtifactProcessor<Policy> {
+
+    public QName getArtifactType() {
+        return new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyAttachment");
+    }
+
+    public Policy read(XMLStreamReader arg0) throws ContributionReadException, XMLStreamException {
+        return new MockPolicyImplOne();
+    }
+
+    public void write(Policy arg0, XMLStreamWriter arg1) throws ContributionWriteException,
+                                                        XMLStreamException {
+    } 
+
+    public Class<Policy> getModelType() {
+        return Policy.class;
+    }
+
+    public void resolve(Policy arg0, ModelResolver arg1) throws ContributionResolveException {
+    }
+
+    
+    public class MockPolicyImplOne implements Policy {
+        public QName getSchemaName() {
+            return new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyAttachment");
+        }
+
+        public boolean isUnresolved() {
+            return false;
+        }
+
+        public void setUnresolved(boolean unresolved) {
+        }
+        
+    }
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadAllTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadAllTestCase.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadAllTestCase.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadAllTestCase.java Sat May  3 13:52:41 2008
@@ -0,0 +1,162 @@
+/*
+ * 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.InputStream;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Callback;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.CompositeReference;
+import org.apache.tuscany.sca.assembly.CompositeService;
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+import org.apache.tuscany.sca.assembly.Property;
+import org.apache.tuscany.sca.contribution.ContributionFactory;
+import org.apache.tuscany.sca.contribution.DefaultContributionFactory;
+import org.apache.tuscany.sca.contribution.DefaultModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.policy.DefaultPolicyFactory;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Test reading SCA XML assemblies.
+ * 
+ * @version $Rev: 638855 $ $Date: 2008-03-19 08:04:32 -0700 (Wed, 19 Mar 2008) $
+ */
+public class ReadAllTestCase extends TestCase {
+    private ExtensibleStAXArtifactProcessor staxProcessor;
+
+    @Override
+    public void setUp() throws Exception {
+        AssemblyFactory factory = new DefaultAssemblyFactory();
+        PolicyFactory policyFactory = new DefaultPolicyFactory();
+        ContributionFactory  contributionFactory = new DefaultContributionFactory();
+        
+        StAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint(new DefaultModelFactoryExtensionPoint());
+        staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance());
+        
+        staxProcessors.addArtifactProcessor(new CompositeProcessor(contributionFactory, factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ComponentTypeProcessor(factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ConstrainingTypeProcessor(factory, policyFactory, staxProcessor));
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+    }
+
+    public void testReadComposite() throws Exception {
+        InputStream is = getClass().getResourceAsStream("TestAllCalculator.composite");
+        Composite composite = (Composite)staxProcessor.read(is, Composite.class);
+        assertNotNull(composite);
+        assertEquals(composite.getName(), new QName("http://calc", "TestAllCalculator"));
+        assertEquals(composite.getConstrainingType().getName(), new QName("http://calc", "CalculatorComponent"));
+        assertTrue(composite.isLocal());
+        assertFalse(composite.getAutowire() == Boolean.TRUE);
+        assertEquals(((PolicySetAttachPoint)composite).getRequiredIntents().get(0).getName(), new QName("http://test",
+                                                                                "confidentiality"));
+        assertEquals(((PolicySetAttachPoint)composite).getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+
+        Composite include = composite.getIncludes().get(0);
+        assertEquals(include.getName(), new QName("http://calc", "TestAllDivide"));
+
+        CompositeService calcCompositeService = (CompositeService)composite.getServices().get(0);
+        assertEquals(calcCompositeService.getName(), "CalculatorService");
+        assertTrue(calcCompositeService.getPromotedService().isUnresolved());
+        assertEquals(calcCompositeService.getPromotedService().getName(),
+                     "CalculatorService");
+        assertEquals(calcCompositeService.getRequiredIntents().get(0).getName(),
+                     new QName("http://test", "confidentiality"));
+        assertEquals(calcCompositeService.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+        // TODO test operations
+        Callback calcServiceCallback = calcCompositeService.getCallback();
+        assertNotNull(calcServiceCallback);
+        assertEquals(calcServiceCallback.getRequiredIntents().get(0).getName(),
+                     new QName("http://test", "confidentiality"));
+        assertEquals(calcServiceCallback.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+        // TODO test operations
+
+        Component calcComponent = composite.getComponents().get(0);
+        assertEquals(calcComponent.getName(), "CalculatorServiceComponent");
+        assertEquals(calcComponent.getAutowire(), Boolean.FALSE);
+        assertEquals(calcComponent.getConstrainingType().getName(), new QName("http://calc",
+                                                                              "CalculatorComponent"));
+        assertEquals(calcComponent.getRequiredIntents().get(0).getName(), new QName("http://test",
+                                                                                    "confidentiality"));
+        assertEquals(calcComponent.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+
+        ComponentService calcComponentService = calcComponent.getServices().get(0);
+        assertEquals(calcComponentService.getName(), "CalculatorService");
+        assertEquals(calcComponentService.getRequiredIntents().get(0).getName(),
+                     new QName("http://test", "confidentiality"));
+        assertEquals(calcComponentService.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+        // TODO test operations
+
+        ComponentReference calcComponentReference = calcComponent.getReferences().get(0);
+        assertEquals(calcComponentReference.getName(), "addService");
+        assertEquals(calcComponentReference.getAutowire(), Boolean.FALSE);
+        assertEquals(calcComponentReference.isWiredByImpl(), false);
+        assertEquals(calcComponentReference.getRequiredIntents().get(0).getName(),
+                     new QName("http://test", "confidentiality"));
+        assertEquals(calcComponentReference.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+        // TODO test operations
+
+        Property property = calcComponent.getProperties().get(0);
+        assertEquals(property.getName(), "round");
+        Document doc = (Document) property.getValue();
+        Element element = doc.getDocumentElement();
+        String value = element.getTextContent();
+        assertEquals(value, "true");
+        assertEquals(property.getXSDType(), new QName("http://www.w3.org/2001/XMLSchema", "boolean"));
+        assertEquals(property.isMany(), false);
+
+        CompositeReference calcCompositeReference = (CompositeReference)composite.getReferences().get(0);
+        assertEquals(calcCompositeReference.getName(), "MultiplyService");
+        assertTrue(calcCompositeReference.getPromotedReferences().get(0).isUnresolved());
+        assertEquals(calcCompositeReference.getPromotedReferences().get(0).getName(),
+                     "CalculatorServiceComponent/multiplyService");
+        assertEquals(calcCompositeReference.getRequiredIntents().get(0).getName(),
+                     new QName("http://test", "confidentiality"));
+        assertEquals(calcCompositeReference.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+        // TODO test operations
+        Callback calcCallback = calcCompositeReference.getCallback();
+        assertEquals(calcCompositeReference.getRequiredIntents().get(0).getName(),
+                     new QName("http://test", "confidentiality"));
+        assertEquals(calcCompositeReference.getPolicySets().get(0).getName(), new QName("http://test", "SecureReliablePolicy"));
+        assertNotNull(calcCallback);
+        // TODO test operations
+
+        //new PrintUtil(System.out).print(composite);
+    }
+
+}

Added: incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java?rev=653133&view=auto
==============================================================================
--- incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java (added)
+++ incubator/tuscany/sandbox/mobile-android/tuscany-assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java Sat May  3 13:52:41 2008
@@ -0,0 +1,223 @@
+/*
+ * 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.net.URI;
+import java.net.URL;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.ValidatorHandler;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.ConstrainingType;
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+import org.apache.tuscany.sca.contribution.DefaultContributionFactory;
+import org.apache.tuscany.sca.contribution.DefaultModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.DefaultStAXArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.DefaultURLArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.DefaultValidatingXMLInputFactory;
+import org.apache.tuscany.sca.contribution.processor.DefaultValidationSchemaExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.ValidationSchemaExtensionPoint;
+import org.apache.tuscany.sca.policy.DefaultPolicyFactory;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+
+/**
+ * Test reading SCA XML assembly documents.
+ * 
+ * @version $Rev: 639226 $ $Date: 2008-03-20 02:45:37 -0700 (Thu, 20 Mar 2008) $
+ */
+public class ReadDocumentTestCase extends TestCase {
+
+    private ExtensibleURLArtifactProcessor documentProcessor;
+    private TestModelResolver resolver;
+    private XMLInputFactory inputFactory;
+    private ExtensibleStAXArtifactProcessor staxProcessor; 
+
+    @Override
+    public void setUp() throws Exception {
+        AssemblyFactory factory = new DefaultAssemblyFactory();
+        PolicyFactory policyFactory = new DefaultPolicyFactory();
+        
+        URLArtifactProcessorExtensionPoint documentProcessors = new DefaultURLArtifactProcessorExtensionPoint(new DefaultModelFactoryExtensionPoint());
+        documentProcessor = new ExtensibleURLArtifactProcessor(documentProcessors); 
+        
+        // Create StAX processors
+        DefaultStAXArtifactProcessorExtensionPoint staxProcessors = new DefaultStAXArtifactProcessorExtensionPoint(new DefaultModelFactoryExtensionPoint());
+        staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance());
+        staxProcessors.addArtifactProcessor(new CompositeProcessor(new DefaultContributionFactory(), factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ComponentTypeProcessor(factory, policyFactory, staxProcessor));
+        staxProcessors.addArtifactProcessor(new ConstrainingTypeProcessor(factory, policyFactory, staxProcessor));
+        
+        inputFactory = XMLInputFactory.newInstance();
+        documentProcessors.addArtifactProcessor(new CompositeDocumentProcessor(staxProcessor, inputFactory, null));
+        documentProcessors.addArtifactProcessor(new ComponentTypeDocumentProcessor(staxProcessor, inputFactory));
+        documentProcessors.addArtifactProcessor(new ConstrainingTypeDocumentProcessor(staxProcessor, inputFactory));
+        
+        resolver = new TestModelResolver();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+    }
+    
+    public void testValidateAssembly() throws Exception {
+        
+        SchemaFactory schemaFactory;
+        try {
+            schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        } catch (Error e) {
+            // Some old JDKs don't support XMLSchema validation
+            return;
+        } catch (Exception e) {
+            // Some old JDKs don't support XMLSchema validation
+            return;
+        }
+        Schema schema = schemaFactory.newSchema(getClass().getClassLoader().getResource("tuscany-sca.xsd"));
+        ValidatorHandler handler = schema.newValidatorHandler();
+        
+        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+        URL url = getClass().getResource("Calculator.composite");
+        XMLReader reader = parserFactory.newSAXParser().getXMLReader();
+        reader.setFeature("http://xml.org/sax/features/namespaces", true);
+        reader.setContentHandler(handler);
+        reader.parse(new InputSource(url.openStream()));
+    }
+
+    public void testValidateImplementation() throws Exception {
+        
+        SchemaFactory schemaFactory;
+        try {
+            schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        } catch (Error e) {
+            // Some old JDKs don't support XMLSchema validation
+            return;
+        } catch (Exception e) {
+            // Some old JDKs don't support XMLSchema validation
+            return;
+        }
+        Schema schema = schemaFactory.newSchema(getClass().getClassLoader().getResource("tuscany-sca.xsd"));
+        ValidatorHandler handler = schema.newValidatorHandler();
+        
+        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+        URL url = getClass().getResource("JavaScriptReference.composite");
+        XMLReader reader = parserFactory.newSAXParser().getXMLReader();
+        reader.setFeature("http://xml.org/sax/features/namespaces", true);
+        reader.setContentHandler(handler);
+        reader.parse(new InputSource(url.openStream()));
+    }
+        
+    public void testReadImplementation() throws Exception { 
+        
+        ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint();
+        schemas.addSchema(getClass().getClassLoader().getResource("tuscany-sca.xsd").toString());
+        XMLInputFactory validatingInputFactory = new DefaultValidatingXMLInputFactory(inputFactory, schemas);
+        CompositeDocumentProcessor compositeDocumentProcessor = new CompositeDocumentProcessor(staxProcessor, validatingInputFactory, null);
+        
+        URL url = getClass().getResource("JavaScriptReference.composite");
+        URI uri = URI.create("JavaScriptReference.composite");
+        Composite composite = (Composite)compositeDocumentProcessor.read(null, uri, url);
+        assertNotNull(composite);
+    }
+        
+    public void testValidateBinding() throws Exception {
+        
+        SchemaFactory schemaFactory;
+        try {
+            schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        } catch (Error e) {
+            // Some old JDKs don't support XMLSchema validation
+            return;
+        } catch (Exception e) {
+            // Some old JDKs don't support XMLSchema validation
+            return;
+        }
+        Schema schema = schemaFactory.newSchema(getClass().getClassLoader().getResource("tuscany-sca.xsd"));
+        ValidatorHandler handler = schema.newValidatorHandler();
+        
+        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+        URL url = getClass().getResource("RMIBindingTest.composite");
+        XMLReader reader = parserFactory.newSAXParser().getXMLReader();
+        reader.setFeature("http://xml.org/sax/features/namespaces", true);
+        reader.setContentHandler(handler);
+        reader.parse(new InputSource(url.openStream()));
+    }
+        
+    public void testReadBinding() throws Exception {
+        
+        ValidationSchemaExtensionPoint schemas = new DefaultValidationSchemaExtensionPoint();
+        schemas.addSchema(getClass().getClassLoader().getResource("tuscany-sca.xsd").toString());
+        XMLInputFactory validatingInputFactory = new DefaultValidatingXMLInputFactory(inputFactory, schemas);
+        CompositeDocumentProcessor compositeDocumentProcessor = new CompositeDocumentProcessor(staxProcessor, validatingInputFactory, null);
+        
+        URL url = getClass().getResource("RMIBindingTest.composite");
+        URI uri = URI.create("RMIBindingTest.composite");
+        Composite composite = (Composite)compositeDocumentProcessor.read(null, uri, url);
+        assertNotNull(composite);
+    }
+        
+    public void testResolveConstrainingType() throws Exception {
+        
+        URL url = getClass().getResource("CalculatorComponent.constrainingType");
+        URI uri = URI.create("CalculatorComponent.constrainingType");
+        ConstrainingType constrainingType = (ConstrainingType)documentProcessor.read(null, uri, url);
+        assertNotNull(constrainingType);
+        resolver.addModel(constrainingType);
+
+        url = getClass().getResource("TestAllCalculator.composite");
+        uri = URI.create("TestAllCalculator.constrainingType");
+        Composite composite = (Composite)documentProcessor.read(null, uri, url);
+        assertNotNull(composite);
+        
+        documentProcessor.resolve(composite, resolver);
+        
+        assertEquals(composite.getConstrainingType(), constrainingType);
+        assertEquals(composite.getComponents().get(0).getConstrainingType(), constrainingType);
+    }
+
+    public void testResolveComposite() throws Exception {
+        URL url = getClass().getResource("Calculator.composite");
+        URI uri = URI.create("Calculator.composite");
+        Composite nestedComposite = (Composite)documentProcessor.read(null, uri, url);
+        assertNotNull(nestedComposite);
+        resolver.addModel(nestedComposite);
+
+        url = getClass().getResource("TestAllCalculator.composite");
+        uri = URI.create("TestAllCalculator.composite");
+        Composite composite = (Composite)documentProcessor.read(null, uri, url);
+        
+        documentProcessor.resolve(composite, resolver);
+        
+        assertEquals(composite.getComponents().get(2).getImplementation(), nestedComposite);
+    }
+
+}