BrooklynVersion implements BrooklynVersionService { public static Iterable<BrooklynFeature> getFeatures(ManagementContext mgmt) { if (OsgiUtil.isBrooklynInsideFramework()) { List<Bundle> bundles = Lists.newArrayList( FrameworkUtil.getBundle(BrooklynVersion.class) .getBundleContext() .getBundles() ); Maybe<OsgiManager> osgi = ((ManagementContextInternal)mgmt).getOsgiManager(); if (osgi.isPresentAndNonNull()) { for (ManagedBundle b: osgi.get().getManagedBundles().values()) { Maybe<Bundle> osgiBundle = osgi.get().findBundle(b); if (osgiBundle.isPresentAndNonNull()) { bundles.add(osgiBundle.get()); } } for (RegisteredType t: mgmt.getTypeRegistry().getAll()) { for (OsgiBundleWithUrl catalogBundle : t.getLibraries()) { Maybe<Bundle> osgiBundle = osgi.get().findBundle(catalogBundle); if (osgiBundle.isPresentAndNonNull()) { bundles.add(osgiBundle.get()); } } } } ImmutableSet.Builder<BrooklynFeature> features = ImmutableSet.builder(); for(Bundle bundle : bundles) { Optional<BrooklynFeature> fs = BrooklynFeature.newFeature(bundle.getHeaders()); if (fs.isPresent()) { features.add(fs.get()); } } return features.build(); } else { Set<URL> manifests = MutableSet.copyOf(ResourceUtils.create(mgmt).getResources(MANIFEST_PATH)); Maybe<OsgiManager> osgi = ((ManagementContextInternal)mgmt).getOsgiManager(); if (osgi.isPresentAndNonNull()) { Iterables.addAll(manifests, osgi.get().getResources(MANIFEST_PATH, osgi.get().getManagedBundles().values()) ); } for (RegisteredType t: mgmt.getTypeRegistry().getAll()) { OsgiBrooklynClassLoadingContext osgiContext = new OsgiBrooklynClassLoadingContext(mgmt, t.getId(), t.getLibraries()); Iterables.addAll(manifests, osgiContext.getResources(MANIFEST_PATH)); } ImmutableSet.Builder<BrooklynFeature> features = ImmutableSet.builder(); for (URL manifest : manifests) { ManifestHelper mh = null; try { mh = ManifestHelper.forManifest(manifest); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.debug("Error reading OSGi manifest from " + manifest + " when determining version properties: " + e, e); } if (mh == null) continue; Attributes attrs = mh.getManifest().getMainAttributes(); Optional<BrooklynFeature> fs = BrooklynFeature.newFeature(attrs); if (fs.isPresent()) { features.add(fs.get()); } } return features.build(); } } private  BrooklynVersion(); @Override void checkVersions(); @Override @Nullable String getVersionFromMavenProperties(); @Override @Nullable String getVersionFromOsgiManifest(); @Override @Nullable /** SHA1 of the last commit to brooklyn at the time this build was made. * For SNAPSHOT builds of course there may have been further non-committed changes. */ String getSha1FromOsgiManifest(); @Override String getVersion(); @Override boolean isSnapshot(); static boolean isDevelopmentEnvironment(); @Override void logSummary(); static String get(); static BrooklynVersion getInstance(); static Iterable<BrooklynFeature> getFeatures(ManagementContext mgmt); static String getOsgiVersion(); static final BrooklynVersion INSTANCE; }
@Test public void testGetFeatures() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); LocalManagementContext mgmt = LocalManagementContextForTests.builder(true) .enableOsgiReusable() .build(); String symName = "org.apache.brooklyn.test.resources.osgi.brooklyn-test-osgi-entities"; String version = "0.1.0"; String type = "brooklyn.osgi.tests.SimpleEntity"; List<String> libraries = Lists.newArrayList("classpath:" + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); OsgiTestBase.preinstallLibrariesLowLevelToPreventCatalogBomParsing(mgmt, libraries.toArray(new String[1])); CatalogEntityItemDto c1 = CatalogItemBuilder.newEntity(symName, version) .javaType(type) .libraries(CatalogItemDtoAbstract.parseLibraries(libraries)) .build(); mgmt.getCatalog().addItem(c1); Iterable<BrooklynVersion.BrooklynFeature> features = BrooklynVersion.getFeatures(mgmt); assertTrue(features.iterator().hasNext()); boolean found = false; Iterator<BrooklynVersion.BrooklynFeature> iterator = features.iterator(); while (!found && iterator.hasNext()) { BrooklynVersion.BrooklynFeature feature = iterator.next(); if (feature.getSymbolicName().equals(symName) && feature.getVersion().equals(version)) { found = true; } } assertTrue(found, "Expected to find " + symName + ":" + version + " in: " + Iterables.toString(features)); }