diff options
Diffstat (limited to 'java/sca/modules/contribution/src')
11 files changed, 1219 insertions, 1219 deletions
diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java index af2ca9cd57..ee94d889ad 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java @@ -1,42 +1,42 @@ -/*
- * 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.java;
-
-/**
- * Base Java Import/Export model factory
- *
- * @version $Rev$ $Date$
- */
-public interface JavaImportExportFactory {
-
- /**
- * Create a java import model object
- *
- * @return The new JavaImport model object
- */
- JavaImport createJavaImport();
-
- /**
- * Create a java export model object
- *
- * @return The new JavaExport model object
- */
- JavaExport createJavaExport();
-}
+/* + * 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.java; + +/** + * Base Java Import/Export model factory + * + * @version $Rev$ $Date$ + */ +public interface JavaImportExportFactory { + + /** + * Create a java import model object + * + * @return The new JavaImport model object + */ + JavaImport createJavaImport(); + + /** + * Create a java export model object + * + * @return The new JavaExport model object + */ + JavaExport createJavaExport(); +} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java index 11e13ced11..811c5b7010 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java @@ -1,159 +1,159 @@ -/*
- * 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.java.impl;
-
-import java.lang.ref.WeakReference;
-import java.lang.reflect.Constructor;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.tuscany.sca.contribution.Contribution;
-import org.apache.tuscany.sca.contribution.resolver.ClassReference;
-import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
-import org.apache.tuscany.sca.core.FactoryExtensionPoint;
-import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
-
-/**
- * A Model Resolver for ClassReferences.
- * @deprecated Use ClassLoaderModelResolver instead
- * @version $Rev: 557916 $ $Date: 2007-07-20 01:04:40 -0700 (Fri, 20 Jul 2007) $
- */
-@Deprecated()
-public class ClassReferenceModelResolver implements ModelResolver {
- private Contribution contribution;
- private WeakReference<ClassLoader> classLoader;
- private Map<String, ClassReference> map = new HashMap<String, ClassReference>();
-
- private ModelResolver osgiResolver;
-
- public ClassReferenceModelResolver(final Contribution contribution, FactoryExtensionPoint modelFactories) {
- this.contribution = contribution;
- if (this.contribution != null) {
- // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy.
- // ClassLoader cl = contribution.getClassLoader();
- ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
- public ClassLoader run() {
- return contribution.getClassLoader();
- }
- });
-
- if (cl == null) {
- // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy.
- //ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
- // public ClassLoader run() {
- // return Thread.currentThread().getContextClassLoader();
- // }
- //});
- ClassLoader contextClassLoader = ServiceDiscovery.getInstance().getServiceDiscoverer().getClass().getClassLoader();
- cl = new ContributionClassLoader(contribution, contextClassLoader);
- contribution.setClassLoader(cl);
- }
- this.classLoader = new WeakReference<ClassLoader>(cl);
- } else {
- // This path should be used only for unit testing.
- // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy.
- // this.classLoader = new WeakReference<ClassLoader>(this.getClass().getClassLoader());
- ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
- public ClassLoader run() {
- return this.getClass().getClassLoader();
- }
- });
- this.classLoader = new WeakReference<ClassLoader>( cl );
- }
-
- try {
- Class<?> osgiResolverClass =
- Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver");
- if (osgiResolverClass != null) {
- Constructor<?> constructor =
- osgiResolverClass.getConstructor(Contribution.class, FactoryExtensionPoint.class);
- this.osgiResolver = (ModelResolver)constructor.newInstance(contribution, modelFactories);
- }
- } catch (Throwable e) {
- // Ignore error, non-OSGi classloading is used in this case
- }
- }
-
- public void addModel(Object resolved) {
- ClassReference clazz = (ClassReference)resolved;
- map.put(clazz.getClassName(), clazz);
- }
-
- public Object removeModel(Object resolved) {
- return map.remove(((ClassReference)resolved).getClassName());
- }
-
-
-
- public <T> T resolveModel(Class<T> modelClass, T unresolved) {
- if (!(unresolved instanceof ClassReference)) {
- return unresolved;
- }
- Object resolved = map.get(((ClassReference)unresolved).getClassName());
-
- if (resolved != null) {
- return modelClass.cast(resolved);
- }
-
- //Load a class on demand
- Class<?> clazz = null;
-
- if (osgiResolver != null) {
- resolved = osgiResolver.resolveModel(modelClass, unresolved);
- clazz = ((ClassReference)resolved).getJavaClass();
- }
-
- if (clazz == null) {
- try {
- // Search contribution ClassLoader (which has visibility of classes in the contribution
- // as well as explicitly imported packages from other contributions)
- clazz = Class.forName(((ClassReference)unresolved).getClassName(), true, classLoader.get());
- } catch (ClassNotFoundException e) {
- } catch (NoClassDefFoundError e) {
- }
- }
-
- if (clazz != null) {
- //if we load the class
- // Store a new ClassReference wrapping the loaded class
- ClassReference classReference = new ClassReference(clazz);
- map.put(clazz.getName(), classReference);
-
- // Return the resolved ClassReference
- return modelClass.cast(classReference);
- } else {
- return unresolved;
- }
-
- }
-
-
- /***************
- * Helper methods
- ***************/
-
- private String getPackageName(ClassReference clazz) {
- int pos = clazz.getClassName().lastIndexOf(".");
- return clazz.getClassName().substring(0, pos);
- }
-
-}
+/* + * 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.java.impl; + +import java.lang.ref.WeakReference; +import java.lang.reflect.Constructor; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + +/** + * A Model Resolver for ClassReferences. + * @deprecated Use ClassLoaderModelResolver instead + * @version $Rev$ $Date$ + */ +@Deprecated() +public class ClassReferenceModelResolver implements ModelResolver { + private Contribution contribution; + private WeakReference<ClassLoader> classLoader; + private Map<String, ClassReference> map = new HashMap<String, ClassReference>(); + + private ModelResolver osgiResolver; + + public ClassReferenceModelResolver(final Contribution contribution, FactoryExtensionPoint modelFactories) { + this.contribution = contribution; + if (this.contribution != null) { + // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy. + // ClassLoader cl = contribution.getClassLoader(); + ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return contribution.getClassLoader(); + } + }); + + if (cl == null) { + // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy. + //ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + // public ClassLoader run() { + // return Thread.currentThread().getContextClassLoader(); + // } + //}); + ClassLoader contextClassLoader = ServiceDiscovery.getInstance().getServiceDiscoverer().getClass().getClassLoader(); + cl = new ContributionClassLoader(contribution, contextClassLoader); + contribution.setClassLoader(cl); + } + this.classLoader = new WeakReference<ClassLoader>(cl); + } else { + // This path should be used only for unit testing. + // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy. + // this.classLoader = new WeakReference<ClassLoader>(this.getClass().getClassLoader()); + ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return this.getClass().getClassLoader(); + } + }); + this.classLoader = new WeakReference<ClassLoader>( cl ); + } + + try { + Class<?> osgiResolverClass = + Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver"); + if (osgiResolverClass != null) { + Constructor<?> constructor = + osgiResolverClass.getConstructor(Contribution.class, FactoryExtensionPoint.class); + this.osgiResolver = (ModelResolver)constructor.newInstance(contribution, modelFactories); + } + } catch (Throwable e) { + // Ignore error, non-OSGi classloading is used in this case + } + } + + public void addModel(Object resolved) { + ClassReference clazz = (ClassReference)resolved; + map.put(clazz.getClassName(), clazz); + } + + public Object removeModel(Object resolved) { + return map.remove(((ClassReference)resolved).getClassName()); + } + + + + public <T> T resolveModel(Class<T> modelClass, T unresolved) { + if (!(unresolved instanceof ClassReference)) { + return unresolved; + } + Object resolved = map.get(((ClassReference)unresolved).getClassName()); + + if (resolved != null) { + return modelClass.cast(resolved); + } + + //Load a class on demand + Class<?> clazz = null; + + if (osgiResolver != null) { + resolved = osgiResolver.resolveModel(modelClass, unresolved); + clazz = ((ClassReference)resolved).getJavaClass(); + } + + if (clazz == null) { + try { + // Search contribution ClassLoader (which has visibility of classes in the contribution + // as well as explicitly imported packages from other contributions) + clazz = Class.forName(((ClassReference)unresolved).getClassName(), true, classLoader.get()); + } catch (ClassNotFoundException e) { + } catch (NoClassDefFoundError e) { + } + } + + if (clazz != null) { + //if we load the class + // Store a new ClassReference wrapping the loaded class + ClassReference classReference = new ClassReference(clazz); + map.put(clazz.getName(), classReference); + + // Return the resolved ClassReference + return modelClass.cast(classReference); + } else { + return unresolved; + } + + } + + + /*************** + * Helper methods + ***************/ + + private String getPackageName(ClassReference clazz) { + int pos = clazz.getClassName().lastIndexOf("."); + return clazz.getClassName().substring(0, pos); + } + +} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java index 97d8dcff73..25b3eae350 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java @@ -1,386 +1,386 @@ -/*
- * 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.java.impl;
-
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashSet;
-
-import org.apache.tuscany.sca.contribution.Contribution;
-import org.apache.tuscany.sca.contribution.Export;
-import org.apache.tuscany.sca.contribution.Import;
-import org.apache.tuscany.sca.contribution.java.JavaImport;
-
-
-public class ContributionClassLoader extends URLClassLoader {
-// public class ContributionClassLoader {
-
- private Contribution contribution;
- // private b urlClassLoader;
-
- /**
- * Constructor for contribution ClassLoader
- *
- * @param contribution
- * @param parent
- * @throws MalformedURLException
- */
- public ContributionClassLoader(Contribution contribution, final ClassLoader parent) {
- super(new URL[0], parent);
- // Note that privileged use of getContextClassLoader have been promoted to callers.
- // super(new URL[0], parent == null?Thread.currentThread().getContextClassLoader(): null);
- this.contribution = contribution;
- if (contribution.getLocation() != null) {
- try {
- this.addURL(new URL(contribution.getLocation()));
- for (URL url : ContributionHelper.getNestedJarUrls(contribution)) {
- this.addURL(url);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- /*
- * Return the ClassLoader corresponding to a contribution providing an export
- * Create a new ClassLoader for the contribution if one does not exist
- */
- private ClassLoader getExportClassLoader(Contribution exportingContribution) {
- ClassLoader cl = exportingContribution.getClassLoader();
- if (!(cl instanceof ContributionClassLoader)) {
- if (cl == null) {
- cl = getParent();
- }
-
- cl = new ContributionClassLoader(exportingContribution, cl);
- exportingContribution.setClassLoader(cl);
- }
- return cl;
- }
-
- /* (non-Javadoc)
- * @see java.net.URLClassLoader#findClass(java.lang.String)
- *
- * Search path for class:
- * This contribution
- * Imported contributions
- */
- @Override
- protected Class<?> findClass(String className) throws ClassNotFoundException {
-
- Class<?> clazz = null;
- try {
- clazz = findClassFromContribution(className);
- } catch (ClassNotFoundException e) {
-
- for (Import import_ : this.contribution.getImports()) {
- if (classNameMatchesImport(className, import_)) {
- // Delegate the resolution to the imported contribution
- for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) {
-
- ClassLoader exportClassLoader = getExportClassLoader(exportingContribution);
- if (exportClassLoader instanceof ContributionClassLoader) {
-
- for (Export export : exportingContribution.getExports()) {
- try {
- if (import_.match(export)) {
- clazz = ((ContributionClassLoader)exportClassLoader).findClassFromContribution(className);
- break;
- }
- } catch (ClassNotFoundException e1) {
- continue;
- }
-
- }
- if (clazz != null) break;
- }
- }
- if (clazz != null) break;
- }
- }
-
- if (clazz == null) throw e;
- }
- return clazz;
- }
-
-
- /* (non-Javadoc)
- * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
- *
- * Search path for class:
- * Parent ClassLoader
- * This contribution
- * Imported contributions
- *
- */
- @Override
- protected synchronized Class<?> loadClass(String className, boolean resolveClass)
- throws ClassNotFoundException {
-
- Class<?> clazz = null;
- try {
-
- if (this.getParent() != null)
- clazz = this.getParent().loadClass(className);
-
- } catch (ClassNotFoundException e) {
- }
-
- if (clazz == null)
- clazz = findClass(className);
-
-
- if (resolveClass)
- this.resolveClass(clazz);
- return clazz;
-
- }
-
-
-
- /*
- * (non-Javadoc)
- *
- * @see java.net.URLClassLoader#findResource(java.lang.String)
- */
- @Override
- public URL findResource(String name) {
-
- URL url = findResourceFromContribution(name);
-
- if (url == null) {
- for (Import import_ : this.contribution.getImports()) {
- if (resourceNameMatchesImport(name, import_)) {
- // Delegate the resolution to the imported contribution
- for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) {
-
- ClassLoader exportClassLoader = getExportClassLoader(exportingContribution);
- if (exportClassLoader instanceof ContributionClassLoader) {
-
- for (Export export : exportingContribution.getExports()) {
- if (import_.match(export)) {
- url = ((ContributionClassLoader)exportClassLoader).findResourceFromContribution(name);
- if (url != null) break;
- }
- }
- if (url != null) break;
- }
- }
- if (url != null) break;
- }
- }
-
- }
- return url;
- }
-
-
- /* (non-Javadoc)
- * @see java.net.URLClassLoader#findResources(java.lang.String)
- */
- @Override
- public Enumeration<URL> findResources(String name) throws IOException {
-
- return Collections.enumeration(findResourceSet(name));
- }
-
-
-
-
- /* (non-Javadoc)
- * @see java.lang.ClassLoader#getResource(java.lang.String)
- *
- * Find a resource.
- * Search path for resource:
- * Parent ClassLoader
- * This contribution
- * Imported contributions
- */
- @Override
- public URL getResource(String resName) {
-
- URL resource = null;
-
- if (this.getParent() != null) {
- resource = this.getParent().getResource(resName);
- }
- if (resource == null)
- resource = findResource(resName);
-
- return resource;
- }
-
-
-
- /* (non-Javadoc)
- * @see java.lang.ClassLoader#getResources(java.lang.String)
- *
- * Return list of resources from this contribution, resources
- * imported through imported contributions and resources from parent
- * ClassLoader.
- */
- @Override
- public Enumeration<URL> getResources(String resName) throws IOException {
-
- HashSet<URL> resourceSet = findResourceSet(resName);
- addEnumerationToCollection(resourceSet, super.getResources(resName));
-
- return Collections.enumeration(resourceSet);
- }
-
-
- /*
- * Find set of resources
- */
- private HashSet<URL> findResourceSet(String name) throws IOException {
-
- HashSet<URL> resources = new HashSet<URL>();
-
- addEnumerationToCollection(resources, super.findResources(name));
-
- for (Import import_ : this.contribution.getImports()) {
- if (!(import_ instanceof JavaImport)) {
- continue;
- }
- if (resourceNameMatchesImport(name, import_)) {
- // Delegate the resolution to the imported contribution
- for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) {
-
- ClassLoader exportClassLoader = getExportClassLoader(exportingContribution);
- if (exportClassLoader instanceof ContributionClassLoader) {
-
- for (Export export : exportingContribution.getExports()) {
- if (import_.match(export)) {
- addEnumerationToCollection(resources,
- ((ContributionClassLoader)exportClassLoader).findResources(name));
- }
- }
- }
- }
- }
- }
-
- return resources;
- }
-
-
- /*
- * Find class from contribution. If class has already been loaded, return loaded class.
- */
- private Class<?> findClassFromContribution(String className) throws ClassNotFoundException {
-
- Class<?> clazz = findLoadedClass(className);
- if (clazz == null)
- clazz = super.findClass(className);
- return clazz;
-
- }
-
- /*
- * Find resource from contribution.
- */
- private URL findResourceFromContribution(String name) {
-
- return super.findResource(name);
- }
-
- /**
- * Check if a class name matches an import statement.
- * Class matches if the package name used in <import.java/> matches
- *
- * @param name Name of class
- * @param import_ SCA contribution import
- * @return true if this is a matching import
- */
- private boolean classNameMatchesImport(String name, Import import_) {
-
- if (import_ instanceof JavaImport && name != null && name.lastIndexOf('.') > 0) {
- JavaImport javaImport = (JavaImport) import_;
-
- String packageName = name.substring(0, name.lastIndexOf('.'));
- if (javaImport.getPackage().endsWith(".*")) {
- String prefix = javaImport.getPackage().substring(0, javaImport.getPackage().length() -1);
- if (packageName.startsWith(prefix)) {
- return true;
- }
- } else {
- return packageName.equals(javaImport.getPackage());
- }
- }
-
- return false;
- }
-
- /**
- * Check if a resource name matches an import statement.
- * Resource matches if package/namespace match the directory of resource.
- *
- * @param name Name of resource
- * @param import_ SCA contribution import
- * @return true if this is a matching import
- */
- private boolean resourceNameMatchesImport(String name, Import import_) {
-
-
- if (name == null || name.lastIndexOf('/') <= 0)
- return false;
- else if (import_ instanceof JavaImport) {
- JavaImport javaImport = (JavaImport) import_;
-
- if (javaImport.getPackage().endsWith(".*")) {
- String packageName = name.substring(0, name.lastIndexOf('/')).replace('/', '.');
- String prefix = javaImport.getPackage().substring(0, javaImport.getPackage().length() -1);
- if (packageName.startsWith(prefix)) {
- return true;
- }
- } else {
- // 'name' is a resource : contains "/" as separators
- // Get package name from resource name
- String packageName = name.substring(0, name.lastIndexOf('/'));
- return packageName.equals(javaImport.getPackage().replaceAll("\\.", "/"));
- }
- }
- return false;
- }
-
- /*
- * Add an enumeration to a Collection
- */
- private <T extends Object> void addEnumerationToCollection(Collection<T> collection, Enumeration<T> enumeration) {
-
- while (enumeration.hasMoreElements())
- collection.add(enumeration.nextElement());
- }
-
-
- @Override
- public String toString() {
- return "SCA Contribution ClassLoader location: " + contribution.getLocation() + " parent ClassLoader: " + getParent();
- }
-
-
-}
+/* + * 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.java.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.java.JavaImport; + + +public class ContributionClassLoader extends URLClassLoader { +// public class ContributionClassLoader { + + private Contribution contribution; + // private b urlClassLoader; + + /** + * Constructor for contribution ClassLoader + * + * @param contribution + * @param parent + * @throws MalformedURLException + */ + public ContributionClassLoader(Contribution contribution, final ClassLoader parent) { + super(new URL[0], parent); + // Note that privileged use of getContextClassLoader have been promoted to callers. + // super(new URL[0], parent == null?Thread.currentThread().getContextClassLoader(): null); + this.contribution = contribution; + if (contribution.getLocation() != null) { + try { + this.addURL(new URL(contribution.getLocation())); + for (URL url : ContributionHelper.getNestedJarUrls(contribution)) { + this.addURL(url); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + /* + * Return the ClassLoader corresponding to a contribution providing an export + * Create a new ClassLoader for the contribution if one does not exist + */ + private ClassLoader getExportClassLoader(Contribution exportingContribution) { + ClassLoader cl = exportingContribution.getClassLoader(); + if (!(cl instanceof ContributionClassLoader)) { + if (cl == null) { + cl = getParent(); + } + + cl = new ContributionClassLoader(exportingContribution, cl); + exportingContribution.setClassLoader(cl); + } + return cl; + } + + /* (non-Javadoc) + * @see java.net.URLClassLoader#findClass(java.lang.String) + * + * Search path for class: + * This contribution + * Imported contributions + */ + @Override + protected Class<?> findClass(String className) throws ClassNotFoundException { + + Class<?> clazz = null; + try { + clazz = findClassFromContribution(className); + } catch (ClassNotFoundException e) { + + for (Import import_ : this.contribution.getImports()) { + if (classNameMatchesImport(className, import_)) { + // Delegate the resolution to the imported contribution + for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) { + + ClassLoader exportClassLoader = getExportClassLoader(exportingContribution); + if (exportClassLoader instanceof ContributionClassLoader) { + + for (Export export : exportingContribution.getExports()) { + try { + if (import_.match(export)) { + clazz = ((ContributionClassLoader)exportClassLoader).findClassFromContribution(className); + break; + } + } catch (ClassNotFoundException e1) { + continue; + } + + } + if (clazz != null) break; + } + } + if (clazz != null) break; + } + } + + if (clazz == null) throw e; + } + return clazz; + } + + + /* (non-Javadoc) + * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) + * + * Search path for class: + * Parent ClassLoader + * This contribution + * Imported contributions + * + */ + @Override + protected synchronized Class<?> loadClass(String className, boolean resolveClass) + throws ClassNotFoundException { + + Class<?> clazz = null; + try { + + if (this.getParent() != null) + clazz = this.getParent().loadClass(className); + + } catch (ClassNotFoundException e) { + } + + if (clazz == null) + clazz = findClass(className); + + + if (resolveClass) + this.resolveClass(clazz); + return clazz; + + } + + + + /* + * (non-Javadoc) + * + * @see java.net.URLClassLoader#findResource(java.lang.String) + */ + @Override + public URL findResource(String name) { + + URL url = findResourceFromContribution(name); + + if (url == null) { + for (Import import_ : this.contribution.getImports()) { + if (resourceNameMatchesImport(name, import_)) { + // Delegate the resolution to the imported contribution + for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) { + + ClassLoader exportClassLoader = getExportClassLoader(exportingContribution); + if (exportClassLoader instanceof ContributionClassLoader) { + + for (Export export : exportingContribution.getExports()) { + if (import_.match(export)) { + url = ((ContributionClassLoader)exportClassLoader).findResourceFromContribution(name); + if (url != null) break; + } + } + if (url != null) break; + } + } + if (url != null) break; + } + } + + } + return url; + } + + + /* (non-Javadoc) + * @see java.net.URLClassLoader#findResources(java.lang.String) + */ + @Override + public Enumeration<URL> findResources(String name) throws IOException { + + return Collections.enumeration(findResourceSet(name)); + } + + + + + /* (non-Javadoc) + * @see java.lang.ClassLoader#getResource(java.lang.String) + * + * Find a resource. + * Search path for resource: + * Parent ClassLoader + * This contribution + * Imported contributions + */ + @Override + public URL getResource(String resName) { + + URL resource = null; + + if (this.getParent() != null) { + resource = this.getParent().getResource(resName); + } + if (resource == null) + resource = findResource(resName); + + return resource; + } + + + + /* (non-Javadoc) + * @see java.lang.ClassLoader#getResources(java.lang.String) + * + * Return list of resources from this contribution, resources + * imported through imported contributions and resources from parent + * ClassLoader. + */ + @Override + public Enumeration<URL> getResources(String resName) throws IOException { + + HashSet<URL> resourceSet = findResourceSet(resName); + addEnumerationToCollection(resourceSet, super.getResources(resName)); + + return Collections.enumeration(resourceSet); + } + + + /* + * Find set of resources + */ + private HashSet<URL> findResourceSet(String name) throws IOException { + + HashSet<URL> resources = new HashSet<URL>(); + + addEnumerationToCollection(resources, super.findResources(name)); + + for (Import import_ : this.contribution.getImports()) { + if (!(import_ instanceof JavaImport)) { + continue; + } + if (resourceNameMatchesImport(name, import_)) { + // Delegate the resolution to the imported contribution + for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) { + + ClassLoader exportClassLoader = getExportClassLoader(exportingContribution); + if (exportClassLoader instanceof ContributionClassLoader) { + + for (Export export : exportingContribution.getExports()) { + if (import_.match(export)) { + addEnumerationToCollection(resources, + ((ContributionClassLoader)exportClassLoader).findResources(name)); + } + } + } + } + } + } + + return resources; + } + + + /* + * Find class from contribution. If class has already been loaded, return loaded class. + */ + private Class<?> findClassFromContribution(String className) throws ClassNotFoundException { + + Class<?> clazz = findLoadedClass(className); + if (clazz == null) + clazz = super.findClass(className); + return clazz; + + } + + /* + * Find resource from contribution. + */ + private URL findResourceFromContribution(String name) { + + return super.findResource(name); + } + + /** + * Check if a class name matches an import statement. + * Class matches if the package name used in <import.java/> matches + * + * @param name Name of class + * @param import_ SCA contribution import + * @return true if this is a matching import + */ + private boolean classNameMatchesImport(String name, Import import_) { + + if (import_ instanceof JavaImport && name != null && name.lastIndexOf('.') > 0) { + JavaImport javaImport = (JavaImport) import_; + + String packageName = name.substring(0, name.lastIndexOf('.')); + if (javaImport.getPackage().endsWith(".*")) { + String prefix = javaImport.getPackage().substring(0, javaImport.getPackage().length() -1); + if (packageName.startsWith(prefix)) { + return true; + } + } else { + return packageName.equals(javaImport.getPackage()); + } + } + + return false; + } + + /** + * Check if a resource name matches an import statement. + * Resource matches if package/namespace match the directory of resource. + * + * @param name Name of resource + * @param import_ SCA contribution import + * @return true if this is a matching import + */ + private boolean resourceNameMatchesImport(String name, Import import_) { + + + if (name == null || name.lastIndexOf('/') <= 0) + return false; + else if (import_ instanceof JavaImport) { + JavaImport javaImport = (JavaImport) import_; + + if (javaImport.getPackage().endsWith(".*")) { + String packageName = name.substring(0, name.lastIndexOf('/')).replace('/', '.'); + String prefix = javaImport.getPackage().substring(0, javaImport.getPackage().length() -1); + if (packageName.startsWith(prefix)) { + return true; + } + } else { + // 'name' is a resource : contains "/" as separators + // Get package name from resource name + String packageName = name.substring(0, name.lastIndexOf('/')); + return packageName.equals(javaImport.getPackage().replaceAll("\\.", "/")); + } + } + return false; + } + + /* + * Add an enumeration to a Collection + */ + private <T extends Object> void addEnumerationToCollection(Collection<T> collection, Enumeration<T> enumeration) { + + while (enumeration.hasMoreElements()) + collection.add(enumeration.nextElement()); + } + + + @Override + public String toString() { + return "SCA Contribution ClassLoader location: " + contribution.getLocation() + " parent ClassLoader: " + getParent(); + } + + +} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java index e5dc397f4e..3bf9e54774 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java @@ -1,41 +1,41 @@ -/*
- * 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.java.impl;
-
-import org.apache.tuscany.sca.contribution.java.JavaExport;
-import org.apache.tuscany.sca.contribution.java.JavaImport;
-import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory;
-
-/**
- * Java Import/Export Factory implementation
- *
- * @version $Rev$ $Date$
- */
-public class JavaImportExportFactoryImpl implements JavaImportExportFactory {
-
- public JavaImport createJavaImport() {
- return new JavaImportImpl();
- }
-
- public JavaExport createJavaExport() {
- return new JavaExportImpl();
- }
-
-}
+/* + * 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.java.impl; + +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; + +/** + * Java Import/Export Factory implementation + * + * @version $Rev$ $Date$ + */ +public class JavaImportExportFactoryImpl implements JavaImportExportFactory { + + public JavaImport createJavaImport() { + return new JavaImportImpl(); + } + + public JavaExport createJavaExport() { + return new JavaExportImpl(); + } + +} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedArtifactProcessor.java index 98d3fcca88..952adbc4e3 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedArtifactProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedArtifactProcessor.java @@ -1,40 +1,40 @@ -/*
- * 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;
-
-import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
-
-/**
- * Interface for extended Artifact Processors which require a pre-resolve phase prior to the resolve phase
- *
- * @version $Rev: 704156 $ $Date: 2008-10-13 17:31:59 +0100 (Mon, 13 Oct 2008) $
- */
-public interface ExtendedArtifactProcessor<M> extends ArtifactProcessor<M> {
-
- /**
- * Pre-resolve references from this model to other models. Used for models where initial setup of
- * the resolve phase is required. An example is Contribution models with imports and exports which must
- * be set up prior to the main resolve phase
- *
- * @param model The model to resolve
- * @param resolver The resolver to use to resolve referenced models
- */
- void preResolve(M model, ModelResolver resolver) throws ContributionResolveException;
-
-} // end interface
+/* + * 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; + +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * Interface for extended Artifact Processors which require a pre-resolve phase prior to the resolve phase + * + * @version $Rev$ $Date$ + */ +public interface ExtendedArtifactProcessor<M> extends ArtifactProcessor<M> { + + /** + * Pre-resolve references from this model to other models. Used for models where initial setup of + * the resolve phase is required. An example is Contribution models with imports and exports which must + * be set up prior to the main resolve phase + * + * @param model The model to resolve + * @param resolver The resolver to use to resolve referenced models + */ + void preResolve(M model, ModelResolver resolver) throws ContributionResolveException; + +} // end interface diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedURLArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedURLArtifactProcessor.java index 872b9804ce..2639a21e41 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedURLArtifactProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtendedURLArtifactProcessor.java @@ -1,30 +1,30 @@ -/*
- * 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;
-
-
-/**
- * An extended artifact processor that can read models from a URL.
- *
- * @version $Rev: 704156 $ $Date: 2008-10-13 17:31:59 +0100 (Mon, 13 Oct 2008) $
- */
-public interface ExtendedURLArtifactProcessor<M> extends ExtendedArtifactProcessor<M>, URLArtifactProcessor<M> {
-
-} // end interface
+/* + * 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; + + +/** + * An extended artifact processor that can read models from a URL. + * + * @version $Rev$ $Date$ + */ +public interface ExtendedURLArtifactProcessor<M> extends ExtendedArtifactProcessor<M>, URLArtifactProcessor<M> { + +} // end interface diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java index 7e8b3a9d9e..634563ebd7 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLDocumentStreamReader.java @@ -33,7 +33,7 @@ import javax.xml.stream.XMLStreamReader; * create a XMLStreamReader representing a document and it will produce * START_DOCUMENT, END_DOCUMENT events. * - * @version $Rev: 659153 $ $Date: 2008-05-22 09:43:58 -0700 (Thu, 22 May 2008) $ + * @version $Rev$ $Date$ */ public class XMLDocumentStreamReader implements XMLStreamReader { private static final int STATE_COMPLETE_AT_NEXT = 2; // The wrapper diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java index c6facd2b02..5cbf37cf70 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/DefaultImportAllModelResolver.java @@ -1,80 +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;
- }
-
-}
+/* + * 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$ $Date$ + */ +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; + } + +} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java index 2394f4510b..dfe8f08719 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/resolver/ResourceReference.java @@ -1,103 +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;
- }
- }
- }
-
-}
+/* + * 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; + } + } + } + +} diff --git a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java index 68a2c771cf..b101c2f1b6 100644 --- a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java +++ b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java @@ -1,75 +1,75 @@ - /*
- * 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.java.impl;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import org.apache.tuscany.sca.contribution.resolver.ClassReference;
-import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver;
-import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint;
-import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
-import org.apache.tuscany.sca.core.ExtensionPointRegistry;
-import org.apache.tuscany.sca.core.FactoryExtensionPoint;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- * Test ClassReferenceArtifactResolver.
- *
- * @version $Rev: 560435 $ $Date: 2007-07-27 18:26:55 -0700 (Fri, 27 Jul 2007) $
- */
-public class ClassReferenceArtifactResolverTestCase {
- private static ExtensibleModelResolver resolver;
-
- @BeforeClass
- public static void setUp() throws Exception {
- ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry();
-
- ModelResolverExtensionPoint resolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class);
- resolvers.addResolver(ClassReference.class, ClassReferenceModelResolver.class);
- FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
- resolver = new ExtensibleModelResolver(null, resolvers, modelFactories);
- }
-
- /**
- * Test ClassReference resolution
- *
- */
- @Test
- public void testResolveClass() {
- ClassReference ref = new ClassReference(getClass().getName());
- ClassReference clazz = resolver.resolveModel(ClassReference.class, ref);
- assertFalse(clazz.isUnresolved());
- assertTrue(clazz.getJavaClass() == getClass());
- }
-
- /**
- * Test ClassReference resolution of inexistent class
- *
- */
- @Test
- public void testUnresolvedClass() {
- ClassReference ref = new ClassReference("NonExistentClass");
- ClassReference clazz = resolver.resolveModel(ClassReference.class, ref);
- assertTrue(clazz.isUnresolved());
- assertTrue(clazz.getJavaClass() == null);
- }
-}
+ /* + * 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.java.impl; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test ClassReferenceArtifactResolver. + * + * @version $Rev$ $Date$ + */ +public class ClassReferenceArtifactResolverTestCase { + private static ExtensibleModelResolver resolver; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + + ModelResolverExtensionPoint resolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); + resolvers.addResolver(ClassReference.class, ClassReferenceModelResolver.class); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + resolver = new ExtensibleModelResolver(null, resolvers, modelFactories); + } + + /** + * Test ClassReference resolution + * + */ + @Test + public void testResolveClass() { + ClassReference ref = new ClassReference(getClass().getName()); + ClassReference clazz = resolver.resolveModel(ClassReference.class, ref); + assertFalse(clazz.isUnresolved()); + assertTrue(clazz.getJavaClass() == getClass()); + } + + /** + * Test ClassReference resolution of inexistent class + * + */ + @Test + public void testUnresolvedClass() { + ClassReference ref = new ClassReference("NonExistentClass"); + ClassReference clazz = resolver.resolveModel(ClassReference.class, ref); + assertTrue(clazz.isUnresolved()); + assertTrue(clazz.getJavaClass() == null); + } +} diff --git a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java index 779c00f71c..8409dd9491 100644 --- a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java +++ b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java @@ -1,262 +1,262 @@ -/*
- * 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.java.impl;
-
-import java.io.File;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.ArrayList;
-
-import org.apache.tuscany.sca.contribution.Contribution;
-import org.apache.tuscany.sca.contribution.ContributionFactory;
-import org.apache.tuscany.sca.contribution.java.JavaExport;
-import org.apache.tuscany.sca.contribution.java.JavaImport;
-import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory;
-import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
-import org.apache.tuscany.sca.core.ExtensionPointRegistry;
-import org.apache.tuscany.sca.core.FactoryExtensionPoint;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-
-/**
- * Test ContributionClassLoader.
- *
- */
-public class ContributionClassLoaderTestCase {
-
- private static ContributionFactory contributionFactory;
- private static JavaImportExportFactory javaImportExportFactory;
-
- @BeforeClass
- public static void setUp() throws Exception {
- ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry();
- FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
- contributionFactory = modelFactories.getFactory(ContributionFactory.class);
- javaImportExportFactory = modelFactories.getFactory(JavaImportExportFactory.class);
- }
-
- private Contribution createContribution(String fileName) throws MalformedURLException {
- Contribution contrib = contributionFactory.createContribution();
- File contribDir = new File(fileName);
- contrib.setLocation(contribDir.toURI().toURL().toString());
- ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
- public ClassLoader run() {
- return Thread.currentThread().getContextClassLoader();
- }
- });
- contrib.setClassLoader(new ContributionClassLoader(contrib, contextClassLoader));
- return contrib;
- }
-
-
- @Test
- public void testClassLoadingFromContribution() throws ClassNotFoundException, MalformedURLException {
-
- Contribution contribA = createContribution("target/test-classes");
- Contribution contribB = createContribution("target");
- Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
-
- // Class present in contribution, also in parent. Class is loaded from parent
- Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName());
- Assert.assertNotNull(testClassA);
- Assert.assertSame(this.getClass(), testClassA);
-
- // Class not present in contribution, but present in parent ClassLoader
- Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName());
- Assert.assertNotNull(testClassB);
- Assert.assertSame(this.getClass(), testClassB);
-
- // Class present in contribution, but not in parent
- Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService");
- Assert.assertNotNull(testClassC);
-
- // Class not present in contribution or in parent
- try {
- contribA.getClassLoader().loadClass("NonExistent");
-
- Assert.assertTrue("ClassNotFoundException not thrown as expected", false);
-
- } catch (ClassNotFoundException e) {
- }
-
-
-
- }
-
- @Test
- public void testResourceLoadingFromContribution() throws ClassNotFoundException, MalformedURLException {
-
- Contribution contribA = createContribution("target/test-classes");
- Contribution contribB = createContribution("target");
- Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
-
- // Resource present in contribution, and in parent
- URL resA = contribA.getClassLoader().getResource("deployables/sample-calculator.jar");
- Assert.assertNotNull(resA);
-
- // Resource not present in contribution, but present in parent ClassLoader
- URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar");
- Assert.assertNotNull(resB);
-
- // Resource present in contribution, but not in parent
- URL resC = contribC.getClassLoader().getResource("calculator/AddService.class");
- Assert.assertNotNull(resC);
-
- // Load Java class as resource from parent
- String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class";
- URL classResA = contribA.getClassLoader().getResource(classResName);
- Assert.assertNotNull(classResA);
-
- // Non-existent resource
- URL res = contribA.getClassLoader().getResource("deployables/NonExistent");
- Assert.assertNull(res);
-
- }
-
- private static String getPackageName(Class<?> cls) {
- String name = cls.getName();
- int index = name.lastIndexOf('.');
- return index == -1 ? "" : name.substring(0, index);
- }
-
- @Test
- public void testClassLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException {
-
- Contribution contribA = createContribution("target/test-classes");
- Contribution contribB = createContribution("target");
- Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
- ArrayList<Contribution> exportContribList = new ArrayList<Contribution>();
- exportContribList.add(contribA);
- exportContribList.add(contribC);
-
- JavaImport import_ = javaImportExportFactory.createJavaImport();
- import_.setPackage(getPackageName(getClass()));
- import_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
- contribB.getImports().add(import_);
- import_ = javaImportExportFactory.createJavaImport();
- import_.setPackage("calculator");
- import_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
- contribB.getImports().add(import_);
-
- JavaExport export = javaImportExportFactory.createJavaExport();
- export.setPackage(getPackageName(getClass()));
- contribA.getExports().add(export);
- export = javaImportExportFactory.createJavaExport();
- export.setPackage("calculator");
- contribC.getExports().add(export);
-
- // Load class from parent, class is also present in imported contribution. Class should
- // be loaded from parent
- Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName());
- Assert.assertNotNull(testClassB);
- Assert.assertSame(this.getClass(), testClassB);
-
- // Load class from parent, class is also present in parent. Class should be loaded
- // from parent.
- Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName());
- Assert.assertNotNull(testClassA);
- Assert.assertSame(this.getClass(), testClassA);
-
- // Imported class should be the same as the one loaded by the exporting contribution
- Assert.assertSame(testClassA, testClassB);
-
- // Load class from imported contribution, class is not present in parent
- Class<?> testClassB1 = contribB.getClassLoader().loadClass("calculator.AddService");
- Assert.assertNotNull(testClassB1);
-
- // Imported class should be the same as the one loaded by the exporting contribution
- Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService");
- Assert.assertNotNull(testClassC);
- Assert.assertSame(testClassC, testClassB1);
-
-
- // Try to load class from package which is not explicitly imported - should throw ClassNotFoundException
- try {
- contribA.getClassLoader().loadClass("calculator.AddService");
-
- Assert.assertTrue("ClassNotFoundException not thrown as expected", false);
-
- } catch (ClassNotFoundException e) {
- }
-
- // Try to load non-existent class from imported package - should throw ClassNotFoundException
- try {
- contribB.getClassLoader().loadClass(getPackageName(getClass()) + ".NonExistentClass");
-
- Assert.assertTrue("ClassNotFoundException not thrown as expected", false);
-
- } catch (ClassNotFoundException e) {
- }
-
- }
-
- @Test
- public void testResourceLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException {
-
- Contribution contribA = createContribution("target/test-classes");
- Contribution contribB = createContribution("target");
- Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar");
-
- ArrayList<Contribution> exportContribList = new ArrayList<Contribution>();
- exportContribList.add(contribA);
- exportContribList.add(contribC);
-
- JavaImport import_ = javaImportExportFactory.createJavaImport();
- import_.setPackage(getPackageName(getClass()));
- import_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
- contribB.getImports().add(import_);
- JavaImport import1_ = javaImportExportFactory.createJavaImport();
- import1_.setPackage("calculator");
- import1_.setModelResolver(new JavaImportModelResolver(exportContribList, null));
- contribB.getImports().add(import1_);
-
- JavaExport export = javaImportExportFactory.createJavaExport();
- export.setPackage(getPackageName(getClass()));
- contribA.getExports().add(export);
- JavaExport export1 = javaImportExportFactory.createJavaExport();
- export1.setPackage("calculator");
- contribC.getExports().add(export1);
-
-
- // Load resource from parent
- URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar");
- Assert.assertNotNull(resB);
-
- // Load Java class as resource from imported contribution with JavaImport
- String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class";
- URL classResB = contribB.getClassLoader().getResource(classResName);
- Assert.assertNotNull(classResB);
-
- // Load Java class as resource from imported contribution with JavaImport
- URL classResB1 = contribB.getClassLoader().getResource("calculator/AddService.class");
- Assert.assertNotNull(classResB1);
-
- // Try to load resource not explicitly imported by contribution
- URL classResA1 = contribA.getClassLoader().getResource("calculator/AddService.class");
- Assert.assertNull(classResA1);
-
-
- }
-
-}
+/* + * 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.java.impl; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + + +/** + * Test ContributionClassLoader. + * + */ +public class ContributionClassLoaderTestCase { + + private static ContributionFactory contributionFactory; + private static JavaImportExportFactory javaImportExportFactory; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + contributionFactory = modelFactories.getFactory(ContributionFactory.class); + javaImportExportFactory = modelFactories.getFactory(JavaImportExportFactory.class); + } + + private Contribution createContribution(String fileName) throws MalformedURLException { + Contribution contrib = contributionFactory.createContribution(); + File contribDir = new File(fileName); + contrib.setLocation(contribDir.toURI().toURL().toString()); + ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return Thread.currentThread().getContextClassLoader(); + } + }); + contrib.setClassLoader(new ContributionClassLoader(contrib, contextClassLoader)); + return contrib; + } + + + @Test + public void testClassLoadingFromContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + // Class present in contribution, also in parent. Class is loaded from parent + Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassA); + Assert.assertSame(this.getClass(), testClassA); + + // Class not present in contribution, but present in parent ClassLoader + Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassB); + Assert.assertSame(this.getClass(), testClassB); + + // Class present in contribution, but not in parent + Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassC); + + // Class not present in contribution or in parent + try { + contribA.getClassLoader().loadClass("NonExistent"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + + + } + + @Test + public void testResourceLoadingFromContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + // Resource present in contribution, and in parent + URL resA = contribA.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resA); + + // Resource not present in contribution, but present in parent ClassLoader + URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resB); + + // Resource present in contribution, but not in parent + URL resC = contribC.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNotNull(resC); + + // Load Java class as resource from parent + String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class"; + URL classResA = contribA.getClassLoader().getResource(classResName); + Assert.assertNotNull(classResA); + + // Non-existent resource + URL res = contribA.getClassLoader().getResource("deployables/NonExistent"); + Assert.assertNull(res); + + } + + private static String getPackageName(Class<?> cls) { + String name = cls.getName(); + int index = name.lastIndexOf('.'); + return index == -1 ? "" : name.substring(0, index); + } + + @Test + public void testClassLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + ArrayList<Contribution> exportContribList = new ArrayList<Contribution>(); + exportContribList.add(contribA); + exportContribList.add(contribC); + + JavaImport import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage(getPackageName(getClass())); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage("calculator"); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + + JavaExport export = javaImportExportFactory.createJavaExport(); + export.setPackage(getPackageName(getClass())); + contribA.getExports().add(export); + export = javaImportExportFactory.createJavaExport(); + export.setPackage("calculator"); + contribC.getExports().add(export); + + // Load class from parent, class is also present in imported contribution. Class should + // be loaded from parent + Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassB); + Assert.assertSame(this.getClass(), testClassB); + + // Load class from parent, class is also present in parent. Class should be loaded + // from parent. + Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassA); + Assert.assertSame(this.getClass(), testClassA); + + // Imported class should be the same as the one loaded by the exporting contribution + Assert.assertSame(testClassA, testClassB); + + // Load class from imported contribution, class is not present in parent + Class<?> testClassB1 = contribB.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassB1); + + // Imported class should be the same as the one loaded by the exporting contribution + Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassC); + Assert.assertSame(testClassC, testClassB1); + + + // Try to load class from package which is not explicitly imported - should throw ClassNotFoundException + try { + contribA.getClassLoader().loadClass("calculator.AddService"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + // Try to load non-existent class from imported package - should throw ClassNotFoundException + try { + contribB.getClassLoader().loadClass(getPackageName(getClass()) + ".NonExistentClass"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + } + + @Test + public void testResourceLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + ArrayList<Contribution> exportContribList = new ArrayList<Contribution>(); + exportContribList.add(contribA); + exportContribList.add(contribC); + + JavaImport import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage(getPackageName(getClass())); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + JavaImport import1_ = javaImportExportFactory.createJavaImport(); + import1_.setPackage("calculator"); + import1_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import1_); + + JavaExport export = javaImportExportFactory.createJavaExport(); + export.setPackage(getPackageName(getClass())); + contribA.getExports().add(export); + JavaExport export1 = javaImportExportFactory.createJavaExport(); + export1.setPackage("calculator"); + contribC.getExports().add(export1); + + + // Load resource from parent + URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resB); + + // Load Java class as resource from imported contribution with JavaImport + String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class"; + URL classResB = contribB.getClassLoader().getResource(classResName); + Assert.assertNotNull(classResB); + + // Load Java class as resource from imported contribution with JavaImport + URL classResB1 = contribB.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNotNull(classResB1); + + // Try to load resource not explicitly imported by contribution + URL classResA1 = contribA.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNull(classResA1); + + + } + +} |