Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 721B2200C53 for ; Mon, 27 Mar 2017 12:35:23 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 70C9F160B5D; Mon, 27 Mar 2017 10:35:23 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 2605C160B9E for ; Mon, 27 Mar 2017 12:35:21 +0200 (CEST) Received: (qmail 59423 invoked by uid 500); 27 Mar 2017 10:35:21 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 59231 invoked by uid 99); 27 Mar 2017 10:35:21 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Mar 2017 10:35:21 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id CB181DFFD7; Mon, 27 Mar 2017 10:35:20 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: geomacy@apache.org To: commits@brooklyn.apache.org Date: Mon, 27 Mar 2017 10:35:23 -0000 Message-Id: <57f1237803b343d5a90c5cf9b34a6fbc@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [04/12] brooklyn-server git commit: Separate the CatalogPopulator from the CatalogBundleLoader archived-at: Mon, 27 Mar 2017 10:35:23 -0000 Separate the CatalogPopulator from the CatalogBundleLoader Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/84c1b69f Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/84c1b69f Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/84c1b69f Branch: refs/heads/master Commit: 84c1b69f83b79ce43a5b7d19c68971775c71d000 Parents: 2ed7543 Author: Thomas Bouron Authored: Thu Mar 23 16:05:29 2017 +0000 Committer: Thomas Bouron Committed: Thu Mar 23 17:21:02 2017 +0000 ---------------------------------------------------------------------- .../catalog/internal/CatalogBomScanner.java | 144 +++++++++++-------- 1 file changed, 87 insertions(+), 57 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/84c1b69f/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBomScanner.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBomScanner.java b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBomScanner.java index 8bbb34d..1348886 100644 --- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBomScanner.java +++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBomScanner.java @@ -44,6 +44,7 @@ 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.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -51,6 +52,7 @@ import com.google.common.collect.Iterables; /** Scans bundles being added, filtered by a whitelist and blacklist, and adds catalog.bom files to the catalog. * See karaf blueprint.xml for configuration, and tests in dist project. */ +@Beta public class CatalogBomScanner { private final String ACCEPT_ALL_BY_DEFAULT = ".*"; @@ -126,6 +128,7 @@ public class CatalogBomScanner { private ServiceReference mgmtContextReference; private BundleContext bundleContext; private ManagementContext managementContext; + private CatalogBundleLoader catalogBundleLoader; public CatalogPopulator(ServiceReference serviceReference) { super(serviceReference.getBundle().getBundleContext(), Bundle.ACTIVE, null); @@ -145,6 +148,7 @@ public class CatalogBomScanner { if (mgmtContextReference != null) { bundleContext = getBundleContext(); managementContext = getManagementContext(); + catalogBundleLoader = new CatalogBundleLoader(managementContext); } super.open(); } @@ -156,6 +160,7 @@ public class CatalogBomScanner { managementContext = null; getBundleContext().ungetService(mgmtContextReference); bundleContext = null; + catalogBundleLoader = null; } } @@ -179,13 +184,23 @@ public class CatalogBomScanner { * * @return The items added to the catalog; these will be tracked by the {@link BundleTracker} mechanism * and supplied to the {@link #removedBundle(Bundle, BundleEvent, Iterable)} method. + * + * @throws RuntimeException if the catalog items failed to be added to the catalog */ @Override public Iterable> addingBundle(Bundle bundle, BundleEvent bundleEvent) { - return scanForCatalog(bundle); + return catalogBundleLoader.scanForCatalog(bundle); } - + /** + * Remove the given entries from the catalog, related to the given bundle. + * + * @param bundle The bundle being removed to the bundle context. + * @param bundleEvent The event of the removal. + * @param items The items being removed + * + * @throws RuntimeException if the catalog items failed to be added to the catalog + */ @Override public void removedBundle(Bundle bundle, BundleEvent bundleEvent, Iterable> items) { if (!items.iterator().hasNext()) { @@ -195,22 +210,29 @@ public class CatalogBomScanner { for (CatalogItem item : items) { LOG.debug("Unloading {} {} from catalog", item.getSymbolicName(), item.getVersion()); - removeFromCatalog(item); + catalogBundleLoader.removeFromCatalog(item); } } + } - private void removeFromCatalog(CatalogItem item) { - try { - getManagementContext().getCatalog().deleteCatalogItem(item.getSymbolicName(), item.getVersion()); - } catch (Exception e) { - Exceptions.propagateIfFatal(e); - LOG.warn(Strings.join(new String[] { - "Failed to remove", item.getSymbolicName(), item.getVersion(), "from catalog" - }, " "), e); - } + @Beta + public class CatalogBundleLoader { + + private ManagementContext managementContext; + + public CatalogBundleLoader(ManagementContext managementContext) { + this.managementContext = managementContext; } - private Iterable> scanForCatalog(Bundle bundle) { + /** + * Scan the given bundle for a catalog.bom and adds it to the catalog. + * + * @param bundle The bundle to add + * @return A list of items added to the catalog + * + * @throws RuntimeException if the catalog items failed to be added to the catalog + */ + public Iterable> scanForCatalog(Bundle bundle) { Iterable> catalogItems = MutableList.of(); @@ -219,11 +241,10 @@ public class CatalogBomScanner { LOG.debug("Found catalog BOM in {} {} {}", bundleIds(bundle)); String bomText = readBom(bom); String bomWithLibraryPath = addLibraryDetails(bundle, bomText); - catalogItems = getManagementContext().getCatalog().addItems(bomWithLibraryPath); + catalogItems = this.managementContext.getCatalog().addItems(bomWithLibraryPath); for (CatalogItem item : catalogItems) { LOG.debug("Added to catalog: {}, {}", item.getSymbolicName(), item.getVersion()); } - } else { LOG.debug("No BOM found in {} {} {}", bundleIds(bundle)); } @@ -235,33 +256,12 @@ public class CatalogBomScanner { return catalogItems; } - private Iterable> removeAnyApplications( - Iterable> catalogItems) { - - List> result = MutableList.of(); - - for (CatalogItem item: catalogItems) { - if (TEMPLATE.equals(item.getCatalogItemType())) { - removeFromCatalog(item); - } else { - result.add(item); - } - } - return result; - } - - private boolean passesWhiteAndBlacklists(Bundle bundle) { - return on(bundle, getWhiteList()) && !on(bundle, getBlackList()); - } - - private boolean on(Bundle bundle, List list) { - for (String candidate : list) { - final String symbolicName = bundle.getSymbolicName(); - if (symbolicName.matches(candidate.trim())) { - return true; - } + private String readBom(URL bom) { + try (final InputStream ins = bom.openStream()) { + return Streams.readFullyString(ins); + } catch (IOException e) { + throw Exceptions.propagate("Error loading Catalog BOM from " + bom, e); } - return false; } private String addLibraryDetails(Bundle bundle, String bomText) { @@ -282,20 +282,13 @@ public class CatalogBomScanner { return updatedBom; } - private String backToYaml(Map bom) { - final DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - options.setPrettyFlow(true); - return new Yaml(options).dump(bom); - } - private void addLibraryDetails(Bundle bundle, Map catalog) { if (!catalog.containsKey(BROOKLYN_LIBRARIES)) { catalog.put(BROOKLYN_LIBRARIES, MutableList.of()); } final Object librarySpec = catalog.get(BROOKLYN_LIBRARIES); if (!(librarySpec instanceof List)) { - throw new RuntimeException("expected " + BROOKLYN_LIBRARIES + " to be a list, but got " + throw new RuntimeException("expected " + BROOKLYN_LIBRARIES + " to be a list, but got " + (librarySpec == null ? "null" : librarySpec.getClass().getName())); } @SuppressWarnings("unchecked") @@ -304,20 +297,57 @@ public class CatalogBomScanner { 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())); + "name", bundle.getSymbolicName(), + "version", bundle.getVersion().toString())); LOG.debug("library spec is {}", librarySpec); } - private String readBom(URL bom) { - try (final InputStream ins = bom.openStream()) { - return Streams.readFullyString(ins); - } catch (IOException e) { - throw Exceptions.propagate("Error loading Catalog BOM from " + bom, e); + private String backToYaml(Map bom) { + final DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setPrettyFlow(true); + return new Yaml(options).dump(bom); + } + + private Iterable> removeAnyApplications( + Iterable> catalogItems) { + + List> result = MutableList.of(); + + for (CatalogItem item: catalogItems) { + if (TEMPLATE.equals(item.getCatalogItemType())) { + removeFromCatalog(item); + } else { + result.add(item); + } } + return result; } - } + private boolean passesWhiteAndBlacklists(Bundle bundle) { + return on(bundle, getWhiteList()) && !on(bundle, getBlackList()); + } + + private boolean on(Bundle bundle, List list) { + for (String candidate : list) { + final String symbolicName = bundle.getSymbolicName(); + if (symbolicName.matches(candidate.trim())) { + return true; + } + } + return false; + } + private void removeFromCatalog(CatalogItem item) { + try { + this.managementContext.getCatalog().deleteCatalogItem(item.getSymbolicName(), item.getVersion()); + } catch (Exception e) { + Exceptions.propagateIfFatal(e); + LOG.warn(Strings.join(new String[] { + "Failed to remove", item.getSymbolicName(), item.getVersion(), "from catalog" + }, " "), e); + } + } + } }