brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From geom...@apache.org
Subject [09/12] brooklyn-server git commit: Tidy ugly dependency on `CatalogBomScanner` in the tracker and loader.
Date Mon, 27 Mar 2017 10:35:28 GMT
Tidy ugly dependency on `CatalogBomScanner` in the tracker and loader.

Replace it with a simple predicate that wraps the whitelist checks and
inject the predicate into the loader. That way the CatalogBomScanner
can inject the correct whitelist configuration in the blueprint.xml,
while in the REST API here we just live with an "always true" for the
moment.

Create the loader the scanner and inject it into the tracker,
which simplifies the latter a bit.


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/ffded0c8
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/ffded0c8
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/ffded0c8

Branch: refs/heads/master
Commit: ffded0c8a4b451b5162fcd8a9f046ad94a4070da
Parents: f86eb87
Author: Geoff Macartney <geoff.macartney@cloudsoftcorp.com>
Authored: Fri Mar 24 17:37:40 2017 +0000
Committer: Geoff Macartney <geoff.macartney@cloudsoftcorp.com>
Committed: Fri Mar 24 17:37:40 2017 +0000

----------------------------------------------------------------------
 .../catalog/internal/CatalogBomScanner.java     | 44 ++++++++++++++++----
 .../catalog/internal/CatalogBundleLoader.java   | 29 ++++---------
 .../catalog/internal/CatalogBundleTracker.java  | 36 +++-------------
 .../core/catalog/internal/CatalogUtils.java     |  7 ++++
 .../rest/resources/CatalogResource.java         |  5 ++-
 5 files changed, 60 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ffded0c8/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 78e7d54..1f528c8 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
@@ -20,15 +20,19 @@ package org.apache.brooklyn.core.catalog.internal;
 
 import java.util.List;
 
