You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2012/09/27 11:55:42 UTC

svn commit: r1390914 - in /karaf/trunk/features/core/src: main/java/org/apache/karaf/features/internal/ main/resources/OSGI-INF/blueprint/ test/java/org/apache/karaf/features/ test/java/org/apache/karaf/features/internal/

Author: cschneider
Date: Thu Sep 27 09:55:41 2012
New Revision: 1390914

URL: http://svn.apache.org/viewvc?rev=1390914&view=rev
Log:
KARAF-608 Small refactoring of the FeatureServiceImpl. Extracting the config management and the boot feature installation

Added:
    karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BootFeaturesInstaller.java   (with props)
    karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java   (with props)
Modified:
    karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
    karaf/trunk/features/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
    karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
    karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java

Added: karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BootFeaturesInstaller.java
URL: http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BootFeaturesInstaller.java?rev=1390914&view=auto
==============================================================================
--- karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BootFeaturesInstaller.java (added)
+++ karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BootFeaturesInstaller.java Thu Sep 27 09:55:41 2012
@@ -0,0 +1,99 @@
+package org.apache.karaf.features.internal;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.FeaturesService;
+import org.apache.karaf.features.FeaturesService.Option;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Manages installation of the boot features in the background
+ */
+public class BootFeaturesInstaller {
+	private static final Logger LOGGER = LoggerFactory.getLogger(BootFeaturesInstaller.class);
+
+    public static String VERSION_PREFIX = "version=";
+
+    private final FeaturesService featuresService;
+    private final String boot;
+
+    /**
+     * 
+     * @param featuresService
+     * @param boot list of boot features separated by comma. Optionally contains ;version=x.x.x to specify a specific feature version
+     */
+    public BootFeaturesInstaller(FeaturesService featuresService, String boot) {
+		this.featuresService = featuresService;
+		this.boot = boot;
+	}
+    
+    /**
+     * Install boot features
+     * @throws Exception
+     */
+    public void start() throws Exception {
+        if (boot != null) {
+            new Thread() {
+                public void run() {
+                    installBootFeatures();
+                }
+            }.start();
+        }
+    }
+    
+	void installBootFeatures() {
+		Set<Feature> features = determineFeaturesToInstall();
+        try {
+            featuresService.installFeatures(features, EnumSet.of(Option.NoCleanIfFailure, Option.ContinueBatchOnFailure));
+        } catch (Exception e) {
+            LOGGER.error("Error installing boot features", e);
+        }
+	}
+	
+    private Set<Feature> determineFeaturesToInstall() {
+    	List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
+        String[] list = boot.split(",");
+        Set<Feature> features = new LinkedHashSet<Feature>();
+        for (String f : list) {
+            f = f.trim();
+            if (f.length() > 0) {
+                String featureVersion = null;
+
+                // first we split the parts of the feature string to gain access to the version info
+                // if specified
+                String[] parts = f.split(";");
+                String featureName = parts[0];
+                for (String part : parts) {
+                    // if the part starts with "version=" it contains the version info
+                    if (part.startsWith(VERSION_PREFIX)) {
+                        featureVersion = part.substring(VERSION_PREFIX.length());
+                    }
+                }
+
+                if (featureVersion == null) {
+                    // no version specified - use default version
+                    featureVersion = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION;
+                }
+
+                try {
+                    // try to grab specific feature version
+                    Feature feature = featuresService.getFeature(featureName, featureVersion);
+                    if (feature != null && !installedFeatures.contains(feature)) {
+                        features.add(feature);
+                    } else {
+                        LOGGER.error("Error installing boot feature " + f + ": feature not found");
+                    }
+                } catch (Exception e) {
+                    LOGGER.error("Error installing boot feature " + f, e);
+                }
+            }
+        }
+		return features;
+	}
+}

