/** create a copy of the given ZIP as a JAR with the given entries added at the end, returning the new temp file */ public File copyAddingAtEnd(File f, Map<ZipEntry, ? extends InputStream> entries) { return copyAdding(f, entries, Predicates.<String>alwaysTrue(), true); }
/** as {@link #copyAddingManifest(File, Manifest)} but taking manifest entries as a map for convenience */ public File copyAddingManifest(File f, Map<String,String> attrs) { return copyAddingManifest(f, manifestOf(attrs)); }
/** create a copy of the given ZIP as a JAR with the given entries removed, returning the new temp file */ public File copyRemoving(File f, final Set<String> itemsToRemove) { return copyRemoving(f, new Predicate<String>(){ @Override public boolean apply(String input) { return !itemsToRemove.contains(input); } }); }
public File createTempBundle(String nameHint, Map<String, String> mf, Map<ZipEntry, InputStream> files) { return createTempBundle(nameHint, manifestOf(mf), files); }
@Test public void testCatalogBomFromBundleWithManualManifest() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); jf = bm.copyRemoving(jf, MutableSet.of(JarFile.MANIFEST_NAME)); String customName = "catalog-bundle-1-manual-"+Identifiers.makeRandomId(4); jf = bm.copyAddingManifest(jf, MutableMap.of( "Manifest-Version", "2.0", "Bundle-SymbolicName", customName, "Bundle-Version", "0.0.0.SNAPSHOT")); Assert.assertTrue(bm.hasOsgiManifest(jf)); installBundle(jf); assertHasBasic1(); Entity basic1 = assertBasic1DeploysAndHasSensor(); RegisteredType item = mgmt().getTypeRegistry().get( basic1.getCatalogItemId() ); Collection<OsgiBundleWithUrl> libs = item.getLibraries(); Asserts.assertSize(libs, 1); Assert.assertEquals(Iterables.getOnlyElement(libs).getSymbolicName(), customName); }
@Test public void testCopyRemovingItemsUnmatched() throws Exception { tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of(new ZipEntry("myfile.txt"), new ByteArrayInputStream("mytext".getBytes()))); generatedJar = bundleMaker.copyRemoving(tempJar, ImmutableSet.of("wrong.txt")); assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", "mytext"), false); }
static void installJavaScanningMoreEntitiesV2(ManagementContext mgmt, Object context) throws FileNotFoundException { // scanning bundle functionality added in 0.12.0, relatively new compared to non-osgi scanning TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); CampYamlLiteTest.installWithoutCatalogBom(mgmt, OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); BundleMaker bm = new BundleMaker(mgmt); File f = Os.newTempFile(context.getClass(), "jar"); Streams.copy(ResourceUtils.create(context).getResourceFromUrl(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH), new FileOutputStream(f)); f = bm.copyRemoving(f, MutableSet.of("catalog.bom")); f = bm.copyAdding(f, MutableMap.of(new ZipEntry("catalog.bom"), new ByteArrayInputStream( Strings.lines( "brooklyn.catalog:", " scanJavaAnnotations: true").getBytes() ) )); ((ManagementContextInternal)mgmt).getOsgiManager().get().install(new FileInputStream(f)).checkNoError(); }
private String copyResourcePathToTempPath(String resourcePath) { BundleMaker bm = new BundleMaker(ResourceUtils.create(this)); bm.setDefaultClassForLoading(CleanOrphanedLocationsIntegrationTest.class); File jar = bm.createJarFromClasspathDir(resourcePath); File output = Os.newTempDir("brooklyn-test-resouce-from-"+resourcePath); try { ArchiveUtils.extractZip(new ZipFile(jar), output.getAbsolutePath()); return output.getAbsolutePath(); } catch (Exception e) { throw Exceptions.propagate(e); } }
public static void addCatalogItemsAsOsgiInUsualWay(ManagementContext mgmt, String catalogYaml, VersionedName bundleName, boolean force) { try { BundleMaker bundleMaker = new BundleMaker(mgmt); File bf = bundleMaker.createTempZip("test", MutableMap.of( new ZipEntry(BasicBrooklynCatalog.CATALOG_BOM), new ByteArrayInputStream(catalogYaml.getBytes()))); if (bundleName!=null) { bf = bundleMaker.copyAddingManifest(bf, MutableMap.of( "Manifest-Version", "2.0", Constants.BUNDLE_SYMBOLICNAME, bundleName.getSymbolicName(), Constants.BUNDLE_VERSION, bundleName.getOsgiVersion().toString())); } ReferenceWithError<OsgiBundleInstallationResult> b = ((ManagementContextInternal)mgmt).getOsgiManager().get().install( new FileInputStream(bf) ); b.checkNoError(); } catch (Exception e) { throw Exceptions.propagate(e); } }
@Test public void testCatalogBomLoadsFileInBundle() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); // add a file in the bundle String customText = "Sample data "+Identifiers.makeRandomId(4); jf = bm.copyAdding(jf, MutableMap.of( new ZipEntry("sample.txt"), (InputStream) new ByteArrayInputStream(customText.getBytes()))); installBundle(jf); String yaml = Strings.lines("name: simple-app-yaml", "services:", "- type: " + "basic1", " brooklyn.initializers:", " - type: "+GetFileContentsEffector.class.getName()); Entity app = createAndStartApplication(yaml); Entity basic1 = Iterables.getOnlyElement( app.getChildren() ); // check the file put in the bundle gets loaded without needing to do anything special String contents = basic1.invoke(GetFileContentsEffector.GET_FILE_CONTENTS, MutableMap.of(GetFileContentsEffector.FILENAME.getName(), "classpath://sample.txt")).get(); Asserts.assertEquals(contents, customText); }
@Test public void testCatalogBomFromBundleWithManifest() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); Assert.assertTrue(bm.hasOsgiManifest(jf)); installBundle(jf); assertHasBasic1(); assertBasic1DeploysAndHasSensor(); }
File zf2 = new BundleMaker(mgmt()).copyAddingManifest(zipFile, discoveredManifest); zipFile.delete(); zipFile = zf2;
@Test public void testHasOsgiManifestWhenValidManifest() throws Exception { Map<String, String> manifest = ImmutableMap.of(Attributes.Name.MANIFEST_VERSION.toString(), "1.2.3", Constants.BUNDLE_SYMBOLICNAME, "myname"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); assertTrue(bundleMaker.hasOsgiManifest(generatedJar)); }
mf.getMainAttributes().putValue(BROOKLYN_WRAPPED_BOM_BUNDLE, Boolean.TRUE.toString()); BundleMaker bm = new BundleMaker(mgmt); File bf = bm.createTempBundle(vn.getSymbolicName(), mf, MutableMap.of( new ZipEntry(CATALOG_BOM), (InputStream) new ByteArrayInputStream(yaml.getBytes())) );
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { mgmt = LocalManagementContextForTests.builder(true).enableOsgiReusable().build(); super.setUp(); bundleMaker = new BundleMaker(mgmt); emptyJar = createEmptyJarFile(); }
private void discoverManifestFromCatalogBom(boolean isCatalogBomRequired) { discoveredManifest = new BundleMaker(mgmt()).getManifest(zipFile); ZipFile zf = null; try {
@Test public void testCopyAddingManifest() throws Exception { Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.2.3"); // needs version, or nothing added to manifest! manifest.getMainAttributes().putValue("mykey", "myval"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); String expectedManifest = "Manifest-Version: 1.2.3\r\n" + "mykey: myval\r\n" + "\r\n"; assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, expectedManifest), false); }
public static void addCatalogItemsAsOsgiWithoutStartingBundles(ManagementContext mgmt, String catalogYaml, VersionedName bundleName, boolean force) { try { BundleMaker bundleMaker = new BundleMaker(mgmt); File bf = bundleMaker.createTempZip("test", MutableMap.of( new ZipEntry(BasicBrooklynCatalog.CATALOG_BOM), new ByteArrayInputStream(catalogYaml.getBytes()))); ReferenceWithError<OsgiBundleInstallationResult> b = ((ManagementContextInternal)mgmt).getOsgiManager().get().installDeferredStart( new BasicManagedBundle(bundleName.getSymbolicName(), bundleName.getVersionString(), null), new FileInputStream(bf), false); // bundle not started (no need, and can break), and BOM not installed nor validated above; // do BOM install and validation below manually to test the type registry approach // but skipping the rollback / uninstall mgmt.getCatalog().addTypesFromBundleBom(catalogYaml, b.get().getMetadata(), force, null); Map<RegisteredType, Collection<Throwable>> validation = mgmt.getCatalog().validateTypes( mgmt.getTypeRegistry().getMatching(RegisteredTypePredicates.containingBundle(b.get().getVersionedName())) ); if (!validation.isEmpty()) { throw Exceptions.propagate("Brooklyn failed to load types (in tests, skipping rollback): "+validation.keySet(), Iterables.concat(validation.values())); } } catch (Exception e) { throw Exceptions.propagate(e); } }
@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); }
@SuppressWarnings("deprecation") @Test public void testInstallBundle() throws Exception { Map<String, String> manifest = ImmutableMap.of( Attributes.Name.MANIFEST_VERSION.toString(), "1.2.3", Constants.BUNDLE_VERSION, "4.5.6", Constants.BUNDLE_SYMBOLICNAME, "myname"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); Bundle bundle = bundleMaker.installBundle(generatedJar, false); assertEquals(bundle.getSymbolicName(), "myname"); assertEquals(bundle.getVersion(), new Version("4.5.6")); // Confirm it really is installed in the management context's OSGi framework Bundle bundle2 = Osgis.bundleFinder(mgmt.getOsgiManager().get().getFramework()) .symbolicName("myname") .version("4.5.6") .findUnique() .get(); assertEquals(bundle2, bundle); }