diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:07:28 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:07:28 +0000 |
commit | 07b7dfd1a70ba222b899d9813f8c449dbf3b785f (patch) | |
tree | ecfc876747e6cfedee0a8533d87c19ac78a2dbd0 /sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl | |
parent | 6fadbc9cd1fcf03d0b3630f772d91df149b70428 (diff) |
Moving 1.x branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835124 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl')
2 files changed, 206 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl/ModelResolverImpl.java b/sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl/ModelResolverImpl.java new file mode 100644 index 0000000000..6c2257803a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl/ModelResolverImpl.java @@ -0,0 +1,92 @@ +/* + * 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.impl; + +import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * A default implementation of an artifact resolver, based on a map. + * + * @version $Rev$ $Date$ + */ +public class ModelResolverImpl implements ModelResolver { + private static final long serialVersionUID = -7826976465762296634L; + + private Map<Object, Object> map = new HashMap<Object, Object>(); + + private WeakReference<ClassLoader> classLoader; + + public ModelResolverImpl(ClassLoader classLoader) { + this.classLoader = new WeakReference<ClassLoader>(classLoader); + } + + 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 if (unresolved instanceof ClassReference) { + + // Load a class on demand + ClassReference classReference = (ClassReference)unresolved; + Class clazz; + try { + clazz = Class.forName(classReference.getClassName(), true, classLoader.get()); + } catch (ClassNotFoundException e) { + + // Return the unresolved object + return unresolved; + } + + // Store a new ClassReference wrappering the loaded class + resolved = new ClassReference(clazz); + map.put(resolved, resolved); + + // Return the resolved ClassReference + return modelClass.cast(resolved); + + } else { + + // Return the unresolved object + return unresolved; + } + } + + public void addModel(Object resolved) { + map.put(resolved, resolved); + } + + public Object removeModel(Object resolved) { + return map.remove(resolved); + } + + public Collection<Object> getModels() { + return map.values(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl/ResourceReferenceModelResolver.java b/sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl/ResourceReferenceModelResolver.java new file mode 100644 index 0000000000..b5df1cde37 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/resolver/impl/ResourceReferenceModelResolver.java @@ -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.impl;
+
+import java.lang.ref.WeakReference;
+import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.contribution.resolver.ResourceReference;
+
+/**
+ * A Model Resolver for ResourceReferences.
+ *
+ */
+public class ResourceReferenceModelResolver implements ModelResolver {
+ private Contribution contribution;
+ protected WeakReference<ClassLoader> classLoader;
+ private Map<String, ResourceReference> map = new HashMap<String, ResourceReference>();
+
+ private ModelResolver osgiResolver;
+
+ public ResourceReferenceModelResolver(Contribution contribution, ModelFactoryExtensionPoint modelFactories) {
+ this.contribution = contribution;
+ this.classLoader = new WeakReference<ClassLoader>(this.contribution.getClassLoader());
+
+ try {
+ Class osgiResolverClass =
+ Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiResourceReferenceModelResolver");
+ if (osgiResolverClass != null) {
+ Constructor constructor =
+ osgiResolverClass.getConstructor(Contribution.class, ModelFactoryExtensionPoint.class);
+ this.osgiResolver = (ModelResolver)constructor.newInstance(contribution, modelFactories);
+ }
+ } catch (Exception e) {
+ }
+ }
+
+ public void addModel(Object resolved) {
+ ResourceReference resourceRef = (ResourceReference)resolved;
+ map.put(resourceRef.getResourceName(), resourceRef);
+ }
+
+ public Object removeModel(Object resolved) {
+ return map.remove(((ResourceReference)resolved).getResourceName());
+ }
+
+ public <T> T resolveModel(Class<T> modelClass, T unresolved) {
+ Object resolved = map.get(unresolved);
+
+ if (resolved != null) {
+ return modelClass.cast(resolved);
+ }
+
+ //Get a resource
+ String resourceName = ((ResourceReference)unresolved).getResourceName();
+ URL resourceURL = null;
+
+ if (URI.create(resourceName).isAbsolute()) {
+ try {
+ resourceURL = URI.create(resourceName).toURL();
+ } catch (MalformedURLException e) {
+ // Ignore
+ }
+ }
+
+ if (resourceURL == null && osgiResolver != null) {
+ // Try OSGi
+ resolved = osgiResolver.resolveModel(modelClass, unresolved);
+ resourceURL = ((ResourceReference)resolved).getResource();
+ }
+
+ if (resourceURL == null) {
+ // Try class loader
+ resourceURL = classLoader.get().getResource(resourceName);
+ }
+
+ if (resourceURL != null) {
+ // Store a new ResourceReference wrappering the resource
+ ResourceReference resourceReference = new ResourceReference(resourceName, resourceURL);
+ map.put(resourceName, resourceReference);
+
+ // Return the resolved ResourceReference
+ return modelClass.cast(resourceReference);
+ } else {
+ return unresolved;
+ }
+
+ }
+
+}
|