/** * {@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(); }
static void addModule(final File modulesDir, String moduleName, InputStream moduleXml, Map<String, StreamExporter> resources) throws IOException { String modulePath = moduleName.replace('.', File.separatorChar) + File.separatorChar + "main"; File moduleDir = new File(modulesDir, modulePath); moduleDir.mkdirs(); FileUtils.copyFile(moduleXml, new File(moduleDir, "module.xml")); for (Map.Entry<String, StreamExporter> entry : resources.entrySet()) { entry.getValue().exportTo(new File(moduleDir, entry.getKey()), true); } }
/** * {@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(); }
static void addModule(final File modulesDir, String moduleName, InputStream moduleXml, Map<String, StreamExporter> resources) throws IOException { String modulePath = moduleName.replace('.', File.separatorChar) + File.separatorChar + "main"; File moduleDir = new File(modulesDir, modulePath); moduleDir.mkdirs(); FileUtils.copyFile(moduleXml, new File(moduleDir, "module.xml")); for (Map.Entry<String, StreamExporter> entry : resources.entrySet()) { entry.getValue().exportTo(new File(moduleDir, entry.getKey()), true); } }
public static void createExtensionModule(File extensionModuleRoot, Class<? extends Extension> extension, Package... additionalPackages) throws IOException { deleteRecursively(extensionModuleRoot.toPath()); if (extensionModuleRoot.exists() && !extensionModuleRoot.isDirectory()) { throw new IllegalArgumentException(extensionModuleRoot + " already exists and is not a directory"); } File file = new File(extensionModuleRoot, "main"); if (!file.mkdirs() && !file.exists()) { throw new IllegalArgumentException("Could not create " + file); } final InputStream is = createResourceRoot(extension, additionalPackages).exportAsInputStream(); try { copyFile(new File(file, JAR_NAME), is); } finally { IoUtils.safeClose(is); } URL url = extension.getResource("module.xml"); if (url == null) { throw new IllegalStateException("Could not find module.xml"); } copyFile(new File(file, "module.xml"), url.openStream()); }
/** * Exports the specified archive to an {@link OutputStream} * * @param archive * @return */ private void exportToOutputStream(final Archive<?> archive, final OutputStream out) { assert archive != null : "archive must be specified"; assert out != null : "outstream must be specified"; // Export final Class<? extends StreamExporter> exporter = this.getExporterClass(); assert exporter != null : "Exporter class must be specified"; try { archive.as(exporter).exportTo(out); } finally { try { out.close(); } catch (final IOException ioe) { log.warning("Could not close " + out + ": " + ioe); } } }
/** * Exports the specified archive as an {@link InputStream} */ private InputStream exportAsInputStream(final Archive<?> archive) { assert archive != null : "archive must be specified"; final Class<? extends StreamExporter> exporter = this.getExporterClass(); assert exporter != null : "Exporter class must be specified"; return archive.as(this.getExporterClass()).exportAsInputStream(); }
/** * Exports the specified archive as a {@link File}, overwriting an existing one is specified * * @param archive * @param file * @param overwrite */ private void exportAsFile(final Archive<?> archive, final File file, final boolean overwrite) { // Precondition checks assert file != null : "file must be specified"; assert archive != null : "archive must be specified"; // Export final Class<? extends StreamExporter> exporter = this.getExporterClass(); assert exporter != null : "Exporter class must be specified"; archive.as(exporter).exportTo(file, overwrite); }
/** * Ensures that we may import an archive, add content to it, export, and that the added content has been reflected * to the exported view. * * @throws Exception */ @Test public void shouldBeAbleToImportAddAndExport() throws Exception { // Get the delegate final ContentAssertionDelegateBase delegate = this.getDelegate(); assert delegate != null : "Delegate must be specified by implementations"; final File testFile = delegate.getExistingResource(); // Import from file final Class<? extends StreamImporter<?>> importerClass = this.getImporterClass(); assert importerClass != null : "Importer class must be specified by implementations"; final Archive<?> archive = ShrinkWrap.create(importerClass, "test.jar").importFrom(testFile) .as(JavaArchive.class); Assert.assertNotNull("Should not return a null archive", archive); // Add a new resource archive.add(new ClassLoaderAsset(EXISTING_RESOURCE), ArchivePaths.create("test.properties")); // Export File tempFile = new File("target/testOutput"); tempFile.deleteOnExit(); final Class<? extends StreamExporter> exporterClass = this.getExporterClass(); Assert.assertNotNull("Exporter class must be specified by implementations", exporterClass); final InputStream stream = archive.as(exporterClass).exportAsInputStream(); IOUtil.copyWithClose(stream, new FileOutputStream(tempFile)); // Ensure the exported view matches that of the archive delegate.assertContent(archive, tempFile); }