This is an automated email from the ASF dual-hosted git repository.

aledsage pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 64c869c8ee7f5a25f4f0b7cd02f9a2c1e53db5bb
Author: Paul Campbell <pcampb...@kemitix.net>
AuthorDate: Wed Oct 24 10:41:39 2018 +0100

    Extract CatalogUpgradeScanner
---
 .../catalog/internal/CatalogInitialization.java    | 45 +----------
 .../catalog/internal/CatalogUpgradeScanner.java    | 91 ++++++++++++++++++++++
 2 files changed, 95 insertions(+), 41 deletions(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogInitialization.java
 
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogInitialization.java
index 4588119..9dcaa4b 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogInitialization.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogInitialization.java
@@ -112,6 +112,7 @@ public class CatalogInitialization implements 
ManagementContextInjectable {
     private boolean hasRunFinalInitialization = false;
 
     private ManagementContextInternal managementContext;
+    private CatalogUpgradeScanner catalogUpgradeScanner;
     private boolean isStartingUp = false;
     private boolean failOnStartupErrors = false;
     
@@ -134,6 +135,7 @@ public class CatalogInitialization implements 
ManagementContextInjectable {
         if (this.managementContext!=null && 
managementContext!=this.managementContext)
             throw new IllegalStateException("Cannot switch management context, 
from "+this.managementContext+" to "+managementContext);
         this.managementContext = (ManagementContextInternal) managementContext;
+        catalogUpgradeScanner = new 
CatalogUpgradeScanner(this.managementContext);
     }
     
     /** Called by the framework to set true while starting up, and false 
afterwards,
@@ -251,8 +253,8 @@ public class CatalogInitialization implements 
ManagementContextInjectable {
             }
             
             populateInitialCatalogImpl(true);
-            
-            CatalogUpgrades catalogUpgrades = 
gatherCatalogUpgradesInstructions(rebindLogger);
+
+            final CatalogUpgrades catalogUpgrades = 
catalogUpgradeScanner.scan(rebindLogger);
             CatalogUpgrades.storeInManagementContext(catalogUpgrades, 
managementContext);
             PersistedCatalogState filteredPersistedState = 
filterBundlesAndCatalogInPersistedState(persistedState, rebindLogger);
             addPersistedCatalogImpl(filteredPersistedState, exceptionHandler, 
rebindLogger);
@@ -606,45 +608,6 @@ public class CatalogInitialization implements 
ManagementContextInjectable {
         return new PersistedCatalogState(bundles, legacyCatalogItems);
     }
 
-    private CatalogUpgrades gatherCatalogUpgradesInstructions(RebindLogger 
rebindLogger) {
-        Maybe<OsgiManager> osgiManager = 
((ManagementContextInternal)managementContext).getOsgiManager();
-        if (osgiManager.isAbsent()) {
-            // Can't find any bundles to tell if there are upgrades. Could be 
running tests; do no filtering.
-            return CatalogUpgrades.EMPTY;
-        }
-        final CatalogUpgrades.Builder catalogUpgradesBuilder = 
CatalogUpgrades.builder();
-        scanManagedBundles(osgiManager.get(), catalogUpgradesBuilder, 
rebindLogger);
-        scanAllBundles(osgiManager.get(), catalogUpgradesBuilder);
-        return catalogUpgradesBuilder.build();
-    }
-
-    private void scanManagedBundles(OsgiManager osgiManager, 
CatalogUpgrades.Builder catalogUpgradesBuilder, RebindLogger rebindLogger) {
-        Collection<ManagedBundle> managedBundles = 
osgiManager.getManagedBundles().values();
-        for (ManagedBundle managedBundle : managedBundles) {
-            Maybe<Bundle> bundle = osgiManager.findBundle(managedBundle);
-            if (bundle.isPresent()) {
-                CatalogUpgrades catalogUpgrades = 
BundleUpgradeParser.parseBundleManifestForCatalogUpgrades(
-                        bundle.get(),
-                        new RegisteredTypesSupplier(managementContext, 
RegisteredTypePredicates.containingBundle(managedBundle)));
-                catalogUpgradesBuilder.addAll(catalogUpgrades);
-            } else {
-                rebindLogger.info("Managed bundle "+managedBundle.getId()+" 
not found by OSGi Manager; "
-                        + "ignoring when calculating persisted state catalog 
upgrades");
-            }
-        }
-    }
-
-    private void scanAllBundles(OsgiManager osgiManager, 
CatalogUpgrades.Builder catalogUpgradesBuilder) {
-        for (Bundle bundle : 
osgiManager.getFramework().getBundleContext().getBundles()) {
-            final RegisteredTypesSupplier typeSupplier =
-                    new RegisteredTypesSupplier(managementContext,
-                            
RegisteredTypePredicates.containingBundle(bundle.getSymbolicName()));
-            final CatalogUpgrades catalogUpgrades =
-                    
BundleUpgradeParser.parseBundleManifestForCatalogUpgrades(bundle, typeSupplier);
-            catalogUpgradesBuilder.addAll(catalogUpgrades);
-        }
-    }
-
     public interface RebindLogger {
         void debug(String message, Object... args);
         void info(String message, Object... args);
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUpgradeScanner.java
 
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUpgradeScanner.java
new file mode 100644
index 0000000..afb181f
--- /dev/null
+++ 
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUpgradeScanner.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.core.catalog.internal;
+
+import org.apache.brooklyn.api.typereg.ManagedBundle;
+import org.apache.brooklyn.core.mgmt.ha.OsgiManager;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
+import org.apache.brooklyn.core.typereg.BundleUpgradeParser;
+import org.apache.brooklyn.core.typereg.BundleUpgradeParser.CatalogUpgrades;
+import org.apache.brooklyn.core.typereg.RegisteredTypePredicates;
+import org.apache.brooklyn.util.guava.Maybe;
+import org.osgi.framework.Bundle;
+
+import java.util.Collection;
+
+/**
+ * Scans managed bundles and other jar bundles to find upgrades for installed 
bundles.
+ */
+class CatalogUpgradeScanner {
+
+    private final ManagementContextInternal managementContext;
+
+    CatalogUpgradeScanner(
+            final ManagementContextInternal managementContext
+    ) {
+        this.managementContext = managementContext;
+    }
+
+    public CatalogUpgrades scan(final CatalogInitialization.RebindLogger 
rebindLogger) {
+        Maybe<OsgiManager> osgiManager = managementContext.getOsgiManager();
+        if (osgiManager.isAbsent()) {
+            // Can't find any bundles to tell if there are upgrades. Could be 
running tests; do no filtering.
+            return CatalogUpgrades.EMPTY;
+        }
+        final CatalogUpgrades.Builder catalogUpgradesBuilder = 
CatalogUpgrades.builder();
+        scanManagedBundles(osgiManager.get(), catalogUpgradesBuilder, 
rebindLogger);
+        scanAllBundles(osgiManager.get(), catalogUpgradesBuilder);
+        return catalogUpgradesBuilder.build();
+    }
+
+    private void scanManagedBundles(
+            final OsgiManager osgiManager,
+            final CatalogUpgrades.Builder catalogUpgradesBuilder,
+            final CatalogInitialization.RebindLogger rebindLogger
+    ) {
+        Collection<ManagedBundle> managedBundles = 
osgiManager.getManagedBundles().values();
+        for (ManagedBundle managedBundle : managedBundles) {
+            Maybe<Bundle> bundle = osgiManager.findBundle(managedBundle);
+            if (bundle.isPresent()) {
+                CatalogUpgrades catalogUpgrades = 
BundleUpgradeParser.parseBundleManifestForCatalogUpgrades(
+                        bundle.get(),
+                        new RegisteredTypesSupplier(managementContext, 
RegisteredTypePredicates.containingBundle(managedBundle)));
+                catalogUpgradesBuilder.addAll(catalogUpgrades);
+            } else {
+                rebindLogger.info("Managed bundle "+managedBundle.getId()+" 
not found by OSGi Manager; "
+                        + "ignoring when calculating persisted state catalog 
upgrades");
+            }
+        }
+    }
+
+    private void scanAllBundles(
+            final OsgiManager osgiManager,
+            final CatalogUpgrades.Builder catalogUpgradesBuilder
+    ) {
+        for (Bundle bundle : 
osgiManager.getFramework().getBundleContext().getBundles()) {
+            final RegisteredTypesSupplier typeSupplier =
+                    new RegisteredTypesSupplier(managementContext,
+                            
RegisteredTypePredicates.containingBundle(bundle.getSymbolicName()));
+            final CatalogUpgrades catalogUpgrades =
+                    
BundleUpgradeParser.parseBundleManifestForCatalogUpgrades(bundle, typeSupplier);
+            catalogUpgradesBuilder.addAll(catalogUpgrades);
+        }
+    }
+
+}

Reply via email to