diff options
author | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-21 06:45:30 +0000 |
---|---|---|
committer | slaws <slaws@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-21 06:45:30 +0000 |
commit | 990291a3ec7af02e4b28846dd0eea9bd5a031945 (patch) | |
tree | 465b58fb282caa2d13a38c5b60548b7dc0b906d3 /branches/sca-java-1.x/modules/contribution-java/src | |
parent | 10ccafbe890ad4d54e95bbeaf67020e10d6be422 (diff) |
TUSCANY-2972 instigate contribution type specific class loading to allow the JEE classloader to be used independently of jar, zip, dir classloading schemes
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@767015 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
7 files changed, 263 insertions, 5 deletions
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
|