You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2007/07/19 00:03:59 UTC

svn commit: r557410 - in /incubator/tuscany/java/sca/modules: contribution-impl/src/test/java/org/apache/tuscany/sca/contribution/resolver/ contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/

Author: lresende
Date: Wed Jul 18 15:03:58 2007
New Revision: 557410

URL: http://svn.apache.org/viewvc?view=rev&rev=557410
Log:
Adding ModelReseolver extensibility

Added:
    incubator/tuscany/java/sca/modules/contribution-impl/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleArtifactResolverTestCase.java
    incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java
    incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java
    incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java

Added: incubator/tuscany/java/sca/modules/contribution-impl/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleArtifactResolverTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/contribution-impl/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleArtifactResolverTestCase.java?view=auto&rev=557410
==============================================================================
--- incubator/tuscany/java/sca/modules/contribution-impl/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleArtifactResolverTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/contribution-impl/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleArtifactResolverTestCase.java Wed Jul 18 15:03:58 2007
@@ -0,0 +1,144 @@
+/*
+ * 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 junit.framework.TestCase;
+
+import org.apache.tuscany.sca.contribution.ContributionFactory;
+import org.apache.tuscany.sca.contribution.DeployedArtifact;
+import org.apache.tuscany.sca.contribution.impl.ContributionFactoryImpl;
+import org.apache.tuscany.sca.contribution.resolver.impl.ModelResolverImpl;
+
+/**
+ * Test DefaultArtifactResolver.
+ *
+ * @version $Rev: 548560 $ $Date: 2007-06-18 19:25:19 -0700 (Mon, 18 Jun 2007) $
+ */
+public class ExtensibleArtifactResolverTestCase extends TestCase {
+    private ModelResolverExtensionPoint resolverExtensionPoint;
+    private ModelResolver defaultResolver;
+    
+    private ExtensibleModelResolver resolver;
+    
+    private ContributionFactory factory;
+    
+    protected void setUp() throws Exception {
+        
+        defaultResolver = new ModelResolverImpl(getClass().getClassLoader());
+        
+        resolverExtensionPoint = new DefaultModelResolverExtensionPoint();
+        resolverExtensionPoint.addResolver(Model.class, defaultResolver);
+        
+        resolver = new ExtensibleModelResolver(resolverExtensionPoint, defaultResolver);
+
+        factory = new ContributionFactoryImpl();
+    }
+    
+    protected void tearDown() throws Exception {
+        resolverExtensionPoint.removeResolver(Model.class);
+        resolverExtensionPoint = null;
+        defaultResolver = null;
+        resolver = null;
+        factory = null;
+    }
+    
+    public void testResolvedDefault() {
+        OtherModel a = new OtherModel("a");
+        resolver.addModel(a);
+        OtherModel x = new OtherModel("a");
+        x = resolver.resolveModel(OtherModel.class, x);
+        assertTrue(x == a);
+    }
+
+    public void testResolvedRegisteredClass() {
+        Model a = new Model("a");
+        resolver.addModel(a);
+        Model x = new Model("a");
+        x = resolver.resolveModel(Model.class, x);
+        assertTrue(x == a);
+    }
+
+    public void testUnresolvedDefault() {
+        OtherModel x = new OtherModel("a");
+        OtherModel y = resolver.resolveModel(OtherModel.class, x);
+        assertTrue(x == y);
+    }
+    
+    public void testUnresolved() {
+        Model x = new Model("a");
+        Model y = resolver.resolveModel(Model.class, x);
+        assertTrue(x == y);
+    }
+    
+    public void testResolveClass() {
+        ClassReference ref = new ClassReference(getClass().getName());
+        ClassReference clazz = resolver.resolveModel(ClassReference.class, ref);
+        assertTrue(clazz.getJavaClass() == getClass());
+    }
+    
+    public void testUnresolvedClass() {
+        ClassReference ref = new ClassReference("NonExistentClass");
+        ClassReference clazz = resolver.resolveModel(ClassReference.class, ref);
+        assertTrue(clazz.isUnresolved());
+        assertTrue(clazz.getJavaClass() == null);
+    }
+    
+    public void testResolvedArtifact() {
+        DeployedArtifact artifact = factory.createDeployedArtifact();
+        artifact.setURI("foo/bar");
+        resolver.addModel(artifact);
+        DeployedArtifact x = factory.createDeployedArtifact();
+        x.setURI("foo/bar");
+        x = resolver.resolveModel(DeployedArtifact.class, x);
+        assertTrue(x == artifact);
+    }
+    
+    class Model {
+        private String name;
+        
+        Model(String name) {
+            this.name = name;
+        }
+        
+        public int hashCode() {
+            return name.hashCode();
+        }
+        
+        public boolean equals(Object obj) {
+            return name.equals(((Model)obj).name);
+        }
+    }
+
+    class OtherModel {
+        private String name;
+        
+        OtherModel(String name) {
+            this.name = name;
+        }
+        
+        public int hashCode() {
+            return name.hashCode();
+        }
+        
+        public boolean equals(Object obj) {
+            return name.equals(((OtherModel)obj).name);
+        }
+    }
+}

