BundleMaker { public File createJarFromClasspathDir(String path) { File f = Os.newTempFile(path, "zip"); ZipOutputStream zout = null; try { if (Urls.getProtocol(path)==null) { path = Os.tidyPath(path); if (!path.startsWith("/") && optionalDefaultClassForLoading!=null) { path = "/"+optionalDefaultClassForLoading.getPackage().getName().replace('.', '/') + "/" + path; } path = "classpath:"+path; } if (resources.doesUrlExist(Urls.mergePaths(path, MANIFEST_PATH))) { InputStream min = resources.getResourceFromUrl(Urls.mergePaths(path, MANIFEST_PATH)); zout = new JarOutputStream(new FileOutputStream(f), new Manifest(min)); addUrlItemRecursively(zout, path, path, Predicates.not(Predicates.equalTo(MANIFEST_PATH))); } else { zout = new JarOutputStream(new FileOutputStream(f)); addUrlItemRecursively(zout, path, path, Predicates.alwaysTrue()); } return f; } catch (Exception e) { throw Exceptions.propagateAnnotated("Error creating ZIP from classpath spec "+path, e); } finally { Streams.closeQuietly(zout); } } BundleMaker(@Nonnull ResourceUtils resources);  BundleMaker(@Nonnull Framework f, @Nonnull ResourceUtils resources);  BundleMaker(@Nonnull ManagementContext mgmt); void setDefaultClassForLoading(Class<?> optionalDefaultClassForLoading); File createJarFromClasspathDir(String path); boolean hasOsgiManifest(File f); Manifest getManifest(File f); File copyAddingManifest(File f, Map<String,String> attrs); File copyAddingManifest(File f, Manifest mf); File copyAdding(File f, Map<ZipEntry, ? extends InputStream> entries); File copyAddingAtEnd(File f, Map<ZipEntry, ? extends InputStream> entries); File copyRemoving(File f, final Set<String> itemsToRemove); File copyRemoving(File f, Predicate<? super String> filter); @Deprecated Bundle installBundle(File f, boolean start); File createTempBundle(String nameHint, Manifest mf, Map<ZipEntry, InputStream> files); File createTempBundle(String nameHint, Map<String, String> mf, Map<ZipEntry, InputStream> files); File createTempZip(String nameHint, Map<ZipEntry, InputStream> files);  }
@Test public void testCreateJarFromClasspathDirNoManifest() throws Exception { generatedJar = bundleMaker.createJarFromClasspathDir("/org/apache/brooklyn/util/core/osgi/test/bundlemaker/nomanifest"); assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", "mytext", "subdir/myfile2.txt", "mytext2"), false); }
@Test public void testCreateJarFromClasspathDirWithManifest() throws Exception { generatedJar = bundleMaker.createJarFromClasspathDir("/org/apache/brooklyn/util/core/osgi/test/bundlemaker/withmanifest"); String expectedManifest = "Manifest-Version: 1.2.3\r\n" + "mykey: myval\r\n" + "\r\n"; assertJarContents(generatedJar, ImmutableMap.of( JarFile.MANIFEST_NAME, expectedManifest, "myfile.txt", "mytext", "myemptyfile.txt", "", "subdir/myfile2.txt", "mytext2"), false); }