diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-06 10:32:15 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-06 10:32:15 +0000 |
commit | e66dcf21f2ec17552765506538888d9fb16a4351 (patch) | |
tree | 3b7f14e6a724c2509d767dec3cc3d175baed297b /branches/sca-java-1.x | |
parent | bb394e3157ee5861d1dce8ac93696f3cd9cac67a (diff) |
Copy zip/folder nested jar support change from 2.x to 1.x
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@762272 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
3 files changed, 93 insertions, 3 deletions
diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java index 61c73bf2f8..e1243dbc6a 100644 --- a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java @@ -55,8 +55,15 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes return parentClassLoader; } - public ClassLoaderModelResolver(final Contribution contribution, ModelFactoryExtensionPoint modelFactories) throws MalformedURLException { - super(new URL[] {new URL(contribution.getLocation())}, parentClassLoader()); + private static URL[] getContributionURLs(final Contribution contribution) throws IOException { + List<URL> urls = new ArrayList<URL>(); + urls.add(new URL(contribution.getLocation())); + urls.addAll(ContributionHelper.getNestedJarUrls(contribution)); + return urls.toArray(new URL[urls.size()]); + } + + public ClassLoaderModelResolver(final Contribution contribution, ModelFactoryExtensionPoint modelFactories) throws IOException { + super(getContributionURLs(contribution), parentClassLoader()); this.contribution = contribution; // Index Java import resolvers by package name diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java index aa68925376..b167d9af4e 100644 --- a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java @@ -25,10 +25,12 @@ 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.Contribution;
import org.apache.tuscany.sca.contribution.Export;
@@ -57,7 +59,10 @@ public class ContributionClassLoader extends URLClassLoader { if (contribution.getLocation() != null) {
try {
this.addURL(new URL(contribution.getLocation()));
- } catch (MalformedURLException e) {
+ for (URL url : ContributionHelper.getNestedJarUrls(contribution)) {
+ this.addURL(url);
+ }
+ } catch (IOException e) {
throw new RuntimeException(e);
}
}
diff --git a/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java new file mode 100644 index 0000000000..54554bcc4e --- /dev/null +++ b/branches/sca-java-1.x/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java @@ -0,0 +1,78 @@ +package org.apache.tuscany.sca.contribution.java.impl; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; + +public class ContributionHelper { + + public static List<URL> getNestedJarUrls(final Contribution contribution) throws IOException { + List<URL> urls = new ArrayList<URL>(); + boolean isZipContribution = contribution.getLocation().endsWith(".zip"); + URI uri = URI.create(contribution.getLocation()); + boolean isFolderContribution = !isZipContribution && uri.getScheme().equals("file") && new File(uri).isDirectory(); + if (isZipContribution || isFolderContribution) { + for (Artifact a : contribution.getArtifacts()) { + if (a.getLocation().endsWith(".jar")) { + if (isZipContribution) { + urls.add(createTempJar(a, contribution)); + } else { + urls.add(new URL(a.getLocation())); + } + } + } + } + return urls; + } + + /** + * URLClassLoader doesn't seem to work with URLs to jars within an archive so as a work around + * copy the jar to a temp file and use the url to that. + */ + private static URL createTempJar(Artifact artifact, Contribution contribution) throws IOException { + FileOutputStream fileOutputStream = null; + ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File(URI.create(contribution.getLocation())))); + try { + ZipEntry zipEntry = zipInputStream.getNextEntry(); + while (zipEntry != null) { + if (artifact.getLocation().endsWith(zipEntry.getName())) { + + String tempName = ("tmp." + artifact.getURI().substring(0, artifact.getURI().length() - 3)).replace('/', '.'); + File tempFile = File.createTempFile(tempName, ".jar"); + tempFile.deleteOnExit(); + fileOutputStream = new FileOutputStream(tempFile); + + byte[] buf = new byte[2048]; + int n; + while ((n = zipInputStream.read(buf, 0, buf.length)) > -1) { + fileOutputStream.write(buf, 0, n); + } + + fileOutputStream.close(); + zipInputStream.closeEntry(); + + return tempFile.toURI().toURL(); + + } + zipEntry = zipInputStream.getNextEntry(); + } + } finally { + zipInputStream.close(); + if (fileOutputStream != null) { + fileOutputStream.close(); + } + } + + throw new IllegalStateException(); + } +} |