Added: incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java?view=auto&rev=557410
==============================================================================
--- incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java (added)
+++ incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverExtensionPoint.java Wed Jul 18 15:03:58 2007
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+/**
+ * The default implementation of an model resolver registry.
+ * 
+ * @version $Rev: 539693 $ $Date: 2007-05-18 23:24:07 -0700 (Fri, 18 May 2007) $
+ */
+class DefaultModelResolverExtensionPoint implements ModelResolverExtensionPoint {
+    protected final Map<Class<?>, ModelResolver> resolversByModelType = new HashMap<Class<?>, ModelResolver>();
+
+    /**
+     * Constructs a new model resolver registry.
+     */
+    public DefaultModelResolverExtensionPoint() {
+    }
+
+    public void addResolver(Class<?> modelType, ModelResolver resolver) {
+        resolversByModelType.put(modelType, resolver);
+    }
+    
+    public void removeResolver(Class<?> modelType) {
+        resolversByModelType.remove(modelType);
+    }
+
+    public ModelResolver getResolver(Class<?> modelType) {
+        Class<?>[] classes = modelType.getInterfaces();
+        for (Class<?> c : classes) {
+            ModelResolver resolver = resolversByModelType.get(c);
+            if (resolver != null) {
+                return resolver;
+            }
+        }
+        return resolversByModelType.get(modelType);
+    }
+}

Added: incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java?view=auto&rev=557410
==============================================================================
--- incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java (added)
+++ incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolver.java Wed Jul 18 15:03:58 2007
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+public class ExtensibleModelResolver implements ModelResolver {
+    private final ModelResolverExtensionPoint resolverRegistry;
+    private final ModelResolver defaultModelResolver;
+
+    public ExtensibleModelResolver(ModelResolverExtensionPoint resolverRegistry, ModelResolver defaultModelResolver) {
+        this.resolverRegistry = resolverRegistry; 
+        this.defaultModelResolver = defaultModelResolver;
+    }
+    
+    public void addModel(Object resolved) {
+        ModelResolver resolver = this.resolverRegistry.getResolver(resolved.getClass());
+        if (resolver == null) {
+            resolver = defaultModelResolver;
+        }
+        
+        resolver.addModel(resolved);
+    }
+
+    public Object removeModel(Object resolved) {
+        ModelResolver resolver = this.resolverRegistry.getResolver(resolved.getClass());
+        if (resolver == null) {
+            resolver = defaultModelResolver;
+        } 
+        
+        return resolver.removeModel(resolved);
+    }
+    
+    public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+        ModelResolver resolver = this.resolverRegistry.getResolver(modelClass);
+        if (resolver == null) {
+            resolver = defaultModelResolver;
+        } 
+        
+        return resolver.resolveModel(modelClass, unresolved);
+    }
+}

Added: incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java?view=auto&rev=557410
==============================================================================
--- incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java (added)
+++ incubator/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ModelResolverExtensionPoint.java Wed Jul 18 15:03:58 2007
@@ -0,0 +1,51 @@
+/*
+ * 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: 539693 $ $Date: 2007-05-18 23:24:07 -0700 (Fri, 18 May 2007) $
+ */
+public interface ModelResolverExtensionPoint {
+
+    /**
+     * Register a Model Resolver using the Artifact Type as the key
+     * 
+     * @param modelType The model artifact type
+     * @param resolver The model resolver
+     */
+    void addResolver(Class<?> modelType, ModelResolver resolver);
+    
+    /**
+     * Remove the Model Resolver for a specific Artifact Type
+     * 
+     * @param modelType The model artifact type
+     */
+    void removeResolver(Class<?> modelType);
+    
+    /**
+     * Retrieve a Model Resolver for a specific Artifact Type
+     * 
+     * @param modelType The model artifact type
+     * @return The model resolver
+     */
+    ModelResolver getResolver(Class<?> modelType);
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org