Propchange: karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BootFeaturesInstaller.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
URL: http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java?rev=1390914&view=auto
==============================================================================
--- karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java (added)
+++ karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java Thu Sep 27 09:55:41 2012
@@ -0,0 +1,150 @@
+package org.apache.karaf.features.internal;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.karaf.features.ConfigFileInfo;
+import org.apache.karaf.features.Feature;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class FeatureConfigInstaller {
+	private static final Logger LOGGER = LoggerFactory.getLogger(FeaturesServiceImpl.class);
+    private static final String CONFIG_KEY = "org.apache.karaf.features.configKey";
+
+    private final ConfigurationAdmin configAdmin;
+    
+    FeatureConfigInstaller(ConfigurationAdmin configAdmin) {
+		this.configAdmin = configAdmin;
+	}
+
+    private String[] parsePid(String pid) {
+        int n = pid.indexOf('-');
+        if (n > 0) {
+            String factoryPid = pid.substring(n + 1);
+            pid = pid.substring(0, n);
+            return new String[]{pid, factoryPid};
+        } else {
+            return new String[]{pid, null};
+        }
+    }
+
+    private Configuration createConfiguration(ConfigurationAdmin configurationAdmin,
+                                                String pid, String factoryPid) throws IOException, InvalidSyntaxException {
+        if (factoryPid != null) {
+            return configurationAdmin.createFactoryConfiguration(pid, null);
+        } else {
+            return configurationAdmin.getConfiguration(pid, null);
+        }
+    }
+
+    private Configuration findExistingConfiguration(ConfigurationAdmin configurationAdmin,
+                                                      String pid, String factoryPid) throws IOException, InvalidSyntaxException {
+        String filter;
+        if (factoryPid == null) {
+            filter = "(" + Constants.SERVICE_PID + "=" + pid + ")";
+        } else {
+            String key = createConfigurationKey(pid, factoryPid);
+            filter = "(" + CONFIG_KEY + "=" + key + ")";
+        }
+        Configuration[] configurations = configurationAdmin.listConfigurations(filter);
+        if (configurations != null && configurations.length > 0) {
+            return configurations[0];
+        }
+        return null;
+    }
+
+    void installFeatureConfigs(Feature feature, boolean verbose) throws IOException, InvalidSyntaxException {
+        for (String config : feature.getConfigurations().keySet()) {
+            Dictionary<String,String> props = new Hashtable<String, String>(feature.getConfigurations().get(config));
+            String[] pid = parsePid(config);
+            Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]);
+            if (cfg == null) {
+                cfg = createConfiguration(configAdmin, pid[0], pid[1]);
+                String key = createConfigurationKey(pid[0], pid[1]);
+                props.put(CONFIG_KEY, key);
+                if (cfg.getBundleLocation() != null) {
+                    cfg.setBundleLocation(null);
+                }
+                cfg.update(props);
+            }
+        }
+        for (ConfigFileInfo configFile : feature.getConfigurationFiles()) {
+            installConfigurationFile(configFile.getLocation(), configFile.getFinalname(), configFile.isOverride(), verbose);
+        }
+    }
+
+    private String createConfigurationKey(String pid, String factoryPid) {
+        return factoryPid == null ? pid : pid + "-" + factoryPid;
+    }
+
+    private void installConfigurationFile(String fileLocation, String finalname, boolean override, boolean verbose) throws IOException {
+    	LOGGER.debug("Checking configuration file " + fileLocation);
+        if (verbose) {
+            System.out.println("Checking configuration file " + fileLocation);
+        }
+    	
+    	String basePath = System.getProperty("karaf.base");
+    	
+    	if (finalname.indexOf("${") != -1) {
+    		//remove any placeholder or variable part, this is not valid.
+    		int marker = finalname.indexOf("}");
+    		finalname = finalname.substring(marker+1);
+    	}
+    	
+    	finalname = basePath + File.separator + finalname;
+    	
+    	File file = new File(finalname); 
+    	if (file.exists() && !override) {
+    		LOGGER.debug("configFile already exist, don't override it");
+    		return;
+    	}
+
+        InputStream is = null;
+        FileOutputStream fop = null;
+        try {
+            is = new BufferedInputStream(new URL(fileLocation).openStream());
+
+            if (!file.exists()) {
+                File parentFile = file.getParentFile();
+                if (parentFile != null)
+                    parentFile.mkdirs();
+                file.createNewFile();
+            }
+
+            fop = new FileOutputStream(file);
+        
+            int bytesRead;
+            byte[] buffer = new byte[1024];
+            
+            while ((bytesRead = is.read(buffer)) != -1) {
+                fop.write(buffer, 0, bytesRead);
+            }
+        } catch (RuntimeException e) {
+            LOGGER.error(e.getMessage());
+            throw e;
+        } catch (MalformedURLException e) {
+        	LOGGER.error(e.getMessage());
+            throw e;
+		} finally {
+			if (is != null)
+				is.close();
+            if (fop != null) {
+			    fop.flush();
+			    fop.close();
+            }
+		}
+            
+    }
+}