+import javax.annotation.Nullable;
+
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.BrooklynFeatureEnablement;
 import org.apache.brooklyn.util.text.Strings;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 
 /** Scans bundles being added, filtered by a whitelist and blacklist, and adds catalog.bom
files to the catalog.
@@ -45,16 +49,22 @@ public class CatalogBomScanner {
 
     private CatalogBundleTracker catalogBundleTracker;
 
-    public void bind(ServiceReference<ManagementContext> managementContext) throws
Exception {
+    public void bind(ServiceReference<ManagementContext> mgmtContextReference) throws
Exception {
         if (isEnabled()) {
             LOG.debug("Binding management context with whiteList [{}] and blacklist [{}]",
                 Strings.join(getWhiteList(), "; "),
                 Strings.join(getBlackList(), "; "));
-            catalogBundleTracker = new CatalogBundleTracker(this, managementContext);
+
+            final BundleContext bundleContext = mgmtContextReference.getBundle().getBundleContext();
+            ManagementContext mgmt = bundleContext.getService(mgmtContextReference);
+            CatalogBundleLoader bundleLoader = new CatalogBundleLoader(new SymbolicNameAccessControl(),
mgmt);
+
+            catalogBundleTracker = new CatalogBundleTracker(bundleContext, bundleLoader);
+            catalogBundleTracker.open();
         }
     }
 
-    public void unbind(ServiceReference<ManagementContext> managementContext) throws
Exception {
+    public void unbind(ServiceReference<ManagementContext> mgmtContextReference) throws
Exception {
         if (isEnabled()) {
             LOG.debug("Unbinding management context");
             if (null != catalogBundleTracker) {
@@ -62,6 +72,7 @@ public class CatalogBomScanner {
                 catalogBundleTracker = null;
                 temp.close();
             }
+            mgmtContextReference.getBundle().getBundleContext().ungetService(mgmtContextReference);
         }
     }
 
@@ -69,12 +80,6 @@ public class CatalogBomScanner {
         return BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_LOAD_BUNDLE_CATALOG_BOM);
     }
 
-    public String[] bundleIds(Bundle bundle) {
-        return new String[] {
-            String.valueOf(bundle.getBundleId()), bundle.getSymbolicName(), bundle.getVersion().toString()
-        };
-    }
-
     public List<String> getWhiteList() {
         return whiteList;
     }
@@ -101,4 +106,25 @@ public class CatalogBomScanner {
         this.blackList = Strings.parseCsv(blackListText);
     }
 
+    public class SymbolicNameAccessControl implements Predicate<Bundle> {
+        @Override
+        public boolean apply(@Nullable Bundle input) {
+            return passesWhiteAndBlacklists(input);
+        }
+    }
+
+    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;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ffded0c8/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 20fe5e2..b1f8fd3 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
@@ -41,6 +41,7 @@ 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;
 
@@ -52,11 +53,11 @@ public class CatalogBundleLoader {
     private static final String BROOKLYN_CATALOG = "brooklyn.catalog";
     private static final String BROOKLYN_LIBRARIES = "brooklyn.libraries";
 
-    private CatalogBomScanner catalogBomScanner;
+    private Predicate<Bundle> applicationsPermitted;
     private ManagementContext managementContext;
 
-    public CatalogBundleLoader(CatalogBomScanner catalogBomScanner, ManagementContext managementContext)
{
-        this.catalogBomScanner = catalogBomScanner;
+    public CatalogBundleLoader(Predicate<Bundle> applicationsPermitted, ManagementContext
managementContext) {
+        this.applicationsPermitted = applicationsPermitted;
         this.managementContext = managementContext;
     }
 
@@ -73,7 +74,7 @@ public class CatalogBundleLoader {
 
         final URL bom = bundle.getResource(CatalogBundleLoader.CATALOG_BOM_URL);
         if (null != bom) {
-            LOG.debug("Found catalog BOM in {} {} {}", catalogBomScanner.bundleIds(bundle));
+            LOG.debug("Found catalog BOM in {} {} {}", CatalogUtils.bundleIds(bundle));
             String bomText = readBom(bom);
             String bomWithLibraryPath = addLibraryDetails(bundle, bomText);
             catalogItems = this.managementContext.getCatalog().addItems(bomWithLibraryPath);
@@ -81,10 +82,10 @@ public class CatalogBundleLoader {
                 LOG.debug("Added to catalog: {}, {}", item.getSymbolicName(), item.getVersion());
             }
         } else {
-            LOG.debug("No BOM found in {} {} {}", catalogBomScanner.bundleIds(bundle));
+            LOG.debug("No BOM found in {} {} {}", CatalogUtils.bundleIds(bundle));
         }
 
-        if (!passesWhiteAndBlacklists(bundle)) {
+        if (!applicationsPermitted.apply(bundle)) {
             catalogItems = removeAnyApplications(catalogItems);
         }
 
@@ -125,7 +126,8 @@ public class CatalogBundleLoader {
                 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());
+                LOG.warn("Unexpected syntax for {} (expected Map, but got {}), ignoring",
+                    CatalogBundleLoader.BROOKLYN_CATALOG, catalog.getClass().getName());
             }
         }
         final String updatedBom = backToYaml(bom);
@@ -175,17 +177,4 @@ public class CatalogBundleLoader {
         return result;
     }
 
-    private boolean passesWhiteAndBlacklists(Bundle bundle) {
-        return on(bundle, catalogBomScanner.getWhiteList()) && !on(bundle, catalogBomScanner.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;
-    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ffded0c8/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleTracker.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleTracker.java
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleTracker.java
index 4cb2470..074d7f7 100644
--- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleTracker.java
+++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogBundleTracker.java
@@ -20,10 +20,9 @@
 package org.apache.brooklyn.core.catalog.internal;
 
 import org.apache.brooklyn.api.catalog.CatalogItem;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.BundleTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,36 +34,11 @@ public class CatalogBundleTracker extends BundleTracker<Iterable<?
extends Catal
 
     private static final Logger LOG = LoggerFactory.getLogger(CatalogBundleTracker.class);
 
-    private ServiceReference<ManagementContext> mgmtContextReference;
-    private ManagementContext managementContext;
-
-    private CatalogBomScanner catalogBomScanner;
     private CatalogBundleLoader catalogBundleLoader;
 
-    public CatalogBundleTracker(CatalogBomScanner catalogBomScanner, ServiceReference<ManagementContext>
serviceReference) {
-        super(serviceReference.getBundle().getBundleContext(), Bundle.ACTIVE, null);
-        this.mgmtContextReference = serviceReference;
-        this.catalogBomScanner = catalogBomScanner;
-        open();
-    }
-
-    @Override
-    public void open() {
-        managementContext = mgmtContextReference.getBundle().getBundleContext().getService(mgmtContextReference);
-        catalogBundleLoader = new CatalogBundleLoader(catalogBomScanner, managementContext);
-        super.open();
-    }
-
-    @Override
-    public void close() {
-        super.close();
-        managementContext = null;
-        mgmtContextReference.getBundle().getBundleContext().ungetService(mgmtContextReference);
-        catalogBundleLoader = null;
-    }
-
-    public ManagementContext getManagementContext() {
-        return managementContext;
+    public CatalogBundleTracker(BundleContext bundleContext, CatalogBundleLoader catalogBundleLoader)
{
+        super(bundleContext, Bundle.ACTIVE, null);
+        this.catalogBundleLoader = catalogBundleLoader;
     }
 
     /**
@@ -94,7 +68,7 @@ public class CatalogBundleTracker extends BundleTracker<Iterable<?
extends Catal
         if (!items.iterator().hasNext()) {
             return;
         }
-        LOG.debug("Unloading catalog BOM entries from {} {} {}", catalogBomScanner.bundleIds(bundle));
+        LOG.debug("Unloading catalog BOM entries from {} {} {}", CatalogUtils.bundleIds(bundle));
         for (CatalogItem<?, ?> item : items) {
             LOG.debug("Unloading {} {} from catalog", item.getSymbolicName(), item.getVersion());
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ffded0c8/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
index ff96ae5..8306b71 100644
--- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
+++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
@@ -48,6 +48,7 @@ import org.apache.brooklyn.core.typereg.RegisteredTypes;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Time;
+import org.osgi.framework.Bundle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -323,4 +324,10 @@ public class CatalogUtils {
         mgmt.getCatalog().persist(item);
     }
 
+
+    public static String[] bundleIds(Bundle bundle) {
+        return new String[] {
+            String.valueOf(bundle.getBundleId()), bundle.getSymbolicName(), bundle.getVersion().toString()
+        };
+    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ffded0c8/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
----------------------------------------------------------------------
diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
index a5cdfe4..e00cad8 100644
--- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
+++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
@@ -247,7 +247,10 @@ public class CatalogResource extends AbstractBrooklynRestResource implements
Cat
             if (!BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_LOAD_BUNDLE_CATALOG_BOM))
{
                 // if the above feature is not enabled, let's do it manually (as a contract
of this method)
                 try {
-                    new CatalogBundleLoader(new CatalogBomScanner(), mgmt()).scanForCatalog(bundle);
+                    // TODO improve on this - it ignores the configuration of whitelists,
see CatalogBomScanner.
+                    final Predicate<Bundle> applicationsPermitted = Predicates.<Bundle>alwaysTrue();
+
+                    new CatalogBundleLoader(applicationsPermitted, mgmt()).scanForCatalog(bundle);
                 } catch (RuntimeException ex) {
                     try {
                         bundle.uninstall();


Mime
View raw message