String bundleUrl = bundleMetadata.getUrl(); if (bundleUrl != null) { Maybe<Bundle> installedBundle = Osgis.bundleFinder(framework).requiringFromUrl(bundleUrl).find(); if (installedBundle.isPresent()) { Bundle b = installedBundle.get(); Maybe<Bundle> installedBundle; if (bundleMetadata.isNameResolved()) { installedBundle = Osgis.bundleFinder(framework).symbolicName(bundleMetadata.getSymbolicName()).version(bundleMetadata.getSuppliedVersionString()).find(); } else { installedBundle = Maybe.absent("Bundle metadata does not have URL nor does it have both name and version");
protected Maybe<Bundle> findOne(boolean requireExactlyOne) { if (symbolicName==null && url==null) throw new IllegalStateException(this+" must be given either a symbolic name or a URL"); List<Bundle> result = findAll(); if (result.isEmpty()) return Maybe.absent("No bundle matching "+getConstraintsDescription()); if (requireExactlyOne && result.size()>1) return Maybe.absent("Multiple bundles ("+result.size()+") matching "+getConstraintsDescription()); // take the highest version of the first symbolic name alphabetically Bundle r1 = result.get(0); for (int i=1; i<result.size(); i++) { if (result.get(i).getSymbolicName().equals(r1.getSymbolicName())) { r1 = result.get(i); } else { // was in order so no more symbolic names break; } } return Maybe.of(r1); }
/** @deprecated since 0.7.0 use {@link #bundleFinder(Framework)} */ @Deprecated public static Maybe<Bundle> getBundle(Framework framework, String symbolicName, Version version) { return bundleFinder(framework).symbolicName(symbolicName).version(Predicates.equalTo(version)).findUnique(); }
result.metadata = inferredMetadata; Maybe<Bundle> b = Osgis.bundleFinder(osgiManager.framework).symbolicName(result.getMetadata().getSymbolicName()).version(result.getMetadata().getSuppliedVersionString()).find(); if (b.isPresent()) {
installedBundle = Osgis.bundleFinder(osgiManager.framework).requiringFromUrl(suppliedKnownBundleMetadata.getOsgiUniqueUrl()).find(); installedBundle = Osgis.bundleFinder(osgiManager.framework).requiringFromUrl(suppliedKnownBundleMetadata.getUrl()).find(); installedBundle = Osgis.bundleFinder(osgiManager.framework).symbolicName(suppliedKnownBundleMetadata.getSymbolicName()).version(suppliedKnownBundleMetadata.getSuppliedVersionString()).find();
.symbolicName(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_SYMBOLIC_NAME_FULL).findAll(); Assert.assertEquals(bundles.size(), 3, "Wrong list of bundles: "+bundles); .symbolicName(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_SYMBOLIC_NAME_FULL).find(); Assert.assertTrue(preferredVersion.isPresent()); Assert.assertEquals(preferredVersion.get().getVersion().toString(), "0.2.0"); preferringFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_EVIL_TWIN_URL).find(); Assert.assertTrue(evilVersion.isPresent()); Assert.assertEquals(evilVersion.get().getVersion().toString(), "0.2.0"); .symbolicName(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_SYMBOLIC_NAME_FULL) .version(TEST_VERSION) .preferringFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_EVIL_TWIN_URL).find(); Assert.assertTrue(versionIgnoresInvalidPreferredUrl.isPresent()); Assert.assertEquals(versionIgnoresInvalidPreferredUrl.get().getVersion().toString(), TEST_VERSION); .symbolicName(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_SYMBOLIC_NAME_FULL) .version(TEST_VERSION) .requiringFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_EVIL_TWIN_URL).find(); Assert.assertFalse(versionRespectsInvalidRequiredUrl.isPresent());
if (catalogBundle.getUrl() != null) { BundleFinder bundleFinder = Osgis.bundleFinder(framework); bundleFinder.requiringFromUrl(catalogBundle.getUrl()); result = bundleFinder.find(); if (result.isPresent()) { return result; bundleFinder.requiringFromUrl(null); bundleFinder.symbolicName(mb.getSymbolicName()).version(mb.getSuppliedVersionString()); result = bundleFinder.find(); if (result.isPresent()) { return result; bundleFinder.symbolicName(catalogBundle.getSymbolicName()).version(catalogBundle.getSuppliedVersionString()); return bundleFinder.find();
@Test public void testFindByNameAndVersionRange() throws Exception { List<Bundle> result = Osgis.bundleFinder(framework) .symbolicName("mybundle") .version("[1.0.0, 1.1.0)") .findAll(); assertEquals(result, ImmutableList.of(myBundle1_0_0)); .symbolicName("mybundle") .version("[1.0.0, 2.0.0)") .findAll(); assertEquals(result2, ImmutableList.of(myBundle1_0_0, myBundle1_1_0)); .symbolicName("mybundle") .version("[1.0.0, 2.0.0]") .findAll(); assertEquals(result3, ImmutableList.of(myBundle1_0_0, myBundle1_1_0, myBundle2_0_0)); .symbolicName("mybundle") .version("[2.0.0, 3.0.0)") .findAll(); assertEquals(result4, ImmutableList.of(myBundle2_0_0_snapshot, myBundle2_0_0)); .symbolicName("snapshotbundle") .version("[1.0.0, 2.0.0)") .findAll(); assertEquals(result5, ImmutableList.of(snapshotBundle1_0_0_snapshot));
@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); }
void checkCorrectlyInstalled(OsgiBundleWithUrl bundle, Bundle b) { String nv = b.getSymbolicName()+":"+b.getVersion().toString(); if (!isBundleNameEqualOrAbsent(bundle, b)) { throw new IllegalStateException("Bundle already installed as "+nv+" but user explicitly requested "+bundle); } List<Bundle> matches = Osgis.bundleFinder(framework) .symbolicName(b.getSymbolicName()) .version(b.getVersion().toString()) .findAll(); if (matches.isEmpty()) { log.error("OSGi could not find bundle "+nv+" in search after installing it from "+bundle); } else if (matches.size()==1) { log.debug("Bundle from "+bundle.getUrl()+" successfully installed as " + nv + " ("+b+")"); } else { log.warn("OSGi has multiple bundles matching "+nv+", when installing "+bundle+"; not guaranteed which versions will be consumed"); // TODO for snapshot versions we should indicate which one is best to use } }
protected <T> Maybe<T> tryLoadFromBundle(LoaderDispatcher<T> dispatcher, String symbolicName, String version, String name) { Framework framework = getFramework(); if (framework != null) { Maybe<Bundle> bundle = Osgis.bundleFinder(framework) .symbolicName(symbolicName) .version(version) .find(); if (bundle.isAbsent()) { throw new IllegalStateException("Bundle " + toBundleString(symbolicName, version) + " not found to load " + name); } return dispatcher.tryLoadFrom(bundle.get(), name); } else { log.warn("Request for bundle '"+symbolicName+"' "+(Strings.isNonBlank(version) ? "("+version+") " : "")+"will be ignored as no framework available; will look for '"+name+"' in plain old classpath"); return dispatcher.tryLoadFrom(classLoader, name); } }
protected <T> Maybe<T> tryLoadFromBundleWhiteList(LoaderDispatcher<T> dispatcher, String className) { Framework framework = getFramework(); if (framework == null) { return Maybe.absentNull(); } List<Bundle> bundles = Osgis.bundleFinder(framework) .satisfying(createBundleMatchingPredicate()) .findAll(); for (Bundle b : bundles) { Maybe<T> item = dispatcher.tryLoadFrom(b, className); if (item.isPresent()) { return item; } } return Maybe.absentNull(); }
@Override public String toString() { return getClass().getCanonicalName()+"["+getConstraintsDescription()+"]"; }
/** @deprecated since 0.7.0 use {@link #bundleFinder(Framework)} */ @Deprecated public static List<Bundle> getBundlesByName(Framework framework, String symbolicName) { return bundleFinder(framework).symbolicName(symbolicName).findAll(); }
/** @deprecated since 0.7.0 use {@link #bundleFinder(Framework)} */ @Deprecated public static Maybe<Bundle> getBundle(Framework framework, String symbolicName, String version) { return bundleFinder(framework).symbolicName(symbolicName).version(version).find(); }
private Bundle getBundle(ManagementContext mgmt, final String symbolicName) throws Exception { OsgiManager osgiManager = ((ManagementContextInternal)mgmt).getOsgiManager().get(); Framework framework = osgiManager.getFramework(); Maybe<Bundle> result = Osgis.bundleFinder(framework) .symbolicName(symbolicName) .find(); return result.get(); }
@Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { int separator = name.indexOf(":"); if (separator>=0) { Bundle bundle = Osgis.bundleFinder(framework).symbolicName(name.substring(0, separator)).find().get(); return bundle.loadClass(name.substring(separator+1)); } else { return super.loadClass(name, resolve); } } }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { int separator = name.indexOf(":"); if (separator>=0) { Bundle bundle = Osgis.bundleFinder(framework).symbolicName(name.substring(0, separator)).find().get(); return bundle.loadClass(name.substring(separator+1)); } else { return super.findClass(name); } }
@Test public void testFindAllByNameOnly() throws Exception { List<Bundle> result = Osgis.bundleFinder(framework) .symbolicName("mybundle") .findAll(); assertEquals(result, ImmutableList.of(myBundle2_0_0_snapshot, myBundle1_0_0, myBundle1_1_0, myBundle2_0_0)); } }
private Bundle getBundle(ManagementContext mgmt, final String symbolicName) throws Exception { OsgiManager osgiManager = ((ManagementContextInternal)mgmt).getOsgiManager().get(); Framework framework = osgiManager.getFramework(); Maybe<Bundle> result = Osgis.bundleFinder(framework) .symbolicName(symbolicName) .find(); return result.get(); }