You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2009/05/08 08:44:19 UTC

svn commit: r772861 [6/8] - in /tuscany/java/sca/modules/contribution-new: ./ META-INF/ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/tuscany/ src/main/java/org/apache/tuscany/sca/ src/main/java/org...

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLFragmentStreamReader.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLFragmentStreamReader.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLFragmentStreamReader.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLFragmentStreamReader.java Fri May  8 06:44:08 2009
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.contribution.processor.xml;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+
+public interface XMLFragmentStreamReader extends XMLStreamReader {
+    QName NIL_QNAME = new QName("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi");
+    String NIL_VALUE_TRUE = "true";
+
+    /**
+     * this will help to handle Text within the current element. user should
+     * pass the element text to the property list as this ELEMENT_TEXT as the
+     * key. This key deliberately has a space in it so that it is not a valid
+     * XML name
+     */
+    String ELEMENT_TEXT = "Element Text";
+
+    /**
+     * Extra method to query the state of the pullparser
+     */
+    boolean isDone();
+
+    /**
+     * add the parent namespace context to this parser
+     */
+    void setParentNamespaceContext(NamespaceContext nsContext);
+
+    /**
+     * Initiate the parser - this will do whatever the needed tasks to initiate
+     * the parser and must be called before attempting any specific parsing
+     * using this parser
+     */
+    void init();
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ClassReference.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ClassReference.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ClassReference.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ClassReference.java Fri May  8 06:44:08 2009
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.lang.ref.WeakReference;
+
+import org.apache.tuscany.sca.assembly.Base;
+
+/**
+ * A weak reference to a class, which should be used to register classes
+ * with an ArtifactResolver and resolve these classes later.
+ * 
+ * FIXME The core contribution model should not have dependencies on classes
+ * and ClassLoaders. This should move to the Java import support module.
+ *
+ * @version $Rev: 652044 $ $Date: 2008-04-28 03:21:39 +0100 (Mon, 28 Apr 2008) $
+ */
+public class ClassReference implements Base {
+    
+    private WeakReference<Class<?>> clazz;
+    private String className;
+
+    /**
+     * Constructs a new ClassReference.
+     * 
+     * @param clazz The class reference
+     */
+    public ClassReference(Class<?> clazz) {
+        this.clazz = new WeakReference<Class<?>>(clazz);
+        this.className = clazz.getName();
+    }
+    
+    /**
+     * Constructs a new ClassReference.
+     * 
+     * @param className The class name
+     */
+    public ClassReference(String className) {
+        this.className = className;
+    }
+    
+    /**
+     * Get the referenced class.
+     * 
+     * @return The referenced class
+     */
+    public Class<?> getJavaClass() {
+        if (clazz != null) {
+            return clazz.get();
+        } else {
+            return null;
+        }
+    }
+    
+    /**
+     * Get the referenced class name.
+     * 
+     * @return The class name
+     */
+    public String getClassName() {
+        return className;
+    }
+    
+    public boolean isUnresolved() {
+        return clazz == null;
+    }
+    
+    public void setUnresolved(boolean unresolved) {
+        throw new IllegalStateException();
+    }
+
+    @Override
+    public int hashCode() {
+        return className.hashCode();
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        } else {
+            if (obj instanceof ClassReference) {
+                return className.equals(((ClassReference)obj).className);
+            } else {
+                return false;
+            }
+        }
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultDelegatingModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultDelegatingModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultDelegatingModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultDelegatingModelResolver.java Fri May  8 06:44:08 2009
@@ -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.contribution.resolver;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Base;
+
+/**
+ * A model resolver implementation that delegates to a list of model resolvers.
+ *
+ * @version $Rev: 652044 $ $Date: 2008-04-28 03:21:39 +0100 (Mon, 28 Apr 2008) $
+ */
+public class DefaultDelegatingModelResolver implements ModelResolver {
+    
+    private List<ModelResolver> resolvers;
+    
+    public DefaultDelegatingModelResolver(List<ModelResolver> resolvers) {
+        this.resolvers = resolvers;
+    }
+
+    public void addModel(Object resolved) {
+        throw new IllegalStateException();
+    }
+
+    public Object removeModel(Object resolved) {
+        throw new IllegalStateException();
+    }
+
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        
+        //TODO optimize and cache results of the resolution later
+        
+        // Go over all resolvers
+        for (ModelResolver resolver: resolvers) {
+            
+            Object resolved = resolver.resolveModel(modelClass, unresolved);
+            
+            // Return the resolved model object
+            if (resolved instanceof Base) {
+                if (!((Base)resolved).isUnresolved()) {
+                    return modelClass.cast(resolved);
+                }
+            }
+        }
+
+        // Model object was not resolved
+        return unresolved;
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java Fri May  8 06:44:08 2009
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Base;
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.Export;
+import org.apache.tuscany.sca.contribution.Import;
+
+/**
+ * A model resolver implementation that considers Exports in a list of contributions.
+ *
+ * @version $Rev: 560435 $ $Date: 2007-07-27 18:26:55 -0700 (Fri, 27 Jul 2007) $
+ */
+public class DefaultImportAllModelResolver implements ModelResolver {
+    
+    private Import import_;
+    private List<Contribution> contributions;
+    
+    public DefaultImportAllModelResolver(Import import_, List<Contribution> contributions) {
+        this.import_ = import_;
+        this.contributions = contributions;
+    }
+
+    public void addModel(Object resolved) {
+        throw new IllegalStateException();
+    }
+
+    public Object removeModel(Object resolved) {
+        throw new IllegalStateException();
+    }
+
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        
+        //TODO optimize and cache results of the resolution later
+        
+        // Go over all available contributions
+        for (Contribution contribution : contributions) {
+            
+            // Go over all exports in the contribution
+            for (Export export : contribution.getExports()) {
+                
+                    // If the export matches the export, try to resolve the model object
+                    if (import_.match(export)) {
+                        Object resolved = export.getModelResolver().resolveModel(modelClass, unresolved);
+                        
+                        // Return the resolved model object
+                        if (resolved instanceof Base) {
+                            if (!((Base)resolved).isUnresolved()) {
+                                return modelClass.cast(resolved);
+                            }
+                        }
+                    }
+            }
+        }
+
+        // Model object was not resolved
+        return unresolved;
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportModelResolver.java Fri May  8 06:44:08 2009
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Base;
+import org.apache.tuscany.sca.contribution.Export;
+
+/**
+ * A model resolver implementation that delegates to a list of exports.
+ *
+ * @version $Rev: 652044 $ $Date: 2008-04-28 03:21:39 +0100 (Mon, 28 Apr 2008) $
+ */
+public class DefaultImportModelResolver implements ModelResolver {
+    
+    private List<Export> exports;
+    
+    public DefaultImportModelResolver(List<Export> exports) {
+        this.exports = exports;
+    }
+
+    public void addModel(Object resolved) {
+        throw new IllegalStateException();
+    }
+
+    public Object removeModel(Object resolved) {
+        throw new IllegalStateException();
+    }
+
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        
+        //TODO optimize and cache results of the resolution later
+        
+        // Go over all exports
+        for (Export export: exports) {
+            
+            Object resolved = export.getModelResolver().resolveModel(modelClass, unresolved);
+            
+            // Return the resolved model object
+            if (resolved instanceof Base) {
+                if (!((Base)resolved).isUnresolved()) {
+                    return modelClass.cast(resolved);
+                }
+            }
+        }
+
+        // Model object was not resolved
+        return unresolved;
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolver.java Fri May  8 06:44:08 2009
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.DefaultImport;
+import org.apache.tuscany.sca.contribution.Import;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+
+/**
+ * A default implementation of a model resolver based on a map.
+ *
+ * @version $Rev: 742767 $ $Date: 2009-02-09 22:11:34 +0000 (Mon, 09 Feb 2009) $
+ */
+public class DefaultModelResolver implements ModelResolver {
+    
+    private Contribution contribution;
+    private Map<Object, Object> map = new HashMap<Object, Object>();
+    
+    public DefaultModelResolver() {
+    }
+    
+    public DefaultModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) {
+        this.contribution = contribution;
+    }    
+    
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        Object resolved = map.get(unresolved);
+        if (resolved != null) {
+            
+            // Return the resolved object
+            return modelClass.cast(resolved);
+            
+        } else {
+            
+            // by default try and resolve through a default import
+            // if there is one. 
+            if (contribution != null){
+                for (Import _import : contribution.getImports()){
+                    if (_import instanceof DefaultImport){
+                        resolved = _import.getModelResolver().resolveModel(modelClass, unresolved);
+                        if (resolved != unresolved){
+                            return modelClass.cast(resolved);
+                        }
+                    }
+                }
+            }
+            
+            // Return the unresolved object
+            return unresolved;
+        }
+    }
+    
+    public void addModel(Object resolved) {
+        map.put(resolved, resolved);
+    }
+    
+    public Object removeModel(Object resolved) {
+        return map.remove(resolved);
+    }
+    
+    // FIXME: TUSCANY-2499: temporarily  give access to the models to get the jms binding
+    //  use of definitions.xml working while the definitions.xml processing is being refactored
+    public Map<Object, Object> getModels() {
+        return map;
+    }
+    
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java Fri May  8 06:44:08 2009
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
+import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
+
+
+/**
+ * The default implementation of a model resolver extension point.
+ * 
+ * @version $Rev: 746356 $ $Date: 2009-02-20 20:10:37 +0000 (Fri, 20 Feb 2009) $
+ */
+public class DefaultModelResolverExtensionPoint implements ModelResolverExtensionPoint {
+    
+    private final Map<Class<?>, Class<? extends ModelResolver>> resolvers = new HashMap<Class<?>, Class<? extends ModelResolver>>();
+    private Map<String, ServiceDeclaration> loadedResolvers;
+
+    /**
+     * Constructs a new DefaultModelResolverExtensionPoint.
+     */
+    public DefaultModelResolverExtensionPoint() {
+    }
+
+    public void addResolver(Class<?> modelType, Class<? extends ModelResolver> resolver) {
+        resolvers.put(modelType, resolver);
+    }
+
+    public void removeResolver(Class<?> modelType) {
+        resolvers.remove(modelType);
+    }
+
+    @SuppressWarnings("unchecked")
+    public Class<? extends ModelResolver> getResolver(Class<?> modelType) {
+        loadModelResolvers();
+        
+        Class<?>[] classes = modelType.getInterfaces();
+        for (Class<?> c : classes) {
+            Class<? extends ModelResolver> resolver = resolvers.get(c);
+            if (resolver == null) {
+                ServiceDeclaration resolverClass = loadedResolvers.get(c.getName());
+                if (resolverClass != null) {
+                    try {
+                        return (Class<? extends ModelResolver>)resolverClass.loadClass();
+                    } catch (ClassNotFoundException e) {
+                        throw new IllegalArgumentException(e);
+                    }
+                }
+            } else {
+                return resolver;
+            }
+        }
+
+        Class<? extends ModelResolver > resolver = resolvers.get(modelType);
+        if (resolver == null) {
+            ServiceDeclaration resolverClass = loadedResolvers.get(modelType.getName());
+            if (resolverClass != null) {
+                try {
+                    return (Class<? extends ModelResolver>)resolverClass.loadClass();
+                } catch (ClassNotFoundException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            }
+        }
+        return resolver;
+    }
+
+    /**
+     * Dynamically load model resolvers declared under META-INF/services
+     */
+    private synchronized void loadModelResolvers() {
+        if (loadedResolvers != null)
+            return;
+        loadedResolvers = new HashMap<String, ServiceDeclaration>();
+
+        // Get the model resolver service declarations
+        Set<ServiceDeclaration> modelResolverDeclarations; 
+        try {
+            modelResolverDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ModelResolver.class.getName());
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+        
+        // Load model resolvers
+        for (ServiceDeclaration modelResolverDeclaration: modelResolverDeclarations) {
+            Map<String, String> attributes = modelResolverDeclaration.getAttributes();
+            String model = attributes.get("model");
+
+            loadedResolvers.put(model, modelResolverDeclaration);
+        }
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java Fri May  8 06:44:08 2009
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.lang.reflect.Constructor;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+
+/**
+ * An implementation of an extensible model resolver which delegates to the
+ * proper resolver extension based on the class of the model to resolve.
+ * 
+ * @version $Rev: 709339 $ $Date: 2008-10-31 04:01:44 +0000 (Fri, 31 Oct 2008) $
+ */
+public class ExtensibleModelResolver implements ModelResolver {
+    private final ModelResolverExtensionPoint resolverExtensions;
+    private final FactoryExtensionPoint modelFactories;
+    private final Contribution contribution;
+    private ModelResolver defaultResolver;
+    private final Map<Class<?>, ModelResolver> resolversByModelType = new HashMap<Class<?>, ModelResolver>();
+    private final Map<Class<?>, ModelResolver> resolversByImplementationClass = new HashMap<Class<?>, ModelResolver>();
+    private Map<Object, Object> map = new HashMap<Object, Object>();
+    private Object lastUnresolved;
+
+    /**
+     * Constructs an extensible model resolver
+     * 
+     * @param contribution
+     * @param resolverExtensions
+     * @param modelFactories
+     * @param defaultResolver
+     */
+    @Deprecated
+    public ExtensibleModelResolver(Contribution contribution,
+                                   ModelResolverExtensionPoint resolverExtensions,
+                                   FactoryExtensionPoint modelFactories,
+                                   ModelResolver defaultResolver) {
+        this.contribution = contribution;
+        this.resolverExtensions = resolverExtensions;
+        this.modelFactories = modelFactories;
+        //FIXME Remove this default resolver, this is currently used to resolve policy declarations
+        // but they should be handled by the contribution import/export mechanism instead of this
+        // defaultResolver hack.
+        this.defaultResolver = defaultResolver;
+    }
+
+    /**
+     * Constructs an extensible model resolver
+     * 
+     * @param resolverExtensions
+     * @param contribution
+     * @param modelFactories
+     */
+    public ExtensibleModelResolver(Contribution contribution,
+                                   ModelResolverExtensionPoint resolverExtensions,
+                                   FactoryExtensionPoint modelFactories) {
+        this.contribution = contribution;
+        this.resolverExtensions = resolverExtensions;
+        this.modelFactories = modelFactories;
+    }
+
+    /**
+     * Returns the proper resolver instance based on the interfaces of the model
+     * If one is not available on the registry, instantiate on demand
+     * 
+     * @param modelType
+     * @return
+     */
+    private ModelResolver getModelResolverInstance(Class<?> modelType) {
+        // Look up a model resolver instance for the model class or
+        // each implemented interface
+        Class<?>[] interfaces = modelType.getInterfaces();
+        Class<?>[] classes = new Class<?>[interfaces.length + 1];
+        classes[0] = modelType;
+        if (interfaces.length != 0) {
+            System.arraycopy(interfaces, 0, classes, 1, interfaces.length);
+        }
+        for (Class<?> c : classes) {
+
+            // Look up an existing model resolver instance
+            ModelResolver resolverInstance = resolversByModelType.get(c);
+            if (resolverInstance != null) {
+                return resolverInstance;
+            }
+
+            // We don't have an instance, lookup a model resolver class
+            // and instantiate it
+            Class<? extends ModelResolver> resolverClass = resolverExtensions.getResolver(c);
+            if (resolverClass != null) {
+
+                // Construct the model resolver instance and cache it
+                resolverInstance = resolversByImplementationClass.get(resolverClass);
+                if (resolverInstance != null) {
+                    resolversByModelType.put(c, resolverInstance);
+                    return resolverInstance;
+                }
+                try {
+                    Constructor<? extends ModelResolver> constructor =
+                        resolverClass
+                            .getConstructor(new Class[] {Contribution.class, FactoryExtensionPoint.class});
+                    if (constructor != null) {
+
+                        resolverInstance = constructor.newInstance(contribution, modelFactories);
+                        resolversByImplementationClass.put(resolverClass, resolverInstance);
+                        resolversByModelType.put(c, resolverInstance);
+                        return resolverInstance;
+                    }
+                } catch (Exception e) {
+                    throw new IllegalStateException(e);
+                }
+            }
+        }
+
+        return null;
+    }
+
+    public void addModel(Object resolved) {
+        ModelResolver resolver = getModelResolverInstance(resolved.getClass());
+        if (resolver != null) {
+            resolver.addModel(resolved);
+        } else {
+            map.put(resolved, resolved);
+        }
+    }
+
+    public Object removeModel(Object resolved) {
+        ModelResolver resolver = getModelResolverInstance(resolved.getClass());
+        if (resolver != null) {
+            return resolver.removeModel(resolved);
+        } else {
+            return map.remove(resolved);
+        }
+    }
+
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        // Protect against dependency cycles causing infinite recursion
+        // Save the current unresolved object and check later if we are trying
+        // to resolve the same object again
+        if (unresolved == lastUnresolved) {
+            return unresolved;
+        }
+        lastUnresolved = unresolved;
+        
+        ModelResolver resolver = getModelResolverInstance(unresolved.getClass());
+        if (resolver != null) {
+            Object resolved = resolver.resolveModel(modelClass, unresolved);
+            if (resolved != null && resolved != unresolved) {
+                lastUnresolved = null;
+                return modelClass.cast(resolved);
+            }
+        } else {
+            //FIXME Remove this default resolver, this is currently used to resolve policy declarations
+            // but they should be handled by the contribution import/export mechanism instead of this
+            // defaultResolver hack.
+            if (defaultResolver != null) {
+                Object resolved = defaultResolver.resolveModel(modelClass, unresolved);
+                if (resolved != null && resolved != unresolved) {
+                    lastUnresolved = null;
+                    return modelClass.cast(resolved);
+                }
+            }
+            
+            Object resolved = map.get(unresolved);
+            if (resolved != null) {
+                // Return the resolved object
+                lastUnresolved = null;
+                return modelClass.cast(resolved);
+            }
+        }
+
+        return unresolved;
+    }
+    
+    // FIXME: TUSCANY-2499: temporarily  give access to the defaultResolver to get the jms binding
+    //  use of definitions.xml working while the definitions.xml processing is being refactored
+    public ModelResolver getDefaultModelResolver() {
+        return defaultResolver;
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolver.java Fri May  8 06:44:08 2009
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+/**
+ * A model resolver, responsible for resolving models in the scope of an
+ * SCA contribution. 
+ * 
+ * SCA Assemblies reference artifacts of a wide variety of types. These
+ * include:
+ * <ul>
+ * <li> Reference from one SCA composite to another SCA composite
+ * <li> Reference to PolicySet files
+ * <li> Reference to interface definition files, either WSDL or Java interfaces
+ * <li> Reference to XSD files
+ * <li> Reference to any of a wide variety of implementation artifact files,
+ * including Java classes, BPEL scripts, C++ DLLs and classes, PHP scripts
+ * </ul>
+ * 
+ * In the SCA assemblies, these various artifacts are referenced using either
+ * QNames or logical URIs. Model resolvers are used to resolve these references
+ * and get the in-memory models representing the referenced artifacts.
+ * 
+ * @version $Rev: 616114 $ $Date: 2008-01-29 00:05:37 +0000 (Tue, 29 Jan 2008) $
+ */
+public interface ModelResolver {
+
+    /**
+     * Resolve the model representing an artifact.
+     * 
+     * @param modelClass the type of artifact
+     * @param unresolved the unresolved model
+     * @return the resolved model
+     */
+    <T> T resolveModel(Class<T> modelClass, T unresolved);
+    
+    /**
+     * Add a resolved model.
+     * 
+     * @param resolved The model
+     */
+    void addModel(Object resolved);
+    
+    /**
+     * Remove a resolved model.
+     * 
+     * @param resolved
+     * @return The removed model, or null if the model was not removed
+     */
+    Object removeModel(Object resolved);
+    
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java Fri May  8 06:44:08 2009
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+
+/**
+ * An extension point for model resolvers
+ * 
+ * @version $Rev: 616114 $ $Date: 2008-01-29 00:05:37 +0000 (Tue, 29 Jan 2008) $
+ */
+public interface ModelResolverExtensionPoint {
+
+    /**
+     * Register a model resolver class using the model type as the key
+     * 
+     * @param modelType The model type
+     * @param resolver The model resolver Class
+     */
+    void addResolver(Class<?> modelType, Class <? extends ModelResolver> resolver);
+    
+    /**
+     * Remove the model resolver class for a specific model type
+     * 
+     * @param modelType The model type
+     */
+    void removeResolver(Class<?> modelType);
+    
+    /**
+     * Retrieve a model resolver class for a specific model type
+     * 
+     * @param modelType The model artifact type
+     * @return The model resolver Class
+     */
+    Class <? extends ModelResolver> getResolver(Class<?> modelType);
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResolverExtension.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResolverExtension.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResolverExtension.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResolverExtension.java Fri May  8 06:44:08 2009
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+/**
+ * Extension to assembly model implementations to provide a model resolver.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface ResolverExtension {
+
+    /**
+     * Returns the model resolver for the models representing the artifacts
+     * visible in the scope of this contribution.
+     * 
+     * @return The model resolver
+     */
+    ModelResolver getModelResolver();
+    
+    /**
+     * Sets the model resolver for the models representing the artifacts
+     * visible in the scope of this contribution.
+     * 
+     * @param modelResolver The model resolver
+     */
+    void setModelResolver(ModelResolver modelResolver);
+    
+}
\ No newline at end of file

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java Fri May  8 06:44:08 2009
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.resolver;
+
+import java.net.URL;
+
+/**
+ * A resource URL, which should be used to register resources
+ * with an ArtifactResolver and resolve these resources later.
+ * 
+ * FIXME Don't use as its deprecated, use Artifact instead.
+ *
+ * @version $Rev$ $Date$
+ */
+@Deprecated
+public class ResourceReference {
+    
+    private URL resourceURL;
+    private String resourceName;
+
+    /**
+     * Constructs a new ResourceReference.
+     * 
+     * @param resourceName Name of resource
+     * @param resourceURL  The resource URL
+     */
+    public ResourceReference(String resourceName, URL resourceURL) {
+        this.resourceURL = resourceURL;
+        this.resourceName = resourceName;
+    }
+    
+    /**
+     * Constructs a new ResourceReference.
+     * 
+     * @param resourceName Name of resource
+     */
+    public ResourceReference(String resourceName) {
+        this.resourceName = resourceName;
+    }
+    
+    /**
+     * Get the resource URL.
+     * 
+     * @return The resource URL
+     */
+    public URL getResource() {
+        return resourceURL;
+    }
+    
+    /**
+     * Get the resource name.
+     * 
+     * @return The resource name
+     */
+    public String getResourceName() {
+        return resourceName;
+    }
+    
+    /**
+     * Returns true if the resource reference is unresolved.
+     * 
+     * @return Whether or not the resource has been resolved
+     */
+    public boolean isUnresolved() {
+        return resourceURL == null;
+    }
+
+    @Override
+    public int hashCode() {
+        return resourceName.hashCode();
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        } else {
+            if (obj instanceof ResourceReference) {
+                return resourceName.equals(((ResourceReference)obj).resourceName);
+            } else {
+                return false;
+            }
+        }
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java Fri May  8 06:44:08 2009
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.contribution.scanner;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.List;
+
+import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
+
+/**
+ * Interface for contribution package scanners
+ * 
+ * Contribution scanners understand the format of the contribution and how to get the
+ * artifacts in the contribution.
+ * 
+ * @version $Rev: 742767 $ $Date: 2009-02-09 22:11:34 +0000 (Mon, 09 Feb 2009) $
+ */
+public interface ContributionScanner {
+    
+    /**
+     * Returns the type of package supported by this package scanner.
+     * 
+     * @return the package type
+     */
+    String getContributionType();
+
+    /**
+     * Returns a list of artifacts in the contribution.
+     * 
+     * @param contributionURL Contribution URL
+     * @return List of artifact URIs
+     * @throws ContributionReadException
+     * @throws IOException
+     */
+    List<String> getArtifacts(URL contributionURL) throws ContributionReadException;
+
+    /**
+     * Return the URL for an artifact in the contribution.
+     * 
+     * This is needed for archives such as jar files that have specific URL schemes
+     * for the artifacts they contain.
+     * 
+     * @param contributionURL Contribution URL
+     * @param artifact The relative URI for the artifact
+     * @throws ContributionReadException
+     * @return The artifact URL
+     */
+    URL getArtifactURL(URL contributionURL, String artifact) throws ContributionReadException;
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScannerExtensionPoint.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScannerExtensionPoint.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScannerExtensionPoint.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScannerExtensionPoint.java Fri May  8 06:44:08 2009
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.contribution.scanner;
+
+/**
+ * An extension point for contribution scanners
+ * 
+ * @version $Rev: 631271 $ $Date: 2008-02-26 16:11:36 +0000 (Tue, 26 Feb 2008) $
+ */
+public interface ContributionScannerExtensionPoint {
+    
+    /**
+     * Add a ContributionScanner using the contribution type as the key.
+     * 
+     * @param scanner The contribution scanner
+     */
+    void addContributionScanner(ContributionScanner scanner);
+    
+    /**
+     * Remove a ContributionScanner.
+     * 
+     * @param scanner The contribution scanner
+     */
+    void removeContributionScanner(ContributionScanner scanner);
+    
+    /**
+     * Returns the ContributionScanner for the given contribution type.
+     * 
+     * @param contributionType The contribution type
+     * @return The contribution scanner
+     */
+    ContributionScanner getContributionScanner(String contributionType);
+    
+}
\ No newline at end of file

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java Fri May  8 06:44:08 2009
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.scanner;
+
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
+import org.apache.tuscany.sca.extensibility.ServiceDeclaration;
+import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
+
+/**
+ * Default implementation of a contribution scanner extension point.
+ * 
+ * @version $Rev: 746356 $ $Date: 2009-02-20 20:10:37 +0000 (Fri, 20 Feb 2009) $
+ */
+public class DefaultContributionScannerExtensionPoint implements ContributionScannerExtensionPoint {
+
+    private Map<String, ContributionScanner> scanners = new HashMap<String, ContributionScanner>();
+    private boolean loaded;
+
+    public DefaultContributionScannerExtensionPoint() {
+    }
+
+    public void addContributionScanner(ContributionScanner scanner) {
+        scanners.put(scanner.getContributionType(), scanner);
+    }
+
+    public void removeContributionScanner(ContributionScanner scanner) {
+        scanners.remove(scanner.getContributionType());
+    }
+
+    public ContributionScanner getContributionScanner(String contentType) {
+        loadScanners();
+        return scanners.get(contentType);
+    }
+
+    private synchronized void loadScanners() {
+        if (loaded)
+            return;
+
+        // Get the scanner service declarations
+        Set<ServiceDeclaration> scannerDeclarations; 
+        try {
+            scannerDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ContributionScanner.class.getName());
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+        
+        for (ServiceDeclaration scannerDeclaration: scannerDeclarations) {
+            Map<String, String> attributes = scannerDeclaration.getAttributes();
+            
+            // Load a URL artifact scanner
+            String contributionType = attributes.get("type");
+            
+            // Create a scanner wrapper and register it
+            ContributionScanner scanner = new LazyContributionScanner(contributionType, scannerDeclaration);
+            addContributionScanner(scanner);
+        }
+        
+        loaded = true;
+    }
+
+    /**
+     * A facade for contribution scanners.
+     */
+    private static class LazyContributionScanner implements ContributionScanner {
+        
+        private ServiceDeclaration scannerDeclaration;
+        private String contributionType;
+        private ContributionScanner scanner;
+        
+        private LazyContributionScanner(String contributionType, ServiceDeclaration scannerDeclaration) {
+            this.scannerDeclaration = scannerDeclaration;
+            this.contributionType = contributionType;
+        }
+
+        public URL getArtifactURL(URL contributionSourceURL, String artifact) throws ContributionReadException {
+            return getScanner().getArtifactURL(contributionSourceURL, artifact);
+        }
+
+        public List<String> getArtifacts(URL contributionSourceURL) throws ContributionReadException {
+            return getScanner().getArtifacts(contributionSourceURL);
+        }
+
+        public String getContributionType() {
+            return contributionType;
+        }
+        
+        private ContributionScanner getScanner() {
+            if (scanner == null) {
+                try {
+                    Class<ContributionScanner> scannerClass = (Class<ContributionScanner>)scannerDeclaration.loadClass();
+                    Constructor<ContributionScanner> constructor = scannerClass.getConstructor();
+                    scanner = constructor.newInstance();
+                } catch (Exception e) {
+                    throw new IllegalStateException(e);
+                }
+            }
+            return scanner;
+        }
+    }
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionGeneratedMetadataDocumentProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionGeneratedMetadataDocumentProcessor.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionGeneratedMetadataDocumentProcessor.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionGeneratedMetadataDocumentProcessor.java Fri May  8 06:44:08 2009
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.contribution.xml;
+
+import javax.xml.stream.XMLInputFactory;
+
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.monitor.Monitor;
+
+/**
+ * URLArtifactProcessor that handles sca-contribution-generated.xml files.
+ *
+ * @version $Rev: 758464 $ $Date: 2009-03-25 23:40:38 +0000 (Wed, 25 Mar 2009) $
+ */
+public class ContributionGeneratedMetadataDocumentProcessor extends ContributionMetadataDocumentProcessor {
+
+    public ContributionGeneratedMetadataDocumentProcessor(XMLInputFactory inputFactory,
+    													  StAXArtifactProcessor staxProcessor,
+    													  Monitor monitor) {
+        super(inputFactory, staxProcessor, monitor);
+    }
+
+    public ContributionGeneratedMetadataDocumentProcessor(FactoryExtensionPoint modelFactories,
+    													  StAXArtifactProcessor staxProcessor,
+    													  Monitor monitor) {
+        super(modelFactories, staxProcessor, monitor);
+    }
+
+    @Override
+    public String getArtifactType() {
+        return "/META-INF/sca-contribution-generated.xml";
+    }
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java Fri May  8 06:44:08 2009
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.contribution.xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.sca.contribution.ContributionMetadata;
+import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
+import org.apache.tuscany.sca.contribution.processor.ContributionResolveException;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.monitor.Monitor;
+import org.apache.tuscany.sca.monitor.Problem;
+import org.apache.tuscany.sca.monitor.Problem.Severity;
+
+/**
+ * URLArtifactProcessor that handles sca-contribution.xml files.
+ *
+ * @version $Rev: 758464 $ $Date: 2009-03-25 23:40:38 +0000 (Wed, 25 Mar 2009) $
+ */
+public class ContributionMetadataDocumentProcessor implements URLArtifactProcessor<ContributionMetadata>{
+    private final StAXArtifactProcessor staxProcessor;
+    private final XMLInputFactory inputFactory;
+    private final Monitor monitor;
+
+    public ContributionMetadataDocumentProcessor(XMLInputFactory inputFactory,
+                                                 StAXArtifactProcessor staxProcessor,
+                                                 Monitor monitor) {
+        this.inputFactory = inputFactory;
+        this.staxProcessor = staxProcessor;
+        this.monitor = monitor;
+    }
+
+    public ContributionMetadataDocumentProcessor(FactoryExtensionPoint modelFactories,
+                                                 StAXArtifactProcessor staxProcessor,
+                                                 Monitor monitor) {
+        this.inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class);
+        this.staxProcessor = staxProcessor;
+        this.monitor = monitor;
+    }
+
+    /**
+     * Report a exception.
+     *
+     * @param problems
+     * @param message
+     * @param model
+     */
+    private void error(String message, Object model, Exception ex) {
+    	if (monitor != null) {
+	        Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-xml-validation-messages", Severity.ERROR, model, message, ex);
+	        monitor.problem(problem);
+    	}
+    }
+
+    public String getArtifactType() {
+        return "/META-INF/sca-contribution.xml";
+    }
+
+    public Class<ContributionMetadata> getModelType() {
+        return ContributionMetadata.class;
+    }
+
+    public ContributionMetadata 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 contribution model
+            ContributionMetadata contribution = (ContributionMetadata)staxProcessor.read(reader);
+
+            return contribution;
+
+        } catch (XMLStreamException e) {
+        	ContributionReadException ex = new ContributionReadException(e);
+        	error("XMLStreamException", inputFactory, ex);
+        	throw ex;
+        } catch (IOException e) {
+        	ContributionReadException ex = new ContributionReadException(e);
+        	error("IOException", inputFactory, ex);
+            throw ex;
+        } finally {
+            try {
+                if (urlStream != null) {
+                    urlStream.close();
+                    urlStream = null;
+                }
+            } catch (IOException ioe) {
+                //ignore
+            }
+        }
+    }
+
+    public void resolve(ContributionMetadata contribution, ModelResolver resolver) throws ContributionResolveException {
+        staxProcessor.resolve(contribution, resolver);
+    }
+
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessor.java Fri May  8 06:44:08 2009
@@ -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.contribution.xml;
+
+import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
+
+import java.util.List;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.contribution.ContributionFactory;
+import org.apache.tuscany.sca.contribution.ContributionMetadata;
+import org.apache.tuscany.sca.contribution.Export;
+import org.apache.tuscany.sca.contribution.Import;
+import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
+import org.apache.tuscany.sca.contribution.processor.ContributionResolveException;
+import org.apache.tuscany.sca.contribution.processor.ContributionWriteException;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.monitor.Monitor;
+import org.apache.tuscany.sca.monitor.Problem;
+import org.apache.tuscany.sca.monitor.Problem.Severity;
+
+/**
+ * Processor for contribution metadata
+ *
+ * @version $Rev: 767701 $ $Date: 2009-04-22 23:49:55 +0100 (Wed, 22 Apr 2009) $
+ */
+public class ContributionMetadataProcessor extends BaseStAXArtifactProcessor implements
+    StAXArtifactProcessor<ContributionMetadata> {
+    private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903";
+
+    private static final QName CONTRIBUTION_QNAME = new QName(SCA11_NS, "contribution");
+    private static final QName DEPLOYABLE_QNAME = new QName(SCA11_NS, "deployable");
+
+    private final AssemblyFactory assemblyFactory;
+    private final ContributionFactory contributionFactory;
+    private final StAXArtifactProcessor<Object> extensionProcessor;
+    private final StAXAttributeProcessor<Object> attributeProcessor;
+    private Monitor monitor;
+
+    public ContributionMetadataProcessor(FactoryExtensionPoint modelFactories,
+                                         StAXArtifactProcessor<Object> extensionProcessor,
+                                         StAXAttributeProcessor<Object> attributeProcessor,
+                                         Monitor monitor) {
+        this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class);
+        this.contributionFactory = modelFactories.getFactory(ContributionFactory.class);
+        this.extensionProcessor = extensionProcessor;
+        this.attributeProcessor = attributeProcessor;
+        this.monitor = monitor;
+    }
+
+    /**
+     * Report a error.
+     *
+     * @param problems
+     * @param message
+     * @param model
+     */
+    private void error(String message, Object model, Object... messageParameters) {
+        if (monitor != null) {
+            Problem problem =
+                monitor.createProblem(this.getClass().getName(),
+                                      "contribution-xml-validation-messages",
+                                      Severity.ERROR,
+                                      model,
+                                      message,
+                                      (Object[])messageParameters);
+            monitor.problem(problem);
+        }
+    }
+
+    /**
+     * Report a exception.
+     *
+     * @param problems
+     * @param message
+     * @param model
+     */
+    private void error(String message, Object model, Exception ex) {
+        if (monitor != null) {
+            Problem problem =
+                monitor.createProblem(this.getClass().getName(), "contribution-xml-validation-messages", Severity.ERROR,
+                                model, message, ex);
+            monitor.problem(problem);
+        }
+    }
+
+    public QName getArtifactType() {
+        return CONTRIBUTION_QNAME;
+    }
+
+    public Class<ContributionMetadata> getModelType() {
+        return ContributionMetadata.class;
+    }
+
+    public ContributionMetadata read(XMLStreamReader reader) throws ContributionReadException {
+        ContributionMetadata contribution = null;
+        QName name = null;
+
+        try {
+            while (reader.hasNext()) {
+                int event = reader.getEventType();
+                switch (event) {
+                    case START_ELEMENT:
+                        name = reader.getName();
+
+                        if (CONTRIBUTION_QNAME.equals(name)) {
+
+                            // Read <contribution>
+                            contribution = this.contributionFactory.createContributionMetadata();
+                            contribution.setUnresolved(true);
+                            readExtendedAttributes(reader, contribution, attributeProcessor, assemblyFactory);
+
+                        } else if (DEPLOYABLE_QNAME.equals(name)) {
+
+                            // Read <deployable>
+                            QName compositeName = getQName(reader, "composite");
+                            if (compositeName == null) {
+                                error("AttributeCompositeMissing", reader);
+                                //throw new ContributionReadException("Attribute 'composite' is missing");
+                            } else {
+                                if (contribution != null) {
+                                    Composite composite = assemblyFactory.createComposite();
+                                    composite.setName(compositeName);
+                                    composite.setUnresolved(true);
+                                    contribution.getDeployables().add(composite);
+                                }
+                            }
+                        } else {
+
+                            // Read an extension element
+                            Object extension = extensionProcessor.read(reader);
+                            if (extension != null && contribution != null) {
+                                if (extension instanceof Import) {
+                                    contribution.getImports().add((Import)extension);
+                                } else if (extension instanceof Export) {
+                                    contribution.getExports().add((Export)extension);
+                                } else {
+                                    contribution.getExtensions().add(extension);
+                                }
+                            }
+                        }
+                        break;
+
+                    case XMLStreamConstants.END_ELEMENT:
+                        if (CONTRIBUTION_QNAME.equals(reader.getName())) {
+                            return contribution;
+                        }
+                        break;
+                }
+
+                //Read the next element
+                if (reader.hasNext()) {
+                    reader.next();
+                }
+            }
+        } catch (XMLStreamException e) {
+            ContributionReadException ex = new ContributionReadException(e);
+            error("XMLStreamException", reader, ex);
+        }
+
+        return contribution;
+    }
+
+    public void write(ContributionMetadata contribution, XMLStreamWriter writer) throws ContributionWriteException,
+        XMLStreamException {
+
+        // Write <contribution>
+        writeStartDocument(writer, CONTRIBUTION_QNAME.getNamespaceURI(), CONTRIBUTION_QNAME.getLocalPart());
+        writeExtendedAttributes(writer, contribution, attributeProcessor);
+
+        // Write <import>
+        for (Import imp : contribution.getImports()) {
+            extensionProcessor.write(imp, writer);
+        }
+
+        // Write <export>
+        for (Export export : contribution.getExports()) {
+            extensionProcessor.write(export, writer);
+        }
+
+        // Write <deployable>
+        for (Composite deployable : contribution.getDeployables()) {
+            writeStart(writer,
+                       DEPLOYABLE_QNAME.getNamespaceURI(),
+                       DEPLOYABLE_QNAME.getLocalPart(),
+                       new XAttr("composite", deployable.getName()));
+            writeEnd(writer);
+        }
+
+        writeEndDocument(writer);
+    }
+
+    public void resolve(ContributionMetadata contribution, ModelResolver resolver) throws ContributionResolveException {
+
+        // Resolve imports and exports
+        for (Export export : contribution.getExports()) {
+            extensionProcessor.resolve(export, resolver);
+        }
+        for (Import import_ : contribution.getImports()) {
+            extensionProcessor.resolve(import_, resolver);
+        }
+
+        // Resolve deployable composites
+        List<Composite> deployables = contribution.getDeployables();
+        for (int i = 0, n = deployables.size(); i < n; i++) {
+            Composite deployable = deployables.get(i);
+            Composite resolved = (Composite)resolver.resolveModel(Composite.class, deployable);
+            if (resolved != deployable) {
+                deployables.set(i, resolved);
+            }
+        }
+
+        for (Object ext : contribution.getExtensions()) {
+            extensionProcessor.resolve(ext, resolver);
+        }
+
+        contribution.setUnresolved(false);
+    }
+}

Added: tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionModelResolver.java?rev=772861&view=auto
==============================================================================
--- tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionModelResolver.java (added)
+++ tuscany/java/sca/modules/contribution-new/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionModelResolver.java Fri May  8 06:44:08 2009
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.contribution.xml;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+
+/**
+ * A Model Resolver for Contribution models.
+ *
+ * @version $Rev: 702000 $ $Date: 2008-10-06 09:32:38 +0100 (Mon, 06 Oct 2008) $
+ */
+public class ContributionModelResolver implements ModelResolver {
+
+    private Map<String, Contribution> map = new HashMap<String, Contribution>();
+    
+    public ContributionModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) {
+    }
+
+    public void addModel(Object resolved) {
+        Contribution contribution = (Contribution)resolved;
+        map.put(contribution.getURI(), contribution);
+    }
+    
+    public Object removeModel(Object resolved) {
+        return map.remove(((Contribution)resolved).getURI());
+    }
+    
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        
+        // Lookup a contribution for the given URI
+        String uri = ((Contribution)unresolved).getURI();
+        if (uri != null) {
+            Contribution resolved = (Contribution) map.get(uri);
+            if (resolved != null) {
+                return modelClass.cast(resolved);
+            }
+            return unresolved;
+        } else {
+            
+            // If no URI was specified, just return the first contribution
+            if (!map.isEmpty()) {
+                Contribution resolved = map.values().iterator().next();
+                return modelClass.cast(resolved);
+            } else {
+                return unresolved;
+            }
+        }
+    }
+    
+}