Skip to content
Browse files

[SHRINKWRAP-275] Allow adding resources held in nested JARs, along w/…

… test cases
  • Loading branch information...
1 parent cb68297 commit b09121467232bb1f466231287cb3f5542e810643 @ALRubinger ALRubinger committed May 18, 2011
View
51 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java
@@ -17,6 +17,7 @@
package org.jboss.shrinkwrap.impl.base.container;
import java.io.File;
+import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.AccessController;
@@ -32,6 +33,7 @@
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.Asset;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.asset.FileAsset;
import org.jboss.shrinkwrap.api.asset.NamedAsset;
import org.jboss.shrinkwrap.api.asset.UrlAsset;
@@ -593,6 +595,35 @@ public T addAsManifestResource(String resourceName, ArchivePath target) throws I
return addAsManifestResource(fileFromResource(resourceName), target);
}
+
+ /**
+ * Adds the specified {@link File} resource (a nested JAR File form) to the current archive,
+ * returning the archive itself
+ *
+ * @param resource
+ * @param target
+ * @return
+ * @throws IllegalArgumentException
+ */
+ private T addNestedJarFileResource(final File resource, final ArchivePath target, final ArchivePath base) throws IllegalArgumentException
+ {
+ final Iterable<ClassLoader> classLoaders = ((Configurable) this.getArchive()).getConfiguration()
+ .getClassLoaders();
+
+ for (final ClassLoader classLoader : classLoaders)
+ {
+ final String path = resource.getPath();
+ final String adjustedPath = path.substring(path.indexOf("!/") + 2, path.length());
+ final InputStream in = classLoader.getResourceAsStream(adjustedPath);
+ if (in != null)
+ {
+ final Asset asset = new ByteArrayAsset(in);
+ return add(asset, base, target.get());
+ }
+ }
+ throw new IllegalArgumentException(resource.getPath() + " was not found in any available ClassLoaders");
+ }
+
/* (non-Javadoc)
* @see org.jboss.shrinkwrap.api.container.ManifestContainer#addManifestResource(java.io.File, org.jboss.shrinkwrap.api.Path)
*/
@@ -605,7 +636,15 @@ public T addAsManifestResource(File resource, ArchivePath target) throws Illegal
if (resource.isFile())
return addAsManifestResource(new FileAsset(resource), target);
- if (resource.listFiles().length == 0)
+ final File[] files = resource.listFiles();
+
+ // SHRINKWRAP-275, resource URL coming in from a JAR
+ if(files==null)
+ {
+ return this.addNestedJarFileResource(resource, target, this.getManifestPath());
+ }
+
+ if (files.length == 0)
return addAsManifestResource(new FileAsset(resource), target);
for (File file : resource.listFiles())
@@ -837,7 +876,15 @@ public T addAsResource(File resource, ArchivePath target) throws IllegalArgument
if (resource.isFile())
return addAsResource(new FileAsset(resource), target);
- if (resource.listFiles().length == 0)
+ final File[] files = resource.listFiles();
+
+ // SHRINKWRAP-275, resource URL coming in from a JAR
+ if(files==null)
+ {
+ return this.addNestedJarFileResource(resource, target, this.getResourcePath());
+ }
+
+ if (files.length == 0)
return addAsResource(new FileAsset(resource), target);
for (File file : resource.listFiles())
View
30 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicContainerTestBase.java
@@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
+import java.util.logging.Logger;
import junit.framework.Assert;
@@ -515,6 +516,35 @@ public void testAddResourceURL() throws Exception {
ArchivePath testPath = new BasicPath(getResourcePath(), targetPath);
Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
}
+
+ /**
+ * SHRINKWRAP-275
+ */
+ @Test
+ @ArchiveType(ManifestContainer.class)
+ public void testAddManifestStringTargetResourceFromJar() throws Exception
+ {
+ // Causing NPE
+ getManifestContainer().addAsManifestResource("java/lang/String.class", "String.class");
+
+ ArchivePath testPath = new BasicPath(getManifestPath(), "String.class");
+ Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
+ }
+
+ /**
+ * SHRINKWRAP-275
+ */
+ @Test
+ @ArchiveType(ResourceContainer.class)
+ public void testAddResourceStringTargetResourceFromJar() throws Exception
+ {
+ // Causing NPE
+ getResourceContainer().addAsResource("java/lang/String.class", "String.class");
+
+ ArchivePath testPath = new BasicPath(getResourcePath(), "String.class");
+ Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
+ Logger.getAnonymousLogger().info(getArchive().toString(true));
+ }
/*
* https://jira.jboss.org/jira/browse/SHRINKWRAP-145 - Should be Resource, Target
View
28 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicWebContainerTestBase.java
@@ -426,4 +426,32 @@ public void testAddWebInfResourcePackagePathTarget() throws Exception
ArchivePath testPath = new BasicPath(getWebInfPath(), targetPath);
Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
}
+
+ /**
+ * SHRINKWRAP-275
+ */
+ @Test
+ @ArchiveType(WebContainer.class)
+ public void testAddWebStringTargetResourceFromJar() throws Exception
+ {
+ // Causing NPE
+ getWebContainer().addAsWebResource("java/lang/String.class", "String.class");
+
+ ArchivePath testPath = new BasicPath(getWebPath(), "String.class");
+ Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
+ }
+
+ /**
+ * SHRINKWRAP-275
+ */
+ @Test
+ @ArchiveType(WebContainer.class)
+ public void testAddWebInfStringTargetResourceFromJar() throws Exception
+ {
+ // Causing NPE
+ getWebContainer().addAsWebInfResource("java/lang/String.class", "String.class");
+
+ ArchivePath testPath = new BasicPath(getWebInfPath(), "String.class");
+ Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
+ }
}

0 comments on commit b091214

Please sign in to comment.
Something went wrong with that request. Please try again.