add library bundles directly, without munging yaml, and only if not wrapper
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/dc43f03f Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/dc43f03f Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/dc43f03f Branch: refs/heads/master Commit: dc43f03fe2fa269e49a7865eafb491430f4e7e32 Parents: e32d74d Author: Alex Heneveld <[email protected]> Authored: Wed Jun 28 09:46:13 2017 +0100 Committer: Alex Heneveld <[email protected]> Committed: Wed Jun 28 09:46:13 2017 +0100 ---------------------------------------------------------------------- .../catalog/internal/BasicBrooklynCatalog.java | 36 ++++++----- .../catalog/internal/CatalogBundleLoader.java | 68 +------------------- 2 files changed, 24 insertions(+), 80 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/dc43f03f/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java index 8d36b68..5a8c31a 100644 --- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java +++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java @@ -54,7 +54,6 @@ import org.apache.brooklyn.core.catalog.CatalogPredicates; import org.apache.brooklyn.core.catalog.internal.CatalogClasspathDo.CatalogScanningModes; import org.apache.brooklyn.core.location.BasicLocationRegistry; import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult; -import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult.ResultCode; import org.apache.brooklyn.core.mgmt.ha.OsgiManager; import org.apache.brooklyn.core.mgmt.internal.CampYamlParser; import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; @@ -542,18 +541,25 @@ public class BasicBrooklynCatalog implements BrooklynCatalog { // brooklyn.libraries we treat specially, to append the list, with the child's list preferred in classloading order // `libraries` is supported in some places as a legacy syntax; it should always be `brooklyn.libraries` for new apps // TODO in 0.8.0 require brooklyn.libraries, don't allow "libraries" on its own - List<?> librariesNew = MutableList.copyOf(getFirstAs(itemMetadataWithoutItemDef, List.class, "brooklyn.libraries", "libraries").orNull()); - Collection<CatalogBundle> libraryBundlesNew = CatalogItemDtoAbstract.parseLibraries(librariesNew); - - List<?> librariesCombined = MutableList.copyOf(librariesNew) - .appendAll(getFirstAs(parentMetadata, List.class, "brooklyn.libraries", "libraries").orNull()); - if (!librariesCombined.isEmpty()) - catalogMetadata.put("brooklyn.libraries", librariesCombined); - Collection<CatalogBundle> libraryBundles = CatalogItemDtoAbstract.parseLibraries(librariesCombined); + List<?> librariesAddedHereNames = MutableList.copyOf(getFirstAs(itemMetadataWithoutItemDef, List.class, "brooklyn.libraries", "libraries").orNull()); + Collection<CatalogBundle> librariesAddedHereBundles = CatalogItemDtoAbstract.parseLibraries(librariesAddedHereNames); + + MutableSet<Object> librariesCombinedNames = MutableSet.of(); + if (!isNoBundleOrSimpleWrappingBundle(containingBundle)) { + // ensure containing bundle is declared, first, for search purposes + librariesCombinedNames.add(containingBundle.getVersionedName().toOsgiString()); + } + librariesCombinedNames.putAll(librariesAddedHereNames); + librariesCombinedNames.putAll(getFirstAs(parentMetadata, Collection.class, "brooklyn.libraries", "libraries").orNull()); + if (!librariesCombinedNames.isEmpty()) { + catalogMetadata.put("brooklyn.libraries", librariesCombinedNames); + } + Collection<CatalogBundle> libraryBundles = CatalogItemDtoAbstract.parseLibraries(librariesCombinedNames); - // TODO as this may take a while if downloading, the REST call should be async - // (this load is required for the scan below and I think also for yaml resolution) - CatalogUtils.installLibraries(mgmt, libraryBundlesNew); + // TODO this may take a while if downloading; ideally the REST call would be async + // but this load is required for resolving YAML in this BOM (and if java-scanning); + // need to think through how we expect dependencies to be installed + CatalogUtils.installLibraries(mgmt, librariesAddedHereBundles); Boolean scanJavaAnnotations = getFirstAs(itemMetadataWithoutItemDef, Boolean.class, "scanJavaAnnotations", "scan_java_annotations").orNull(); if (scanJavaAnnotations==null || !scanJavaAnnotations) { @@ -561,10 +567,10 @@ public class BasicBrooklynCatalog implements BrooklynCatalog { } else { if (isNoBundleOrSimpleWrappingBundle(containingBundle)) { // BOMs wrapped in JARs, or without JARs, have special treatment - if (isLibrariesMoreThanJustContainingBundle(libraryBundlesNew, containingBundle)) { + if (isLibrariesMoreThanJustContainingBundle(librariesAddedHereBundles, containingBundle)) { // legacy mode, since 0.12.0, scan libraries referenced in a legacy non-bundle BOM - log.warn("Deprecated use of scanJavaAnnotations to scan other libraries ("+libraryBundlesNew+"); libraries should declare they scan themselves"); - result.addAll(scanAnnotationsLegacyInListOfLibraries(mgmt, libraryBundlesNew, catalogMetadata, containingBundle)); + log.warn("Deprecated use of scanJavaAnnotations to scan other libraries ("+librariesAddedHereBundles+"); libraries should declare they scan themselves"); + result.addAll(scanAnnotationsLegacyInListOfLibraries(mgmt, librariesAddedHereBundles, catalogMetadata, containingBundle)); } else if (!isLibrariesMoreThanJustContainingBundle(libraryBundles, containingBundle)) { // for default catalog, no libraries declared, we want to scan local classpath // bundle should be named "brooklyn-default-catalog" http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/dc43f03f/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleLoader.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleLoader.java b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleLoader.java index cce1258..3af87b9 100644 --- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleLoader.java +++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleLoader.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.List; -import java.util.Map; import org.apache.brooklyn.api.catalog.CatalogItem; import org.apache.brooklyn.api.mgmt.ManagementContext; @@ -36,25 +35,18 @@ import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.osgi.VersionedName; import org.apache.brooklyn.util.stream.Streams; import org.apache.brooklyn.util.text.Strings; -import org.apache.brooklyn.util.yaml.Yamls; import org.osgi.framework.Bundle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; import com.google.common.annotations.Beta; import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; @Beta public class CatalogBundleLoader { private static final Logger LOG = LoggerFactory.getLogger(CatalogBundleLoader.class); private static final String CATALOG_BOM_URL = "catalog.bom"; - private static final String BROOKLYN_CATALOG = "brooklyn.catalog"; - private static final String BROOKLYN_LIBRARIES = "brooklyn.libraries"; private Predicate<Bundle> applicationsPermitted; private ManagementContext managementContext; @@ -85,8 +77,7 @@ public class CatalogBundleLoader { if (null != bom) { LOG.debug("Found catalog BOM in {} {} {}", CatalogUtils.bundleIds(bundle)); String bomText = readBom(bom); - String bomWithLibraryPath = addLibraryDetails(bundle, bomText); - catalogItems = this.managementContext.getCatalog().addItems(bomWithLibraryPath, mb, force); + catalogItems = this.managementContext.getCatalog().addItems(bomText, mb, force); for (CatalogItem<?, ?> item : catalogItems) { LOG.debug("Added to catalog: {}, {}", item.getSymbolicName(), item.getVersion()); } @@ -95,7 +86,7 @@ public class CatalogBundleLoader { } if (!applicationsPermitted.apply(bundle)) { - catalogItems = removeAnyApplications(catalogItems); + catalogItems = removeApplications(catalogItems); } return catalogItems; @@ -125,60 +116,7 @@ public class CatalogBundleLoader { } } - // TODO remove; now that the bundle is passed through we can add it in the catalog - private String addLibraryDetails(Bundle bundle, String bomText) { - @SuppressWarnings("unchecked") - final Map<String, Object> bom = (Map<String, Object>) Iterables.getOnlyElement(Yamls.parseAll(bomText)); - final Object catalog = bom.get(CatalogBundleLoader.BROOKLYN_CATALOG); - if (null != catalog) { - if (catalog instanceof Map<?, ?>) { - @SuppressWarnings("unchecked") - Map<String, Object> catalogMap = (Map<String, Object>) catalog; - addLibraryDetails(bundle, catalogMap); - } else { - LOG.warn("Unexpected syntax for {} (expected Map, but got {}), ignoring", - CatalogBundleLoader.BROOKLYN_CATALOG, catalog.getClass().getName()); - } - } - final String updatedBom = backToYaml(bom); - LOG.trace("Updated catalog bom:\n{}", updatedBom); - return updatedBom; - } - - private void addLibraryDetails(Bundle bundle, Map<String, Object> catalog) { - if (!catalog.containsKey(CatalogBundleLoader.BROOKLYN_LIBRARIES)) { - if (catalog.containsKey("libraries")) { - // legacy name - catalog.put(CatalogBundleLoader.BROOKLYN_LIBRARIES, catalog.remove("libraries")); - } else { - catalog.put(CatalogBundleLoader.BROOKLYN_LIBRARIES, MutableList.of()); - } - } - final Object librarySpec = catalog.get(CatalogBundleLoader.BROOKLYN_LIBRARIES); - if (!(librarySpec instanceof List)) { - throw new RuntimeException("expected " + CatalogBundleLoader.BROOKLYN_LIBRARIES + " to be a list, but got " - + (librarySpec == null ? "null" : librarySpec.getClass().getName())); - } - @SuppressWarnings("unchecked") - List<Map<String, String>> libraries = (List<Map<String, String>>) librarySpec; - if (bundle.getSymbolicName() == null || bundle.getVersion() == null) { - throw new IllegalStateException("Cannot scan " + bundle + " for catalog files: name or version is null"); - } - libraries.add(ImmutableMap.of( - "name", bundle.getSymbolicName(), - "version", bundle.getVersion().toString())); - LOG.debug("library spec is {}", librarySpec); - } - - private String backToYaml(Map<String, Object> bom) { - final DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - options.setPrettyFlow(true); - return new Yaml(options).dump(bom); - } - - private Iterable<? extends CatalogItem<?, ?>> removeAnyApplications( - Iterable<? extends CatalogItem<?, ?>> catalogItems) { + private Iterable<? extends CatalogItem<?, ?>> removeApplications(Iterable<? extends CatalogItem<?, ?>> catalogItems) { List<CatalogItem<?, ?>> result = MutableList.of();
