Map<ArchivePath, Node> archiveContents = archive.getContent(); for (Map.Entry<ArchivePath, Node> entry : archiveContents.entrySet()) { ArchivePath path = entry.getKey(); Node n = entry.getValue(); Path f = subpath(deployDir, path.get()); if (n.getAsset() == null) { Files.copy(n.getAsset().openStream(), f, StandardCopyOption.REPLACE_EXISTING); String p = n.getPath().get(); if (callback != null) { callback.accept(p);
/** * Ensure an asset can be retrieved by a string path * * @throws Exception */ @Test public void testGetAssetWithString() throws Exception { Archive<T> archive = getArchive(); ArchivePath location = new BasicPath("/", "test.properties"); Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES); archive.add(asset, location); Node fetchedNode = archive.get(location.get()); Assert.assertTrue("Asset should be returned from path: " + location.get(), compareAssets(asset, fetchedNode.getAsset())); }
private void index(Archive<?> archive, List<IndexView> indexes) throws IOException { LOGGER.debugv("Indexing archive: {0}", archive.getName()); Node indexNode = archive.get(ArchivePaths.create(INDEX_LOCATION)); if (indexNode != null) { try (InputStream indexStream = indexNode.getAsset().openStream()) { LOGGER.debugv("Loading attached index from archive: {0}", archive.getName()); indexes.add(new IndexReader(indexStream).read()); for (Map.Entry<ArchivePath, Node> entry : archive.getContent(this::isClass).entrySet()) { try (InputStream contentStream = entry.getValue().getAsset().openStream()) { LOGGER.debugv("Indexing asset: {0} from archive: {1}", entry.getKey().get(), archive.getName()); indexer.index(contentStream); } catch (IOException indexerIOException) { LOGGER.warnv(indexerIOException, "Failed parsing: {0} from archive: {1}", entry.getKey().get(), archive.getName()); for (Map.Entry<ArchivePath, Node> entry : archive.getContent(a -> a.get().endsWith(JAR_SUFFIX)).entrySet()) { if (entry.getValue().getAsset() instanceof ArchiveAsset) { ArchiveAsset archiveAsset = (ArchiveAsset) entry.getValue().getAsset(); index(archiveAsset.getArchive(), indexes); } else { try (InputStream contentStream = entry.getValue().getAsset().openStream()) { JARArchive jarArchive = ShrinkWrap.create(JARArchive.class, entry.getKey().get()).as(ZipImporter.class).importFrom(contentStream) .as(JARArchive.class); index(jarArchive, indexes);
Assert.assertFalse("Should have imported something", importedArchive.getContent().isEmpty()); Assert.assertTrue("Importer should have imported " + entryName.get() + " from " + originalSource, importedArchive.contains(entryName)); final Node node = importedArchive.get(entryName); byte[] importedContent = IOUtil.asByteArray(node.getAsset().openStream());
protected List<Properties> extractPomProperties(final Node node) { final List<Properties> properties = new ArrayList<>(); try (final InputStream in = node.getAsset().openStream()) { ShrinkWrap.create(ZipImporter.class) .importFrom(in) .as(JavaArchive.class) .getContent(p -> POM_PROPERTIES.matcher(p.get()).matches()) .values() .forEach(propNode -> { final Properties props = new Properties(); try (final InputStream in2 = propNode.getAsset().openStream()) { props.load(in2); } catch (IOException e) { throw new RuntimeException(e); } properties.add(props); }); } catch (IOException e) { throw new RuntimeException(e); } return properties; }
Asset asset = content.getAsset(); if (asset == null) { return null; return ArchiveAsset.class.cast(asset).getArchive().as(type); stream = asset.openStream(); X archive = ShrinkWrap.create(formatBinding.getImporter(), path.get()).importFrom(stream).as(type); delete(path); add(new ArchiveAsset(archive, formatBinding.getExporter()), path);
@Override public Enumeration<String> entries(String path) { List<String> entries = new ArrayList<String>(); for (Entry<ArchivePath, Node> entry : this.getArchive().getContent().entrySet()) { if (entry.getKey().get().startsWith(path)) { if (entry.getValue().getAsset() != null) { entries.add(entry.getKey().get()); } } } return Collections.enumeration(entries); }
private static void copyDir(Archive<?> sourceArchive, Archive<?> targetArchive, String source, String target) { Node sourceNode = sourceArchive.get(source); if (sourceNode.getAsset() != null) { targetArchive.add(sourceNode.getAsset(), target); } else { for (Node child : sourceNode.getChildren()) { String childName = child.getPath().get().replaceFirst(child.getPath().getParent().get(), ""); copyDir(sourceArchive, targetArchive, child.getPath().get(), ArchivePaths.create(target, childName).get()); } } } }
/** * Converts {@link ArchiveAsset}s in parentDir to {@link ByteArrayAsset}s. * <p> * By default {@link org.jboss.shrinkwrap.api.exporter.ExplodedExporter} exports {@link ArchiveAsset}s in parent * archive as exploded directories. Since we need to export them as archives, we convert them to * {@link ByteArrayAsset}s. * * @param a Archive to fix * @param parentDir Directory to scan for {@link ArchiveAsset}s */ private static void fixArchiveAssets(Archive<?> a, String parentDir) { Node n = a.get(parentDir); Archive<?> tmp = ShrinkWrap.create(JavaArchive.class); List<ArchivePath> pathsToDelete = new ArrayList<>(); for (Node child : n.getChildren()) { Asset childAsset = child.getAsset(); if (childAsset instanceof ArchiveAsset && child.getPath().get().endsWith(".jar")) { LOG.fine("Converting archive " + child.getPath().get() + " to ByteArrayAsset"); ArchiveAsset archiveAsset = (ArchiveAsset) childAsset; ByteArrayAsset bas = new ByteArrayAsset(archiveAsset.openStream()); pathsToDelete.add(child.getPath()); tmp.add(bas, child.getPath()); } } for (ArchivePath ap : pathsToDelete) { a.delete(ap); } a.merge(tmp); }
@Override public InputStream getInputStream() throws IOException { ArchivePath path = convertToArchivePath(u); Node node = archive.get(prefix + path.get()); if (node == null && !prefix.isEmpty()) { // WEB-INF/lib/x.jar!* node = archive.get(path); } // SHRINKWRAP-308 if (node == null) { throw new FileNotFoundException("Requested path: " + path + " does not exist in " + archive.toString()); } Asset asset = node.getAsset(); if (asset == null) { return null; } InputStream input = asset.openStream(); synchronized (this) { openedStreams.add(input); } return input; }
private static boolean hasApplicationPathAnnotation(ArchivePath path, Asset asset) { if (asset == null) { return false; } if (! path.get().endsWith(".class" ) ) { return false; } try (InputStream in = asset.openStream() ) { ClassReader reader = new ClassReader(in); AnnotationSeekingClassVisitor visitor = new AnnotationSeekingClassVisitor(); reader.accept(visitor, 0 ); return visitor.isFound(); } catch (FileNotFoundException e) { } catch (IOException e) { } return false; }
/** * Ensure get content returns the correct map of content * * @throws Exception */ @Test public void testToGetContent() throws Exception { Archive<T> archive = getArchive(); ArchivePath location = new BasicPath("/", "test.properties"); ArchivePath locationTwo = new BasicPath("/", "test2.properties"); Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES); Asset assetTwo = new ClassLoaderAsset(NAME_TEST_PROPERTIES_2); archive.add(asset, location).add(assetTwo, locationTwo); Map<ArchivePath, Node> content = archive.getContent(); final Node node1 = content.get(location); final Node node2 = content.get(locationTwo); Assert.assertTrue("Asset should existing in content with key: " + location.get(), this.compareAssets(asset, node1.getAsset())); Assert.assertTrue("Asset should existing in content with key: " + locationTwo.get(), this.compareAssets(assetTwo, node2.getAsset())); }
/** * Ensure deleting an asset successfully removes asset from storage * * @throws Exception */ @Test public void testDeleteAssetWithArchivePath() throws Exception { Archive<T> archive = getArchive(); String resource = NAME_TEST_PROPERTIES; ArchivePath location = new BasicPath("/", "test.properties"); final Asset asset = new ClassLoaderAsset(resource); archive.add(asset, location); Assert.assertTrue(archive.contains(location)); // Sanity check Assert.assertEquals("Successfully deleting an Asset should return the removed Node", asset, archive.delete(location).getAsset()); Assert.assertFalse("There should no longer be an asset at: " + location.get() + " after deleted", archive.contains(location)); }
/** * @param archive * @return */ private String[] resolveWebArchiveNames(Archive<?> archive) { if (archive instanceof WebArchive) { return new String[] {createDeploymentName(archive.getName())}; } else if (archive instanceof EnterpriseArchive) { Map<ArchivePath, Node> webArchives = archive.getContent(Filters.include(".*\\.war")); List<String> deploymentNames = new ArrayList<String>(); for (ArchivePath path : webArchives.keySet()) { deploymentNames.add(createDeploymentName(path.get())); } return deploymentNames.toArray(new String[0]); } return new String[0]; }
/** * Ensure adding an asset to the path results in successful storage. * * @throws Exception */ @Test public void testAddAssetToPath() throws Exception { Archive<T> archive = getArchive(); Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES); ArchivePath location = new BasicPath("/", "test.properties"); archive.add(asset, location); Assert.assertTrue("Asset should be placed on " + location.get(), archive.contains(location)); }
private static Set<Node> filter(Map<ArchivePath, Node> oldNodes, Class<?>... newClasses) { Set<Node> nodes = new HashSet<Node>(); Map<String, Class<?>> classMap = toMap(newClasses); for (Node node : oldNodes.values()) { if (node.getAsset() == null) { continue; } String path = node.getPath().get(); if (path.charAt(0) == File.separatorChar) { path = path.substring(1, path.length()); } if (classMap.get(path) == null) { nodes.add(node); } } return nodes; }
try { JAXRSArchive deployment = a.as(JAXRSArchive.class).addModule("io.swagger"); for (Map.Entry<ArchivePath, Node> entry : deployment.getContent().entrySet()) { final ArchivePath key = entry.getKey(); if (key.get().endsWith(".class")) { String parentPath = key.getParent().get(); parentPath = parentPath.replaceFirst("/", "");
/** * Creates a new Path using the specified base and specified relative context. * * @param basePath * @param context */ public BasicPath(final ArchivePath basePath, final String context) { this(basePath.get(), context); }
@Override protected Archive<?> filter(Archive<?> archive) { final Set<ArtifactSpec> moduleSpecs = dependencyManager.getModuleDependencies(); final Set<ArtifactSpec> nonSwarmSpecs = dependencyManager.getNonSwarmDependencies(); archive.getContent().values().stream() .filter(node -> node.getPath().get().startsWith(WEB_INF_LIB)) .filter(node -> !nodeIsInArtifactList(node, nonSwarmSpecs, false) && (nodeIsInArtifactList(node, moduleSpecs, true) || nodeIsSwarmArtifact(node))) .forEach(node -> archive.delete(node.getPath())); return archive; }
private static Map<String, List<List<String>>> findDescriptorsFromDependencies(List<File> deps, List<String> patterns) { Map<String, List<List<String>>> map = new LinkedHashMap<>(); for (File file : deps) { JavaArchive archive = ShrinkWrap.createFromZipFile(JavaArchive.class, file); Map<ArchivePath, Node> content = getMatchingFilesFromJar(patterns, archive); for (Map.Entry<ArchivePath, Node> entry : content.entrySet()) { Asset asset = entry.getValue().getAsset(); if (asset != null) { List<String> lines; String path = entry.getKey().get(); lines = read(asset, path); List<List<String>> items = map.computeIfAbsent(path, k -> new ArrayList<>()); items.add(lines); map.put(path, items); } } } return map; }