diff options
Diffstat (limited to '')
18 files changed, 758 insertions, 341 deletions
diff --git a/branches/sca-java-1.x/itest/contribution-jee/pom.xml b/branches/sca-java-1.x/itest/contribution-jee/pom.xml index b4cae50635..f56f2ce55c 100644 --- a/branches/sca-java-1.x/itest/contribution-jee/pom.xml +++ b/branches/sca-java-1.x/itest/contribution-jee/pom.xml @@ -86,7 +86,7 @@ <dependency> <groupId>org.apache.tuscany.sca</groupId> - <artifactId>itest-contribution-jee-samples</artifactId> + <artifactId>itest-contribution-jee-samples-6</artifactId> <version>1.6-SNAPSHOT</version> </dependency> diff --git a/branches/sca-java-1.x/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/service/impl/ContributionServiceImpl.java b/branches/sca-java-1.x/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/service/impl/ContributionServiceImpl.java index 1c4d8e8547..32d8cf5b74 100644 --- a/branches/sca-java-1.x/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/service/impl/ContributionServiceImpl.java +++ b/branches/sca-java-1.x/modules/contribution-impl/src/main/java/org/apache/tuscany/sca/contribution/service/impl/ContributionServiceImpl.java @@ -55,6 +55,7 @@ import org.apache.tuscany.sca.contribution.service.ContributionException; import org.apache.tuscany.sca.contribution.service.ContributionRepository; import org.apache.tuscany.sca.contribution.service.ContributionService; import org.apache.tuscany.sca.contribution.service.ExtensibleContributionListener; +import org.apache.tuscany.sca.contribution.service.TypeDescriber; import org.apache.tuscany.sca.contribution.service.util.IOHelper; import org.apache.tuscany.sca.contribution.xml.ContributionMetadataDocumentProcessor; import org.apache.tuscany.sca.core.ExtensionPointRegistry; @@ -135,7 +136,9 @@ public class ContributionServiceImpl implements ContributionService { private Monitor monitor; - private String COMPOSITE_FILE_EXTN = ".composite"; + private String COMPOSITE_FILE_EXTN = ".composite"; + + private TypeDescriber packageTypeDescriber; public ContributionServiceImpl(ContributionRepository repository, PackageProcessor packageProcessor, @@ -166,6 +169,8 @@ public class ContributionServiceImpl implements ContributionService { this.policyDefinitionsResolver = policyDefinitionsResolver; this.policyDefinitions = policyDefinitions; this.monitor = monitor; + + this.packageTypeDescriber = new PackageTypeDescriberImpl(); } /** @@ -370,6 +375,7 @@ public class ContributionServiceImpl implements ContributionService { contribution.setURI(contributionURI); contribution.setLocation(locationURL.toString()); contribution.setModelResolver(modelResolver); + contribution.setType(packageTypeDescriber.getType(locationURL, null)); List<URI> contributionArtifacts = null; diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java index 60e3944e26..aa314dd13f 100644 --- a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java @@ -25,6 +25,15 @@ import org.apache.tuscany.sca.contribution.Contribution; * A pluggable utility to provide a classloader for a given contribution */ public interface ContributionClassLoaderProvider { + + /** + * Get the contribution type that this object will provide + * classloaders for. Takes values from o.a.t.s.contribution.PackageType + * + * @return + */ + String getContributionType(); + /** * Get the classloader for the given contribution * @param contribution diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java new file mode 100644 index 0000000000..b714090ff2 --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java @@ -0,0 +1,55 @@ +/* + * 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; + +/** + * An extension point for contribution classloaders. Contribution + * classloaders respect the classloading strategy for the contribution + * in question. For example, a JAR contribution loads classes from its + * root directory while an EAR contribution follows the JEE classloading + * strategy. The choice of classloader is driven by the type of + * contribution being loaded. + * + * @version $Rev$ $Date$ + */ +public interface ContributionClassloaderProviderExtensionPoint { + + /** + * Add a contribution classloader provider extension. + * + * @param provider The provider to add + */ + void addProvider(ContributionClassLoaderProvider provider); + + /** + * Remove a contribution classloader provider extension. + * + * @param provider The provider to remove + */ + void removeProvider(ContributionClassLoaderProvider provider); + + /** + * Get a contribution classloader provider for the given contribution type. + * @param contributionType the lookup key + * @return The provider + */ + ContributionClassLoaderProvider getProvider(String contributionType); + +} diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java index ee62040731..8fd1898ad6 100644 --- a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java @@ -28,9 +28,17 @@ import org.apache.tuscany.sca.core.ExtensionPointRegistry; */ public class DefaultContributionClassLoaderProvider implements ContributionClassLoaderProvider { - public DefaultContributionClassLoaderProvider(ExtensionPointRegistry registry) { + public DefaultContributionClassLoaderProvider() { super(); } + + /** + * returns null as it is the default provider and applies when no specific + * provider has been specified + */ + public String getContributionType() { + return null; + } public ClassLoader getClassLoader(Contribution contribution, ClassLoader parent) { return new ContributionClassLoader(contribution, parent); diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java new file mode 100644 index 0000000000..7ffecdf312 --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.contribution.java; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.PackageProcessor; +import org.apache.tuscany.sca.contribution.service.ContributionException; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + + +/** + * Default implementation of a contribution classloader provider extension point. + * + * @version $Rev$ $Date$ + */ +public class DefaultContributionClassloaderProviderExtensionPoint implements ContributionClassloaderProviderExtensionPoint { + + private HashMap<String, ContributionClassLoaderProvider> providers = new HashMap<String, ContributionClassLoaderProvider>(); + private boolean loaded; + + /** + * Constructs a new DefaultModelFactoryExtensionPoint. + */ + public DefaultContributionClassloaderProviderExtensionPoint() { + } + + /** + * Add a contribution classloader provider extension. + * + * @param provider The provider to add + */ + public void addProvider(ContributionClassLoaderProvider provider){ + providers.put(provider.getContributionType(), provider); + } + + /** + * Remove a contribution classloader provider extension. + * + * @param provider The provider to remove + */ + public void removeProvider(ContributionClassLoaderProvider provider){ + providers.remove(provider.getContributionType()); + } + + /** + * Get a contribution classloader provider for the given contribution type. + * @param contributionType the lookup key + * @return The provider + */ + public ContributionClassLoaderProvider getProvider(String contributionType){ + loadProviders(); + return providers.get(contributionType); + } + + private synchronized void loadProviders() { + if (loaded) + return; + + // Get the processor service declarations + Set<ServiceDeclaration> processorDeclarations; + try { + processorDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ContributionClassLoaderProvider.class); + } catch (IOException e) { + throw new IllegalStateException(e); + } + + for (ServiceDeclaration processorDeclaration: processorDeclarations) { + Map<String, String> attributes = processorDeclaration.getAttributes(); + + // Load a URL artifact processor + String contributionType = attributes.get("type"); + + // Create a processor wrapper and register it + ContributionClassLoaderProvider provider = new LazyContributionClassLoaderProvider(contributionType, processorDeclaration); + addProvider(provider); + } + + loaded = true; + } + + /** + * A facade for package processors. + */ + private static class LazyContributionClassLoaderProvider implements ContributionClassLoaderProvider { + + private ServiceDeclaration processorDeclaration; + private String contributionType; + private ContributionClassLoaderProvider provider; + + private LazyContributionClassLoaderProvider(String contributionType, ServiceDeclaration processorDeclaration) { + this.processorDeclaration = processorDeclaration; + this.contributionType = contributionType; + } + + public String getContributionType() { + return contributionType; + } + + @SuppressWarnings("unchecked") + public ClassLoader getClassLoader(Contribution contribution, ClassLoader parent){ + if (provider == null) { + try { + Class<ContributionClassLoaderProvider> providerClass = (Class<ContributionClassLoaderProvider>)processorDeclaration.loadClass(); + Constructor<ContributionClassLoaderProvider> constructor = providerClass.getConstructor(); + provider = constructor.newInstance(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + return provider.getClassLoader(contribution, parent); + } + } + +} diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java index 1011f5d4a7..0b1c035ff4 100644 --- a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java @@ -28,6 +28,7 @@ import java.util.Map; import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider;
+import org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint;
import org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;
import org.apache.tuscany.sca.contribution.resolver.ClassReference;
import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
@@ -70,14 +71,16 @@ public class ClassReferenceModelResolver implements ModelResolver { ClassLoader contextClassLoader = ServiceDiscovery.getInstance().getServiceDiscoverer().getClass().getClassLoader();
ContributionClassLoaderProvider provider = null;
try {
- provider =
- registry.getExtensionPoint(UtilityExtensionPoint.class)
- .getUtility(ContributionClassLoaderProvider.class);
+ ContributionClassloaderProviderExtensionPoint providers =
+ registry.getExtensionPoint(ContributionClassloaderProviderExtensionPoint.class);
+
+ provider = providers.getProvider(contribution.getType());
+
} catch (Throwable e) {
// Ignore errors
}
if (provider == null) {
- provider = new DefaultContributionClassLoaderProvider(registry);
+ provider = new DefaultContributionClassLoaderProvider();
}
cl = provider.getClassLoader(contribution, contextClassLoader);
contribution.setClassLoader(cl);
diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider b/branches/sca-java-1.x/modules/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider new file mode 100644 index 0000000000..caf247750d --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider @@ -0,0 +1,21 @@ +# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;type=application/x-compressed
+org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;type=application/vnd.tuscany.folder
+org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;type=application/osgi.bundle
+
+
diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint b/branches/sca-java-1.x/modules/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint new file mode 100644 index 0000000000..e6cf964e81 --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+org.apache.tuscany.sca.contribution.java.DefaultContributionClassloaderProviderExtensionPoint
diff --git a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/EARContributionClassLoader.java b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/EARContributionClassLoader.java new file mode 100644 index 0000000000..87cdaa2cf3 --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/EARContributionClassLoader.java @@ -0,0 +1,201 @@ +/* + * 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.jee.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; + +import org.apache.tuscany.sca.contribution.Artifact; +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; +import org.apache.tuscany.sca.contribution.jee.EjbModuleInfo; +import org.apache.tuscany.sca.contribution.jee.JavaEEApplicationInfo; +import org.apache.tuscany.sca.contribution.jee.WebModuleInfo; + + +public class EARContributionClassLoader extends URLClassLoader { + + private Contribution contribution; + private List<EjbModuleInfo> ejbModules = new ArrayList<EjbModuleInfo>(); + + /** + * Constructor for contribution ClassLoader + * + * @param contribution + * @param parent + * @throws MalformedURLException + */ + public EARContributionClassLoader(Contribution contribution, final ClassLoader parent) { + super(new URL[0], parent); + + this.contribution = contribution; + + // get the classloaders for any EAR archive that is a contribution + for (Artifact artifact : contribution.getArtifacts()){ + if (artifact.getModel() instanceof JavaEEApplicationInfo){ + JavaEEApplicationInfo jeeAppInfo = (JavaEEApplicationInfo)artifact.getModel(); + // TODO - won't these all have the same classloader? + ejbModules.addAll(jeeAppInfo.getEjbModuleInfos().values()); + } + } + + } + + /* (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; + ClassNotFoundException cne = null; + + // TODO - Won't these classloaders all be the same? + for (EjbModuleInfo ejbModuleInfo : ejbModules){ + try { + clazz = ejbModuleInfo.getModuleClassloader().loadClass(className); + } catch (ClassNotFoundException e) { + cne = e; + } + + if (clazz != null){ + break; + } + } + + if (clazz == null){ + throw cne; + } + + 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.lang.ClassLoader#getResource(java.lang.String) + * + * Find a resource. + * Search path for resource: + * Parent ClassLoader + * This contribution + */ + @Override + public URL getResource(String resName) { + + URL resource = null; + + if (this.getParent() != null) { + resource = this.getParent().getResource(resName); + } + if (resource == null) { +// TODO Which classloader? +// resource = webModuleInfo.getModuleClassloader().getResource(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 = new HashSet<URL>(); + +// TODO - which classloader +// addEnumerationToCollection(resourceSet, webModuleInfo.getModuleClassloader().getResources(resName)); + addEnumerationToCollection(resourceSet, super.getResources(resName)); + + return Collections.enumeration(resourceSet); + } + + /* + * 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 JEE ClassLoader, parent ClassLoader: " + getParent(); + } + + +} diff --git a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/JEEClassLoaderProvider.java b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/EARContributionClassLoaderProvider.java index 73745fccef..5ea7dc55f7 100644 --- a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/JEEClassLoaderProvider.java +++ b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/EARContributionClassLoaderProvider.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.contribution.jee.impl; import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.PackageType; import org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider; import org.apache.tuscany.sca.contribution.java.impl.ContributionClassLoader; import org.apache.tuscany.sca.core.ExtensionPointRegistry; @@ -27,15 +28,22 @@ import org.apache.tuscany.sca.core.ExtensionPointRegistry; /** * The default implementation of the ContributionClassLoaderProvider */ -public class JEEClassLoaderProvider implements ContributionClassLoaderProvider { +public class EARContributionClassLoaderProvider implements ContributionClassLoaderProvider { - public JEEClassLoaderProvider(ExtensionPointRegistry registry) { + public EARContributionClassLoaderProvider() { super(); } + + public String getContributionType() { + return PackageType.EAR; + } public ClassLoader getClassLoader(Contribution contribution, ClassLoader parent) { + // TODO - This is not quite right at the CCL will load up the nested jars + // also. However we do need to pick up the import processing so + // need a bit of a refactor ContributionClassLoader ccl = new ContributionClassLoader(contribution, parent); - return new JEEClassLoader(contribution, ccl); + return new EARContributionClassLoader(contribution, ccl); } } diff --git a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/JEEClassLoader.java b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/JEEClassLoader.java deleted file mode 100644 index bc009c50d7..0000000000 --- a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/JEEClassLoader.java +++ /dev/null @@ -1,330 +0,0 @@ -/* - * 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.jee.impl; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.List; - -import org.apache.tuscany.sca.contribution.Artifact; -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; -import org.apache.tuscany.sca.contribution.jee.EjbModuleInfo; -import org.apache.tuscany.sca.contribution.jee.JavaEEApplicationInfo; -import org.apache.tuscany.sca.contribution.jee.WebModuleInfo; - - -public class JEEClassLoader extends URLClassLoader { - - private Contribution contribution; - private List<WebModuleInfo> webModules = new ArrayList<WebModuleInfo>(); - private List<EjbModuleInfo> ejbModules = new ArrayList<EjbModuleInfo>(); - - /** - * Constructor for contribution ClassLoader - * - * @param contribution - * @param parent - * @throws MalformedURLException - */ - public JEEClassLoader(Contribution contribution, final ClassLoader parent) { - super(new URL[0], parent); - - this.contribution = contribution; - - // get the classloaders for any JEE archives references by the contribution - // TODO extend to case where JEE archive is outside the contribution - for (Artifact artifact : contribution.getArtifacts()){ - if (artifact.getModel() instanceof WebModuleInfo){ - WebModuleInfo webModuleInfo = (WebModuleInfo)artifact.getModel(); - webModules.add(webModuleInfo); - } else if (artifact.getModel() instanceof EjbModuleInfo){ - EjbModuleInfo ejbModuleInfo = (EjbModuleInfo)artifact.getModel(); - ejbModules.add(ejbModuleInfo); - } else if (artifact.getModel() instanceof JavaEEApplicationInfo){ - JavaEEApplicationInfo jeeAppInfo = (JavaEEApplicationInfo)artifact.getModel(); - webModules.addAll(jeeAppInfo.getWebModuleInfos().values()); - ejbModules.addAll(jeeAppInfo.getEjbModuleInfos().values()); - } - } - - } - - /* (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; - ClassNotFoundException cne = null; - - // ask each of the JEE archive classloaders in turn - // TODO This is a bad thing as two archives may have the same class - // but don't know how to avoid this - for (WebModuleInfo webModuleInfo : webModules){ - try { - clazz = webModuleInfo.getModuleClassloader().loadClass(className); - } catch (ClassNotFoundException e) { - cne = e; - } - - if (clazz != null){ - break; - } - } - - // We would expect the webapp classloaders to have an ejb classloader as parent - // but we have to go through the ebj module classloaders here just in case - // there are no webapps - for (EjbModuleInfo ejbModuleInfo : ejbModules){ - try { - clazz = ejbModuleInfo.getModuleClassloader().loadClass(className); - } catch (ClassNotFoundException e) { - cne = e; - } - - if (clazz != null){ - break; - } - } - - if (clazz == null){ - throw cne; - } - - 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 = null; -/* - 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 JEEClassLoader) { - - for (Export export : exportingContribution.getExports()) { - if (import_.match(export)) { - url = ((JEEClassLoader)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 JEEClassLoader) { - - for (Export export : exportingContribution.getExports()) { - if (import_.match(export)) { - addEnumerationToCollection(resources, - ((JEEClassLoader)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); - } - - - /* - * 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 JEE ClassLoader, parent ClassLoader: " + getParent(); - } - - -} diff --git a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/WARContributionClassLoader.java b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/WARContributionClassLoader.java new file mode 100644 index 0000000000..4047371583 --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/WARContributionClassLoader.java @@ -0,0 +1,191 @@ +/* + * 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.jee.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; + +import org.apache.tuscany.sca.contribution.Artifact; +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; +import org.apache.tuscany.sca.contribution.jee.EjbModuleInfo; +import org.apache.tuscany.sca.contribution.jee.JavaEEApplicationInfo; +import org.apache.tuscany.sca.contribution.jee.WebModuleInfo; + + +public class WARContributionClassLoader extends URLClassLoader { + + private Contribution contribution; + private WebModuleInfo webModuleInfo = null; + + /** + * Constructor for contribution ClassLoader + * + * @param contribution + * @param parent + * @throws MalformedURLException + */ + public WARContributionClassLoader(Contribution contribution, final ClassLoader parent) { + super(new URL[0], parent); + + this.contribution = contribution; + + // get the classloaders for any WAR archive that is a contribution + // TODO extend to case where JEE archive is outside the contribution + for (Artifact artifact : contribution.getArtifacts()){ + if (artifact.getModel() instanceof WebModuleInfo){ + webModuleInfo = (WebModuleInfo)artifact.getModel(); + break; + } + } + + } + + /* (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; + ClassNotFoundException cne = null; + + try { + clazz = webModuleInfo.getModuleClassloader().loadClass(className); + } catch (ClassNotFoundException e) { + cne = e; + } + + if (clazz == null){ + throw cne; + } + + 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.lang.ClassLoader#getResource(java.lang.String) + * + * Find a resource. + * Search path for resource: + * Parent ClassLoader + * This contribution + */ + @Override + public URL getResource(String resName) { + + URL resource = null; + + if (this.getParent() != null) { + resource = this.getParent().getResource(resName); + } + if (resource == null) + resource = webModuleInfo.getModuleClassloader().getResource(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 = new HashSet<URL>(); + + addEnumerationToCollection(resourceSet, webModuleInfo.getModuleClassloader().getResources(resName)); + addEnumerationToCollection(resourceSet, super.getResources(resName)); + + return Collections.enumeration(resourceSet); + } + + + /* + * 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 WAR ClassLoader, parent ClassLoader: " + getParent(); + } + + +} diff --git a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/WARContributionClassLoaderProvider.java b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/WARContributionClassLoaderProvider.java new file mode 100644 index 0000000000..cfa8dd0d58 --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/java/org/apache/tuscany/sca/contribution/jee/impl/WARContributionClassLoaderProvider.java @@ -0,0 +1,49 @@ +/* + * 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.jee.impl; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.PackageType; +import org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider; +import org.apache.tuscany.sca.contribution.java.impl.ContributionClassLoader; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * The default implementation of the ContributionClassLoaderProvider + */ +public class WARContributionClassLoaderProvider implements ContributionClassLoaderProvider { + + public WARContributionClassLoaderProvider() { + super(); + } + + public String getContributionType() { + return PackageType.EAR; + } + + public ClassLoader getClassLoader(Contribution contribution, ClassLoader parent) { + // TODO - This is not quite right at the CCL will load up the nested jars + // also. However we do need to pick up the import processing so + // need a bit of a refactor + ContributionClassLoader ccl = new ContributionClassLoader(contribution, parent); + return new WARContributionClassLoader(contribution, ccl); + } + +} diff --git a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider index 3f309774aa..44807c1c17 100644 --- a/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider +++ b/branches/sca-java-1.x/modules/contribution-jee-impl/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider @@ -14,4 +14,5 @@ # KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-org.apache.tuscany.sca.contribution.jee.impl.JEEClassLoaderProvider
\ No newline at end of file +org.apache.tuscany.sca.contribution.jee.impl.EJBContributionClassLoaderProvider;type=application/ear
+org.apache.tuscany.sca.contribution.jee.impl.WARContributionClassLoaderProvider;type=application/war
\ No newline at end of file diff --git a/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java b/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java index 743c2356d7..84a927628d 100644 --- a/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java +++ b/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java @@ -113,4 +113,19 @@ public interface Contribution extends Artifact { */ void setClassLoader(ClassLoader classLoader); + /** + * Returns the type string based on the types that appear in + * o.a.t.s.contribution.PackageType + * + * @return the contribution type string + */ + String getType(); + + /** + * Sets the type string based on the types that appear in + * o.a.t.s.contribution.PackageType + * + * @param contributionType the contribution type string + */ + void setType(String contributionType); }
\ No newline at end of file diff --git a/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java b/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java index fc9ae607e7..dcdbda8ce7 100644 --- a/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java +++ b/branches/sca-java-1.x/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java @@ -35,6 +35,7 @@ import org.apache.tuscany.sca.contribution.resolver.ModelResolver; * @version $Rev$ $Date$ */ class ContributionImpl implements Contribution { + private String type; private String uri; private String location; private Object model; @@ -150,5 +151,13 @@ class ContributionImpl implements Contribution { return "Contribution : " + uri + " \n" + "from: " + location; } + + public String getType() { + return type; + } + + public void setType(String contributionType) { + this.type = contributionType; + } } diff --git a/branches/sca-java-1.x/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java b/branches/sca-java-1.x/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java index e60a5d8108..97fd796f9d 100644 --- a/branches/sca-java-1.x/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java +++ b/branches/sca-java-1.x/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java @@ -139,4 +139,13 @@ class WorkspaceImpl implements Workspace { public void setModelResolver(ModelResolver modelResolver) { this.modelResolver = modelResolver; } + + public String getType() { + // TODO type only used to determine contribution classloader + return null; + } + + public void setType(String contributionType) { + // TODO type only used to determine contribution classloader + } } |