Refine search
@Override public ClassLoader getClassLoader(final JavaArchive jar) throws Exception { WebArchive war = ShrinkWrap.create(WebArchive.class); war.setManifest(Thread.currentThread().getContextClassLoader().getResource("META-INF/MANIFEST.MF")); war.addAsLibrary(jar); assertTrue(tmp.delete()); war.as(ZipExporter.class).exportTo(tmp); final byte[] bytes = Utils.readAsBytes(jar.get("foo/A.class").getAsset().openStream()); return new ClassLoader(Thread.currentThread().getContextClassLoader()) { Class<?> aClass = null;
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);
@Override public Manifest getManifest() throws IOException { ArchivePath manifestPath = ArchivePaths.create("META-INF/MANIFEST.MF"); final Archive<?> archive = this.getArchive(); if (archive.contains(manifestPath)) { return new Manifest(archive.get(manifestPath).getAsset().openStream()); } return null; }
@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")); }
/** * 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())); }
public static Manifest getManifest(Archive<?> archive) { try { Node node = archive.get(JarFile.MANIFEST_NAME); if (node != null && node.getAsset() != null) { return new Manifest(node.getAsset().openStream()); } } catch (Exception ex) { throw new IllegalStateException("Cannot obtain manifest", ex); } return null; }
private static InputStream getKeycloakJsonFromClasspath(String resourceName) { InputStream keycloakJson = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); if (keycloakJson == null) { String appArtifact = System.getProperty(BootstrapProperties.APP_ARTIFACT); if (appArtifact != null) { try (InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("_bootstrap/" + appArtifact)) { Archive<?> tmpArchive = ShrinkWrap.create(JARArchive.class); tmpArchive.as(ZipImporter.class).importFrom(in); Node jsonNode = tmpArchive.get(resourceName); if (jsonNode == null) { jsonNode = getKeycloakJsonNodeFromWebInf(tmpArchive, resourceName, true); } if (jsonNode == null) { jsonNode = getKeycloakJsonNodeFromWebInf(tmpArchive, resourceName, false); } if (jsonNode != null && jsonNode.getAsset() != null) { keycloakJson = jsonNode.getAsset().openStream(); } } catch (IOException e) { // ignore } } } return keycloakJson; }
private void format(StringBuilder sb, Node node) { sb.append(node.getPath().get()); if (node.getAsset() == null) { sb.append(FormattingConstants.SLASH); } sb.append(FormattingConstants.NEWLINE); for (Node child : node.getChildren()) { format(sb, child); } }
@Test public void shouldMoveDirectory() { final Archive<JavaArchive> archive = ShrinkWrap.create(JavaArchive.class, "archive.jar"); final String sourcePath = "path1"; final String targetPath = "path2"; archive.addAsDirectory(sourcePath); archive.move(sourcePath, targetPath); Assert.assertTrue("Directory should be at the new path", archive.get(targetPath).getAsset() == null); }
/** * 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); }
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; }
public static EnterpriseArchive createSsoEar() { ClassLoader tccl = Thread.currentThread().getContextClassLoader(); String resourcesLocation = "org/jboss/as/test/integration/web/sso/resources/"; WebArchive war1 = createSsoWar("sso-form-auth1.war"); WebArchive war2 = createSsoWar("sso-form-auth2.war"); WebArchive war3 = createSsoWar("sso-with-no-auth.war"); // Remove jboss-web.xml so the war will not have an authenticator war3.delete(war3.get("WEB-INF/jboss-web.xml").getPath()); JavaArchive webEjbs = ShrinkWrap.create(JavaArchive.class, "jbosstest-web-ejbs.jar"); webEjbs.addAsManifestResource(tccl.getResource(resourcesLocation + "ejb-jar.xml"), "ejb-jar.xml"); webEjbs.addAsManifestResource(tccl.getResource(resourcesLocation + "jboss.xml"), "jboss.xml"); webEjbs.addPackage(StatelessSession.class.getPackage()); EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "web-sso.ear"); ear.setApplicationXML(tccl.getResource(resourcesLocation + "application.xml")); ear.addAsModule(war1); ear.addAsModule(war2); ear.addAsModule(war3); ear.addAsModule(webEjbs); return ear; }
public static WebArchive createDeployment(String packageName) throws IOException { WebArchive war = createPortletDeployment(packageName); Node node = war.get("WEB-INF/portlet.xml"); ArchivePath path = node.getPath(); String s = Tools.read(node.getAsset().openStream(), Tools.UTF_8); s = s.replace("<portlet-info>", "<resource-bundle>bundle</resource-bundle>" + "<portlet-info>"); war.delete(path); war.add(new StringAsset(s), path); war.addAsResource(new StringAsset("abc=def"), "bundle_fr_FR.properties"); return war; }
@Override public Enumeration<String> entries() { List<String> entries = new ArrayList<String>(); for (Entry<ArchivePath, Node> entry : this.getArchive().getContent().entrySet()) { if (entry.getValue().getAsset() != null) { entries.add(entry.getKey().get()); } } return Collections.enumeration(entries); }
@Override public String format(final Archive<?> archive) throws IllegalArgumentException { // Precondition checks if (archive == null) { throw new IllegalArgumentException("archive must be specified"); } // Start the output with the name of the archive StringBuilder sb = new StringBuilder(archive.getName()).append(FormattingConstants.COLON).append( FormattingConstants.NEWLINE); // format recursively, except the parent Node rootNode = archive.get(ROOT); for (Node child : rootNode.getChildren()) { format(sb, child); } // remove the last NEWLINE sb.deleteCharAt(sb.length() - 1); return sb.toString(); }
@Override public void undeploy(Archive<?> archive) throws DeploymentException { try { Manifest manifest = new Manifest(archive.get("/META-INF/MANIFEST.MF").getAsset().openStream()); OSGiMetaData metadata = OSGiMetaDataBuilder.load(manifest); undeploy(metadata.getBundleSymbolicName()); } catch (IOException e) { throw new DeploymentException("Cannot undeploy: " + archive.getName(), e); } }
@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 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()); } } } }
/** * 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 move() throws IOException { final String source = "path"; final String contents = "contents"; this.getArchive().add(new StringAsset(contents), source); final String dest = "newPath"; final Path src = fs.getPath(source); final Path dst = fs.getPath(dest); final Path moved = Files.move(src, dst); Assert.assertEquals(dest, moved.toString()); final String roundtrip = new BufferedReader(new InputStreamReader(this.getArchive().get(dest).getAsset() .openStream())).readLine(); Assert.assertEquals("Contents not as expected after move", contents, roundtrip); }