Propchange: karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java?rev=1390914&r1=1390913&r2=1390914&view=diff
==============================================================================
--- karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java (original)
+++ karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java Thu Sep 27 09:55:41 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.karaf.features.internal;
 
+import static java.lang.String.format;
+
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -23,7 +25,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -36,16 +37,13 @@ import java.util.EnumSet;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Hashtable;
 import java.util.Iterator;
-import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 import java.util.regex.Matcher;
@@ -57,7 +55,6 @@ import org.apache.felix.utils.version.Ve
 import org.apache.felix.utils.version.VersionTable;
 import org.apache.karaf.features.BundleInfo;
 import org.apache.karaf.features.Conditional;
-import org.apache.karaf.features.ConfigFileInfo;
 import org.apache.karaf.features.Dependency;
 import org.apache.karaf.features.Feature;
 import org.apache.karaf.features.FeatureEvent;
@@ -76,19 +73,15 @@ import org.osgi.framework.Filter;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkListener;
 import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.Version;
 import org.osgi.framework.startlevel.BundleStartLevel;
 import org.osgi.framework.wiring.FrameworkWiring;
-import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.url.URLStreamHandlerService;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static java.lang.String.format;
-
 /**
  * The Features service implementation.
  * Adding a repository url will load the features contained in this repository and
@@ -96,44 +89,25 @@ import static java.lang.String.format;
  * installing the needed bundles.
  */
 public class FeaturesServiceImpl implements FeaturesService {
-
-    public static final String CONFIG_KEY = "org.apache.karaf.features.configKey";
-    public static String VERSION_PREFIX = "version=";
-
     private static final Logger LOGGER = LoggerFactory.getLogger(FeaturesServiceImpl.class);
 
-    private BundleContext bundleContext;
-    private ConfigurationAdmin configAdmin;
+    private final BundleContext bundleContext;
+    private final FeatureConfigInstaller configManager;
+
     private long resolverTimeout = 5000;
     private Set<URI> uris;
     private Map<URI, RepositoryImpl> repositories = new HashMap<URI, RepositoryImpl>();
     private Map<String, Map<String, Feature>> features;
     private Map<Feature, Set<Long>> installed = new HashMap<Feature, Set<Long>>();
-    private String boot;
-    AtomicBoolean bootFeaturesInstalled = new AtomicBoolean();
     private List<FeaturesListener> listeners = new CopyOnWriteArrayIdentityList<FeaturesListener>();
     private ThreadLocal<Repository> repo = new ThreadLocal<Repository>();
     private EventAdminListener eventAdminListener;
     private long refreshTimeout = 5000;
     private RegionsPersistence regionsPersistence;
 
-    public FeaturesServiceImpl() {
-    }
-
-    public BundleContext getBundleContext() {
-        return bundleContext;
-    }
-
-    public void setBundleContext(BundleContext bundleContext) {
-        this.bundleContext = bundleContext;
-    }
-
-    public ConfigurationAdmin getConfigAdmin() {
-        return configAdmin;
-    }
-
-    public void setConfigAdmin(ConfigurationAdmin configAdmin) {
-        this.configAdmin = configAdmin;
+    public FeaturesServiceImpl(BundleContext bundleContext, ConfigurationAdmin configAdmin) {
+		this.bundleContext = bundleContext;
+		this.configManager = new FeatureConfigInstaller(configAdmin);
     }
 
     public long getResolverTimeout() {
@@ -185,10 +159,6 @@ public class FeaturesServiceImpl impleme
         }
     }
 
-    public void setBoot(String boot) {
-        this.boot = boot;
-    }
-
     /**
      * Validate a features repository XML.
      *
@@ -433,10 +403,10 @@ public class FeaturesServiceImpl impleme
                     state.installed.addAll(s.installed);
 
                     //Check if current feature satisfies the conditionals of existing features
-                    for (Feature installedFeautre : listInstalledFeatures()) {
-                        for (Conditional conditional : installedFeautre.getConditional()) {
+                    for (Feature installedFeature : listInstalledFeatures()) {
+                        for (Conditional conditional : installedFeature.getConditional()) {
                             if (dependenciesSatisfied(conditional.getCondition(), state)) {
-                                doInstallFeatureConditionals(s, installedFeautre, verbose);
+                                doInstallFeatureConditionals(s, installedFeature, verbose);
                             }
                         }
                     }
@@ -592,7 +562,7 @@ public class FeaturesServiceImpl impleme
         for (Dependency dependency : feature.getDependencies()) {
             installFeatureDependency(dependency, state, verbose);
         }
-        installFeatureConfigs(feature, verbose);
+        configManager.installFeatureConfigs(feature, verbose);
         Set<Long> bundles = new TreeSet<Long>();
         
         for (BundleInfo bInfo : resolve(feature)) {
@@ -641,30 +611,6 @@ public class FeaturesServiceImpl impleme
         }
     }
     
-    private void installFeatureConfigs(Feature feature, boolean verbose) throws IOException, InvalidSyntaxException {
-        for (String config : feature.getConfigurations().keySet()) {
-            Dictionary<String,String> props = new Hashtable<String, String>(feature.getConfigurations().get(config));
-            String[] pid = parsePid(config);
-            Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]);
-            if (cfg == null) {
-                cfg = createConfiguration(configAdmin, pid[0], pid[1]);
-                String key = createConfigurationKey(pid[0], pid[1]);
-                props.put(CONFIG_KEY, key);
-                if (cfg.getBundleLocation() != null) {
-                    cfg.setBundleLocation(null);
-                }
-                cfg.update(props);
-            }
-        }
-        for (ConfigFileInfo configFile : feature.getConfigurationFiles()) {
-            installConfigurationFile(configFile.getLocation(), configFile.getFinalname(), configFile.isOverride(), verbose);
-        }
-    }
-
-    private String createConfigurationKey(String pid, String factoryPid) {
-        return factoryPid == null ? pid : pid + "-" + factoryPid;
-    }
-
     protected List<BundleInfo> resolve(Feature feature) throws Exception {
         String resolver = feature.getResolver();
         // If no resolver is specified, we expect a list of uris
@@ -877,7 +823,7 @@ public class FeaturesServiceImpl impleme
             if (verbose) {
                 System.out.println("Installing bundle " + bundleLocation);
             }
-            Bundle b = getBundleContext().installBundle(bundleLocation, is);
+            Bundle b = bundleContext.installBundle(bundleLocation, is);
             
             // Define the startLevel for the bundle when defined
             int ibsl = bundleInfo.getStartLevel();
@@ -895,64 +841,7 @@ public class FeaturesServiceImpl impleme
         }
     }
     
-    public void installConfigurationFile(String fileLocation, String finalname, boolean override, boolean verbose) throws IOException {
-    	LOGGER.debug("Checking configuration file " + fileLocation);
-        if (verbose) {
-            System.out.println("Checking configuration file " + fileLocation);
-        }
-    	
-    	String basePath = System.getProperty("karaf.base");
-    	
-    	if (finalname.indexOf("${") != -1) {
-    		//remove any placeholder or variable part, this is not valid.
-    		int marker = finalname.indexOf("}");
-    		finalname = finalname.substring(marker+1);
-    	}
-    	
-    	finalname = basePath + File.separator + finalname;
-    	
-    	File file = new File(finalname); 
-    	if (file.exists() && !override) {
-    		LOGGER.debug("configFile already exist, don't override it");
-    		return;
-    	}
 
-        InputStream is = null;
-        FileOutputStream fop = null;
-        try {
-            is = new BufferedInputStream(new URL(fileLocation).openStream());
-
-            if (!file.exists()) {
-                File parentFile = file.getParentFile();
-                if (parentFile != null)
-                    parentFile.mkdirs();
-                file.createNewFile();
-            }
-
-            fop = new FileOutputStream(file);
-        
-            int bytesRead;
-            byte[] buffer = new byte[1024];
-            
-            while ((bytesRead = is.read(buffer)) != -1) {
-                fop.write(buffer, 0, bytesRead);
-            }
-        } catch (RuntimeException e) {
-            LOGGER.error(e.getMessage());
-            throw e;
-        } catch (MalformedURLException e) {
-        	LOGGER.error(e.getMessage());
-            throw e;
-		} finally {
-			if (is != null)
-				is.close();
-            if (fop != null) {
-			    fop.flush();
-			    fop.close();
-            }
-		}
-            
-    }
 
     public void uninstallFeature(String name) throws Exception {
         List<String> versions = new ArrayList<String>();
@@ -998,7 +887,7 @@ public class FeaturesServiceImpl impleme
             bundles.removeAll(b);
         }
         for (long bundleId : bundles) {
-            Bundle b = getBundleContext().getBundle(bundleId);
+            Bundle b = bundleContext.getBundle(bundleId);
             if (b != null) {
                 b.uninstall();
             }
@@ -1103,9 +992,8 @@ public class FeaturesServiceImpl impleme
         return features;
     }
 
-    public void start() throws Exception {
-        // Register EventAdmin listener
-        EventAdminListener listener = null;
+    private void registerEventAdminListener() {
+		EventAdminListener listener = null;
         try {
             getClass().getClassLoader().loadClass("org.bundles.service.event.EventAdmin");
             listener = new EventAdminListener(bundleContext);
@@ -1114,7 +1002,9 @@ public class FeaturesServiceImpl impleme
             LOGGER.debug("EventAdmin package is not available, just don't use it");
         }
         this.eventAdminListener = listener;
-        // Load State
+	}
+
+	private void initState() {
         if (!loadState()) {
             if (uris != null) {
                 for (URI uri : uris) {
@@ -1127,57 +1017,11 @@ public class FeaturesServiceImpl impleme
             }
             saveState();
         }
-        // Install boot features
-        if (boot != null && !bootFeaturesInstalled.get()) {
-            new Thread() {
-                public void run() {
-                    // splitting the features
-                    String[] list = boot.split(",");
-                    Set<Feature> features = new LinkedHashSet<Feature>();
-                    for (String f : list) {
-                        f = f.trim();
-                        if (f.length() > 0) {
-                            String featureVersion = null;
-
-                            // first we split the parts of the feature string to gain access to the version info
-                            // if specified
-                            String[] parts = f.split(";");
-                            String featureName = parts[0];
-                            for (String part : parts) {
-                                // if the part starts with "version=" it contains the version info
-                                if (part.startsWith(VERSION_PREFIX)) {
-                                    featureVersion = part.substring(VERSION_PREFIX.length());
-                                }
-                            }
-
-                            if (featureVersion == null) {
-                                // no version specified - use default version
-                                featureVersion = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION;
-                            }
-
-                            try {
-                                // try to grab specific feature version
-                                Feature feature = getFeature(featureName, featureVersion);
-                                if (feature != null) {
-                                    features.add(feature);
-                                } else {
-                                    LOGGER.error("Error installing boot feature " + f + ": feature not found");
-                                }
-                            } catch (Exception e) {
-                                LOGGER.error("Error installing boot feature " + f, e);
-                            }
-                        }
-                    }
-                    try {
-                        installFeatures(features, EnumSet.of(Option.NoCleanIfFailure, Option.ContinueBatchOnFailure));
-                    } catch (Exception e) {
-                        LOGGER.error("Error installing boot features", e);
-                    }
-                    bootFeaturesInstalled.set(true);
-                    saveState();
-                }
-            }.start();
-        }
+	}
+    
+    public void start() throws Exception {
+        registerEventAdminListener();
+        initState();
     }
 
     public void stop() throws Exception {
@@ -1206,49 +1050,12 @@ public class FeaturesServiceImpl impleme
         }
     }
 
-    protected String[] parsePid(String pid) {
-        int n = pid.indexOf('-');
-        if (n > 0) {
-            String factoryPid = pid.substring(n + 1);
-            pid = pid.substring(0, n);
-            return new String[]{pid, factoryPid};
-        } else {
-            return new String[]{pid, null};
-        }
-    }
-
-    protected Configuration createConfiguration(ConfigurationAdmin configurationAdmin,
-                                                String pid, String factoryPid) throws IOException, InvalidSyntaxException {
-        if (factoryPid != null) {
-            return configurationAdmin.createFactoryConfiguration(pid, null);
-        } else {
-            return configurationAdmin.getConfiguration(pid, null);
-        }
-    }
-
-    protected Configuration findExistingConfiguration(ConfigurationAdmin configurationAdmin,
-                                                      String pid, String factoryPid) throws IOException, InvalidSyntaxException {
-        String filter;
-        if (factoryPid == null) {
-            filter = "(" + Constants.SERVICE_PID + "=" + pid + ")";
-        } else {
-            String key = createConfigurationKey(pid, factoryPid);
-            filter = "(" + CONFIG_KEY + "=" + key + ")";
-        }
-        Configuration[] configurations = configurationAdmin.listConfigurations(filter);
-        if (configurations != null && configurations.length > 0) {
-            return configurations[0];
-        }
-        return null;
-    }
-
     protected void saveState() {
         try {
             File file = bundleContext.getDataFile("FeaturesServiceState.properties");
             Properties props = new Properties();
             saveSet(props, "repositories.", repositories.keySet());
             saveMap(props, "features.", installed);
-            props.put("bootFeaturesInstalled", Boolean.toString(bootFeaturesInstalled.get()));
             OutputStream os = new FileOutputStream(file);
             try {
                 props.store(new FileOutputStream(file), "FeaturesService State");
@@ -1285,7 +1092,6 @@ public class FeaturesServiceImpl impleme
             for (Feature f : installed.keySet()) {
                 callListeners(new FeatureEvent(f, FeatureEvent.EventType.FeatureInstalled, true));
             }
-            bootFeaturesInstalled.set(Boolean.parseBoolean((String) props.get("bootFeaturesInstalled")));
             return true;
         } catch (Exception e) {
             LOGGER.error("Error loading FeaturesService state", e);
@@ -1525,6 +1331,9 @@ public class FeaturesServiceImpl impleme
     private void waitForUrlHandler(String protocol) {
         try {
             Filter filter = bundleContext.createFilter("(&(" + Constants.OBJECTCLASS + "=" + URLStreamHandlerService.class.getName() + ")(url.handler.protocol=" + protocol + "))");
+            if (filter == null) {
+                return;
+            }
             ServiceTracker<URLStreamHandlerService, URLStreamHandlerService> urlHandlerTracker = new ServiceTracker<URLStreamHandlerService, URLStreamHandlerService>(bundleContext, filter, null);
             try {
                 urlHandlerTracker.open();

Modified: karaf/trunk/features/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml?rev=1390914&r1=1390913&r2=1390914&view=diff
==============================================================================
--- karaf/trunk/features/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml (original)
+++ karaf/trunk/features/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml Thu Sep 27 09:55:41 2012
@@ -38,13 +38,17 @@ http://www.osgi.org/xmlns/blueprint-ext/
     </ext:property-placeholder>
 
     <bean id="featuresService" class="org.apache.karaf.features.internal.FeaturesServiceImpl" init-method="start" destroy-method="stop">
+    	<argument ref="blueprintBundleContext" />
+    	<argument ref="configAdmin" />
         <property name="urls" value="$[featuresRepositories]" />
-        <property name="boot" value="$[featuresBoot]" />
         <property name="resolverTimeout" value="$[resolverTimeout]" />
-        <property name="configAdmin" ref="configAdmin" />
-        <property name="bundleContext" ref="blueprintBundleContext" />
         <property name="regionsPersistence" ref="regionsPersistence" />
     </bean>
+    
+    <bean id="bootFeaturesInstaller" class="org.apache.karaf.features.internal.BootFeaturesInstaller" init-method="start">
+    	<argument ref="featuresService" />
+        <argument value="$[featuresBoot]" />
+    </bean>
 
     <reference-list id="featuresListeners" interface="org.apache.karaf.features.FeaturesListener" availability="optional">
         <reference-listener ref="featuresService"

Modified: karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java?rev=1390914&r1=1390913&r2=1390914&view=diff
==============================================================================
--- karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java (original)
+++ karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java Thu Sep 27 09:55:41 2012
@@ -80,8 +80,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
         
         Repository[] repositories = svc.listRepositories();
@@ -158,8 +157,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle, framework);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         verify(bundleContext, installedBundle, framework);
@@ -258,8 +256,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
 
         // Adds Repository
         svc.addRepository(uri);                                                     
@@ -338,8 +335,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle, framework);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeature("f1", "0.1");
@@ -374,8 +370,7 @@ public class FeaturesServiceTest extends
 
         BundleContext bundleContext = prepareBundleContextForInstallUninstall();
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeature("f1", "0.1");
@@ -409,8 +404,7 @@ public class FeaturesServiceTest extends
 
         BundleContext bundleContext = prepareBundleContextForInstallUninstall();
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeature("f1", "0.1");
@@ -443,8 +437,7 @@ public class FeaturesServiceTest extends
 
         BundleContext bundleContext = prepareBundleContextForInstallUninstall();
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeature("f1", "0.1");
@@ -508,8 +501,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle, framework);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeature("f2", "0.1");
@@ -547,8 +539,7 @@ public class FeaturesServiceTest extends
         EasyMock.expectLastCall().andReturn(File.createTempFile("test", "test")); 
         EasyMock.replay(bundleContext);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         Feature feature = svc.getFeature("f2", "[0.1,0.3)");
@@ -650,8 +641,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle1, installedBundle2);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeatures(new CopyOnWriteArraySet<Feature>(Arrays.asList(svc.listFeatures())),
@@ -710,8 +700,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle1, installedBundle2);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         svc.installFeatures(new CopyOnWriteArraySet<Feature>(Arrays.asList(svc.listFeatures())),
@@ -766,8 +755,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle1, installedBundle2);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         try {
@@ -829,8 +817,7 @@ public class FeaturesServiceTest extends
 
         replay(bundleContext, installedBundle1, installedBundle2);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         try {
@@ -913,8 +900,7 @@ public class FeaturesServiceTest extends
 
         replay(wiring, framework, bundleContext, installedBundle1, installedBundle2);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
 
         List<Feature> features = Arrays.asList(svc.listFeatures());
@@ -948,8 +934,7 @@ public class FeaturesServiceTest extends
         expect(bundle.adapt(FrameworkWiring.class)).andReturn(null);
         expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         try {
             svc.addRepository(uri);
             fail("exception expected");
@@ -981,8 +966,7 @@ public class FeaturesServiceTest extends
         expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
         replay(bundleContext);
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl();
-        svc.setBundleContext(bundleContext);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleContext, null);
         svc.addRepository(uri);
         Feature feature = svc.getFeature("f1");
         Assert.assertNotNull("No feature named fi found", feature);        

Modified: karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java
URL: http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java?rev=1390914&r1=1390913&r2=1390914&view=diff
==============================================================================
--- karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java (original)
+++ karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java Thu Sep 27 09:55:41 2012
@@ -57,7 +57,7 @@ public class FeaturesServiceImplTest ext
         org.apache.karaf.features.internal.model.Feature feature = new org.apache.karaf.features.internal.model.Feature("transaction");
         versions.put("1.0.0", feature);
         features.put("transaction", versions);
-        final FeaturesServiceImpl impl = new FeaturesServiceImpl() {
+        final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws Exception {
                 return features;
             };
@@ -72,7 +72,7 @@ public class FeaturesServiceImplTest ext
         org.apache.karaf.features.internal.model.Feature feature = new org.apache.karaf.features.internal.model.Feature("transaction");
         versions.put("1.0.0", feature);
         features.put("transaction", versions);
-        final FeaturesServiceImpl impl = new FeaturesServiceImpl() {
+        final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws Exception {
                 return features;
             };
@@ -86,7 +86,7 @@ public class FeaturesServiceImplTest ext
         Map<String, Feature> versions = new HashMap<String, Feature>();
         versions.put("1.0.0", new org.apache.karaf.features.internal.model.Feature("transaction"));
         features.put("transaction", versions);
-        final FeaturesServiceImpl impl = new FeaturesServiceImpl() {
+        final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws Exception {
                 return features;
             };
@@ -100,7 +100,7 @@ public class FeaturesServiceImplTest ext
         versions.put("1.0.0", new org.apache.karaf.features.internal.model.Feature("transaction", "1.0.0"));
         versions.put("2.0.0", new org.apache.karaf.features.internal.model.Feature("transaction", "2.0.0"));
         features.put("transaction", versions);
-        final FeaturesServiceImpl impl = new FeaturesServiceImpl() {
+        final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws Exception {
                 return features;
             };
@@ -115,8 +115,7 @@ public class FeaturesServiceImplTest ext
         bundleContext.addFrameworkListener(EasyMock.<FrameworkListener>anyObject());
         bundleContext.removeFrameworkListener(EasyMock.<FrameworkListener>anyObject());
         replay(bundleContext);
-        FeaturesServiceImpl service = new FeaturesServiceImpl();
-        service.setBundleContext(bundleContext);
+        FeaturesServiceImpl service = new FeaturesServiceImpl(bundleContext, null);
         try {
             service.setUrls("mvn:inexistent/features/1.0/xml/features");
             service.start();
@@ -141,7 +140,7 @@ public class FeaturesServiceImplTest ext
         versions2.put("1.0.0", new org.apache.karaf.features.internal.model.Feature("ssh", "1.0.0"));
         features.put("ssh", versions2);
 
-        final FeaturesServiceImpl impl = new FeaturesServiceImpl() {
+        final FeaturesServiceImpl impl = new FeaturesServiceImpl(bundleContext, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws Exception {
                 return features;
             };
@@ -155,15 +154,13 @@ public class FeaturesServiceImplTest ext
             protected void saveState() {
             }
         };
-        impl.setBundleContext(bundleContext);
+       
+        BootFeaturesInstaller bootFeatures = new BootFeaturesInstaller(impl, "transaction;version=1.2,ssh;version=1.0.0");
 
         try {
             Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0]));
-            impl.setBoot("transaction;version=1.2,ssh;version=1.0.0");
             impl.start();
-            while (!impl.bootFeaturesInstalled.get()) {
-                Thread.sleep(10L);
-            }
+            bootFeatures.installBootFeatures();
             assertFalse("Feature transaction 1.0.0 should not be installed", impl.isInstalled(impl.getFeature("transaction", "1.0.0")));
             assertFalse("Feature transaction 2.0.0 should not be installed", impl.isInstalled(impl.getFeature("transaction", "2.0.0")));
             assertTrue("Feature ssh should be installed", impl.isInstalled(impl.getFeature("ssh", "1.0.0")));
@@ -179,7 +176,7 @@ public class FeaturesServiceImplTest ext
     public void testNoDuplicateFeaturesInstallation() throws Exception {
         final List<Feature> installed = new LinkedList<Feature>();
 
-        final FeaturesServiceImpl impl = new FeaturesServiceImpl() {
+        final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             // override methods which refers to bundle context to avoid mocking everything
             @Override
             protected boolean loadState() {
@@ -223,7 +220,7 @@ public class FeaturesServiceImplTest ext
     }
 
     public void testGetOptionalImportsOnly() {
-        FeaturesServiceImpl service = new FeaturesServiceImpl();
+        FeaturesServiceImpl service = new FeaturesServiceImpl(null, null);
 
         List<Clause> result = service.getOptionalImports("org.apache.karaf,org.apache.karaf.optional;resolution:=optional");
         assertEquals("One optional import expected", 1, result.size());