/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.api.asset.Asset#openStream() */ @Override public InputStream openStream() { // Export via the specified exporter return this.getArchive().as(this.exporter).exportAsInputStream(); }
/** * Processes a nested archive by delegating to the ExplodedArchiveExporter * * @param parentDirectory * @param nestedArchiveAsset */ private void processArchiveAsset(File parentDirectory, ArchiveAsset nestedArchiveAsset) { // Get the nested archive Archive<?> nestedArchive = nestedArchiveAsset.getArchive(); nestedArchive.as(ExplodedExporter.class).exportExploded(parentDirectory); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.api.asset.Asset#openStream() */ @Override public InputStream openStream() { // Export via the specified exporter return this.getArchive().as(this.exporter).exportAsInputStream(); }
/** * Processes a nested archive by delegating to the ExplodedArchiveExporter * * @param parentDirectory * @param nestedArchiveAsset */ private void processArchiveAsset(File parentDirectory, ArchiveAsset nestedArchiveAsset) { // Get the nested archive Archive<?> nestedArchive = nestedArchiveAsset.getArchive(); nestedArchive.as(ExplodedExporter.class).exportExploded(parentDirectory); }
/** * Check to see if a path is found in a nested archive */ private boolean nestedContains(ArchivePath path) { // Iterate through nested archives for (Entry<ArchivePath, ArchiveAsset> nestedArchiveEntry : nestedArchives.entrySet()) { ArchivePath archivePath = nestedArchiveEntry.getKey(); ArchiveAsset archiveAsset = nestedArchiveEntry.getValue(); // Check to see if the requested path starts with the nested archive path if (startsWith(path, archivePath)) { Archive<?> nestedArchive = archiveAsset.getArchive(); // Get the asset path from within the nested archive ArchivePath nestedAssetPath = getNestedPath(path, archivePath); // Recurse the call to the nested archive return nestedArchive.contains(nestedAssetPath); } } return false; }
/** * Recursively scans archive content (including sub archives) for persistence.xml descriptors. */ private Collection<Node> collectPersistenceXml(final Archive<?> archive) { final Collection<Node> nodes = new LinkedList<Node>(getPersistenceDescriptors(archive)); for (Node node : collectSubArchives(archive)) { if (node.getAsset() instanceof ArchiveAsset) { final ArchiveAsset archiveAsset = (ArchiveAsset) node.getAsset(); nodes.addAll(collectPersistenceXml(archiveAsset.getArchive())); } } return nodes; }
private static boolean isJAXRS(ArchivePath path, Asset asset) { if (asset == null) { return false; } if (asset instanceof ArchiveAsset) { return isJAXRS(((ArchiveAsset) asset).getArchive()); } if (!path.get().endsWith(".class")) { return false; } try (InputStream in = asset.openStream()) { ClassReader reader = new ClassReader(in); JAXRSAnnotationSeekingClassVisitor visitor = new JAXRSAnnotationSeekingClassVisitor(); reader.accept(visitor, 0); return visitor.isFound(); } catch (IOException ignored) { } return false; }
static boolean hasApplicationPathAnnotation(ArchivePath path, Asset asset) { if (asset == null) { return false; } if (asset instanceof ArchiveAsset) { return hasApplicationPathAnnotation(((ArchiveAsset) asset).getArchive()); } if (!path.get().endsWith(".class")) { return false; } try (InputStream in = asset.openStream()) { ClassReader reader = new ClassReader(in); ApplicationPathAnnotationSeekingClassVisitor visitor = new ApplicationPathAnnotationSeekingClassVisitor(); reader.accept(visitor, 0); return visitor.isFound(); } catch (IOException ignored) { } return false; }
/** * Recursively scans archive content (including sub archives) for persistence.xml descriptors. * * @param archive * @return */ private Collection<Node> collectPersistenceXml(final Archive<?> archive) { final Collection<Node> nodes = new LinkedList<Node>(getPersistenceDescriptors(archive)); for (Node node : collectSubArchives(archive)) { if (node.getAsset() instanceof ArchiveAsset) { final ArchiveAsset archiveAsset = (ArchiveAsset) node.getAsset(); nodes.addAll(collectPersistenceXml(archiveAsset.getArchive())); } } return nodes; }
private void enrichWithCDI(Archive<?> applicationArchive) { Map<ArchivePath, Node> contentMap = applicationArchive.getContent(Filters.include(".*/cukespace-core.jar")); for (Node node : contentMap.values()) { if (node.getAsset() instanceof ArchiveAsset) { JavaArchive archive = (JavaArchive) ((ArchiveAsset) node.getAsset()).getArchive(); archive.addClass(CukeSpaceCDIObjectFactory.class); archive.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } } } }
/** * Explodes kumuluzee-loader library to root of the archive. * * @param javaArchive Archive with all jars in /WEB-INF/lib (war) */ private static void explodeLoaderArchiveToRoot(JavaArchive javaArchive) { for (Node n : javaArchive.get("/WEB-INF/lib").getChildren()) { if (n.getAsset() instanceof ArchiveAsset && ((ArchiveAsset) n.getAsset()).getArchive().getName().startsWith("kumuluzee-loader-")) { Archive<?> dependencyJar = ((ArchiveAsset) n.getAsset()).getArchive(); LOG.fine("Found kumuluzee-loader archive: " + dependencyJar.getName()); javaArchive.merge(dependencyJar); javaArchive.delete(n.getPath()); break; } } }
protected final Archive<? extends Archive<?>> findArchiveByTestClass(Archive<?> topArchive, Class testClass) { Archive<?> testArchive = topArchive; if (!archiveContains(testArchive, testClass)) { for (Node node : testArchive.getContent(Filters.include(".*\\.(jar|war)")).values()) { if (node.getAsset() instanceof ArchiveAsset) { Archive archive = ((ArchiveAsset) node.getAsset()).getArchive(); if (archiveContains(archive, testClass) && archive instanceof LibraryContainer) { testArchive = archive; } } } } return testArchive; }
/** * Explodes archive marked with {@link ApplicationArchiveMarker} to root of the archive. * * @param javaArchive Archive with all jars in /WEB-INF/lib (war) */ private static void explodeAppArchiveToRoot(JavaArchive javaArchive) { for (Node n : javaArchive.get("/WEB-INF/lib").getChildren()) { if (n.getAsset() instanceof ArchiveAsset) { Archive<?> dependencyJar = ((ArchiveAsset) n.getAsset()).getArchive(); if (dependencyJar.contains(ApplicationArchiveMarker.MARKER_FILENAME)) { LOG.fine("Found application archive: " + dependencyJar.getName()); dependencyJar.delete(ApplicationArchiveMarker.MARKER_FILENAME); javaArchive.merge(dependencyJar); javaArchive.delete(n.getPath()); break; } } } }
@Test public void testImportArchiveAsTypeFromFilterUsingDefaultFormat() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); Collection<JavaArchive> jars = archive.getAsType(JavaArchive.class, Filters.include(".*jar")); Assert.assertEquals("Unexpected result found", 1, jars.size()); JavaArchive jar = jars.iterator().next().add(new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain a new asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
@Test public void testImportArchiveAsTypeFromFilter() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); Collection<JavaArchive> jars = archive .getAsType(JavaArchive.class, Filters.include(".*jar"), ArchiveFormat.ZIP); Assert.assertEquals("Unexpected result found", 1, jars.size()); JavaArchive jar = jars.iterator().next().add(new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain a new asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
@Test public void testImportArchiveAsTypeFromStringUsingDefaultFormat() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, resourcePath).add(new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain a new asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
/** * Ensure adding an archive to a path successfully stores all assets to specific path including the archive name * * @throws Exception */ @Test public void testAddArchiveToPath() throws Exception { Archive<T> archive = getArchive(); Archive<T> sourceArchive = createNewArchive(); ArchivePath baseLocation = new BasicPath("somewhere"); archive.add(sourceArchive, baseLocation, ZipExporter.class); ArchivePath expectedPath = new BasicPath(baseLocation, sourceArchive.getName()); Node node = archive.get(expectedPath); Assert.assertNotNull("Asset should have been added to path: " + expectedPath.get(), node); Assert.assertTrue("An instance of ArchiveAsset should have been added to path: " + expectedPath.get(), node.getAsset() instanceof ArchiveAsset); ArchiveAsset archiveAsset = ArchiveAsset.class.cast(node.getAsset()); Archive<?> nestedArchive = archiveAsset.getArchive(); Assert.assertEquals("Nested Archive should be same archive that was added", sourceArchive, nestedArchive); }
@Test public void testImportArchiveAsTypeFromString() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, resourcePath, ArchiveFormat.ZIP).add( new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain a new asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
@Test public void testImportArchiveAsTypeFromArchivePathUsingDefaultFormat() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, ArchivePaths.create(resourcePath)).add( new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain an archive asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
@Test public void testImportArchiveAsTypeFromArchivePath() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, ArchivePaths.create(resourcePath), ArchiveFormat.ZIP) .add(new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain an archive asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }