summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-04-02 10:19:37 +0000
committerantelder <antelder@13f79535-47bb-0310-9956-ffa450edef68>2009-04-02 10:19:37 +0000
commite7e7b4aa5a8573bd58508438a64a216d2dc9fdea (patch)
tree8304fe7d6febb1165a530e272c96cec060508f7f
parentfcb3415c1c9b9a793cb9b075fe8a74ef5a5afdf0 (diff)
Add prototype code to support zip/folder contributions containing nested jar's, and add itests for that to build
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@761236 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--java/sca/itest/pom.xml2
-rw-r--r--java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java14
-rw-r--r--java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java73
3 files changed, 85 insertions, 4 deletions
diff --git a/java/sca/itest/pom.xml b/java/sca/itest/pom.xml
index 57dd85b9a7..a18e8247f0 100644
--- a/java/sca/itest/pom.xml
+++ b/java/sca/itest/pom.xml
@@ -59,6 +59,8 @@
<module>callback-set-callback</module>
<module>callback-set-conversation</module>
<module>component-type</module>
+ <module>contribution-folder</module>
+ <module>contribution-zip</module>
<module>conversations</module>
<!--module>definitions-multiple</module-->
<module>exceptions</module>
diff --git a/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java b/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java
index 135b3fbb69..64ff500741 100644
--- a/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java
+++ b/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java
@@ -20,7 +20,6 @@
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.ArrayList;
@@ -52,9 +51,16 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes
ClassLoader parentClassLoader = ServiceDiscovery.getInstance().getServiceDiscoverer().getClass().getClassLoader();
return parentClassLoader;
}
-
- public ClassLoaderModelResolver(final Contribution contribution, FactoryExtensionPoint 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, FactoryExtensionPoint modelFactories) throws IOException {
+ super(getContributionURLs(contribution), parentClassLoader());
this.contribution = contribution;
// Index Java import resolvers by package name
diff --git a/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java b/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java
new file mode 100644
index 0000000000..38e2795841
--- /dev/null
+++ b/java/sca/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java
@@ -0,0 +1,73 @@
+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>();
+ for (Artifact a : contribution.getArtifacts()) {
+ if (a.getLocation().endsWith(".jar")) {
+ if (contribution.getLocation().endsWith(".zip")) {
+ 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);
+ 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();
+ }
+}