You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ge...@apache.org on 2017/03/27 10:35:23 UTC

[04/12] brooklyn-server git commit: Separate the CatalogPopulator from the CatalogBundleLoader

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 <th...@cloudsoftcorp.com>
Authored: Thu Mar 23 16:05:29 2017 +0000
Committer: Thomas Bouron <th...@cloudsoftcorp.com>
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<ManagementContext> mgmtContextReference;
         private BundleContext bundleContext;
         private ManagementContext managementContext;
+        private CatalogBundleLoader catalogBundleLoader;
 
         public CatalogPopulator(ServiceReference<ManagementContext> 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<? extends CatalogItem<?, ?>> 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<? extends CatalogItem<?, ?>> 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<? extends CatalogItem<?, ?>> 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<? extends CatalogItem<?, ?>> scanForCatalog(Bundle bundle) {
 
             Iterable<? extends CatalogItem<?, ?>> 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<? extends CatalogItem<?, ?>> removeAnyApplications(
-            Iterable<? extends CatalogItem<?, ?>> catalogItems) {
-
-            List<CatalogItem<?, ?>> 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<String> 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<String, Object> 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<String, Object> 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<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) {
+
+            List<CatalogItem<?, ?>> 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<String> 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);
+            }
+        }
+    }
 
 }