changeset 557:4e9f1b8d4463

Fix loaded bundle cache to prevent multiple install/start of same bundle. review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002858.html reviewed-by: sgehwolf PR1111
author Jon VanAlten <jon.vanalten@redhat.com>
date Tue, 21 Aug 2012 12:21:15 -0400
parents 517f3d4796af
children 7f564b061056
files bundles/src/main/java/com/redhat/thermostat/bundles/OSGiRegistryService.java bundles/src/main/java/com/redhat/thermostat/bundles/impl/OSGiRegistry.java
diffstat 2 files changed, 21 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/bundles/src/main/java/com/redhat/thermostat/bundles/OSGiRegistryService.java	Mon Aug 20 15:29:14 2012 +0200
+++ b/bundles/src/main/java/com/redhat/thermostat/bundles/OSGiRegistryService.java	Tue Aug 21 12:21:15 2012 -0400
@@ -37,10 +37,8 @@
 package com.redhat.thermostat.bundles;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.List;
 
-import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.launch.Framework;
 
@@ -49,8 +47,6 @@
 
 public abstract class OSGiRegistryService {
 
-    protected static List<String> preLoadedBundles = new ArrayList<>();
-
     public abstract void setPrintOSGiInfo(boolean printOSGiInfo);
 
     public abstract void addBundlesFor(String commandName) throws BundleException, IOException;
@@ -58,10 +54,7 @@
     public static void preLoadBundles(Framework framework, List<String> bundleLocations,
             boolean printOSGiInfo) throws BundleException {
         BundleLoader loader = new BundleLoader(printOSGiInfo);
-        List<Bundle> loaded = loader.installAndStartBundles(framework, bundleLocations);
-        for (Bundle bundle : loaded) {
-            preLoadedBundles.add(bundle.getLocation());
-        }
+        loader.installAndStartBundles(framework, bundleLocations);
     }
 
 }
--- a/bundles/src/main/java/com/redhat/thermostat/bundles/impl/OSGiRegistry.java	Mon Aug 20 15:29:14 2012 +0200
+++ b/bundles/src/main/java/com/redhat/thermostat/bundles/impl/OSGiRegistry.java	Tue Aug 21 12:21:15 2012 -0400
@@ -43,9 +43,9 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
@@ -54,21 +54,28 @@
 
 public class OSGiRegistry extends OSGiRegistryService {
 
-    private Collection<String> loadedBundles;
-    Configuration configuration;
+    private Map<String, Bundle> loaded;
+    private Configuration configuration;
     private String thermostatHome;
     private BundleProperties bundleProperties;
     private BundleLoader loader;
 
     OSGiRegistry(Configuration configuration) throws ConfigurationException, FileNotFoundException, IOException {
-        
-        loadedBundles = new HashSet<>(preLoadedBundles);
+        initLoadedBundles();
         thermostatHome = configuration.getThermostatHome();
         bundleProperties = new BundleProperties(thermostatHome);
         this.configuration = configuration;
         loader = new BundleLoader();
     }
 
+    private void initLoadedBundles() {
+        loaded = new HashMap<>();
+        Framework framework = getFramework(this.getClass());
+        for (Bundle bundle: framework.getBundleContext().getBundles()) {
+            loaded.put(bundle.getLocation(), bundle);
+        }
+    }
+
     @Override
     public void setPrintOSGiInfo(boolean printOSGiInfo) {
         configuration.setPrintOSGiInfo(printOSGiInfo);
@@ -84,16 +91,21 @@
         List<String> requiredBundles = bundleProperties.getDependencyResourceNamesFor(commandName);
         List<String> bundlesToLoad = new ArrayList<>();
         for (String resource : requiredBundles) {
-            if (!loadedBundles.contains(resource)) {
+            if (!isBundleActive(framework, resource)) {
                 bundlesToLoad.add(resource);
             }
         }
         List<Bundle> successBundles = loader.installAndStartBundles(framework, bundlesToLoad);
         for (Bundle bundle : successBundles) {
-            loadedBundles.add(bundle.getLocation());
+            loaded.put(bundle.getLocation(), bundle);
         }
     }
 
+    private boolean isBundleActive(Framework framework, String location) {
+        Bundle bundle = loaded.get(location);
+        return (bundle != null) && bundle.getState() == Bundle.ACTIVE;
+    }
+
     private Framework getFramework(Class<?> cls) {
         return (Framework) FrameworkUtil.getBundle(cls).getBundleContext().getBundle(0);
     }