Modified framework launcher to only use standard OSGi API. (FELIX-1462)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@802037 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/main/src/main/java/org/apache/felix/main/AutoActivator.java b/main/src/main/java/org/apache/felix/main/AutoProcessor.java
similarity index 91%
rename from main/src/main/java/org/apache/felix/main/AutoActivator.java
rename to main/src/main/java/org/apache/felix/main/AutoProcessor.java
index 7d502de..1a3f926 100644
--- a/main/src/main/java/org/apache/felix/main/AutoActivator.java
+++ b/main/src/main/java/org/apache/felix/main/AutoProcessor.java
@@ -23,7 +23,7 @@
 import org.osgi.framework.*;
 import org.osgi.service.startlevel.*;
 
-public class AutoActivator implements BundleActivator
+public class AutoProcessor
 {
     /**
      * The property name used for the bundle directory.
@@ -62,32 +62,16 @@
     **/
     public static final String AUTO_START_PROP = "felix.auto.start";
 
-    private final Map m_configMap;
-
-    public AutoActivator(Map configMap)
-    {
-        m_configMap = configMap;
-    }
-
     /**
-     * Used to instigate auto-install and auto-start configuration
-     * property processing via a custom framework activator during
-     * framework startup.
+     * Used to instigate auto-deploy directory process and auto-install/auto-start
+     * configuration property processing during.
+     * @param configMap Map of configuration properties.
      * @param context The system bundle context.
     **/
-    public void start(BundleContext context)
+    public static void process(Map configMap, BundleContext context)
     {
-        processAutoDeploy(context);
-        processAutoProperties(context);
-    }
-
-    /**
-     * Currently does nothing as part of framework shutdown.
-     * @param context The system bundle context.
-    **/
-    public void stop(BundleContext context)
-    {
-        // Do nothing.
+        processAutoDeploy(configMap, context);
+        processAutoProperties(configMap, context);
     }
 
     /**
@@ -96,10 +80,10 @@
      * starting each one.
      * </p>
      */
-    private void processAutoDeploy(BundleContext context)
+    private static void processAutoDeploy(Map configMap, BundleContext context)
     {
         // Determine if auto deploy actions to perform.
-        String action = (String) m_configMap.get(AUTO_DEPLOY_ACTION_PROPERY);
+        String action = (String) configMap.get(AUTO_DEPLOY_ACTION_PROPERY);
         action = (action == null) ? "" : action;
         List actionList = new ArrayList();
         StringTokenizer st = new StringTokenizer(action, ",");
@@ -127,7 +111,7 @@
             }
 
             // Get the auto deploy directory.
-            String autoDir = (String) m_configMap.get(AUTO_DEPLOY_DIR_PROPERY);
+            String autoDir = (String) configMap.get(AUTO_DEPLOY_DIR_PROPERY);
             autoDir = (autoDir == null) ? AUTO_DEPLOY_DIR_VALUE : autoDir;
             // Look in the specified bundle directory to create a list
             // of all JAR files to install.
@@ -236,7 +220,7 @@
      * specified configuration properties.
      * </p>
      */
-    private void processAutoProperties(BundleContext context)
+    private static void processAutoProperties(Map configMap, BundleContext context)
     {
         // Retrieve the Start Level service, since it will be needed
         // to set the start level of the installed bundles.
@@ -252,7 +236,7 @@
         // property name, where "n" is the desired start level for the list
         // of bundles. If no start level is specified, the default start
         // level is assumed.
-        for (Iterator i = m_configMap.keySet().iterator(); i.hasNext(); )
+        for (Iterator i = configMap.keySet().iterator(); i.hasNext(); )
         {
             String key = ((String) i.next()).toLowerCase();
 
@@ -279,7 +263,7 @@
             }
 
             // Parse and install the bundles associated with the key.
-            StringTokenizer st = new StringTokenizer((String) m_configMap.get(key), "\" ", true);
+            StringTokenizer st = new StringTokenizer((String) configMap.get(key), "\" ", true);
             for (String location = nextLocation(st); location != null; location = nextLocation(st))
             {
                 try
@@ -296,12 +280,12 @@
         }
 
         // Now loop through the auto-start bundles and start them.
-        for (Iterator i = m_configMap.keySet().iterator(); i.hasNext(); )
+        for (Iterator i = configMap.keySet().iterator(); i.hasNext(); )
         {
             String key = ((String) i.next()).toLowerCase();
             if (key.startsWith(AUTO_START_PROP))
             {
-                StringTokenizer st = new StringTokenizer((String) m_configMap.get(key), "\" ", true);
+                StringTokenizer st = new StringTokenizer((String) configMap.get(key), "\" ", true);
                 for (String location = nextLocation(st); location != null; location = nextLocation(st))
                 {
                     // Installing twice just returns the same bundle.
diff --git a/main/src/main/java/org/apache/felix/main/Main.java b/main/src/main/java/org/apache/felix/main/Main.java
index 6cfa7b7..1d86312 100644
--- a/main/src/main/java/org/apache/felix/main/Main.java
+++ b/main/src/main/java/org/apache/felix/main/Main.java
@@ -22,11 +22,10 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.*;
-import org.apache.felix.framework.Felix;
-import org.apache.felix.framework.util.FelixConstants;
 import org.apache.felix.framework.util.Util;
 import org.osgi.framework.Constants;
 import org.osgi.framework.launch.Framework;
+import org.osgi.framework.launch.FrameworkFactory;
 
 /**
  * <p>
@@ -67,7 +66,7 @@
      */
     public static final String CONFIG_DIRECTORY = "conf";
 
-    private static Framework m_felix = null;
+    private static Framework m_fwk = null;
 
     /**
      * <p>
@@ -219,7 +218,7 @@
         // that overwrites anything in the config file.
         if (bundleDir != null)
         {
-            configProps.setProperty(AutoActivator.AUTO_DEPLOY_DIR_PROPERY, bundleDir);
+            configProps.setProperty(AutoProcessor.AUTO_DEPLOY_DIR_PROPERY, bundleDir);
         }
 
         // If there is a passed in bundle cache directory, then
@@ -229,25 +228,24 @@
             configProps.setProperty(Constants.FRAMEWORK_STORAGE, cacheDir);
         }
 
-        // Create a list for custom framework activators and
-        // add an instance of the auto-activator it for processing
-        // auto-install and auto-start properties. Add this list
-        // to the configuration properties.
-        List list = new ArrayList();
-        list.add(new AutoActivator(configProps));
-        configProps.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
-
         // Print welcome banner.
         System.out.println("\nWelcome to Felix.");
         System.out.println("=================\n");
 
         try
         {
-            // Create an instance and start the framework.
-            m_felix = new Felix(configProps);
-            m_felix.start();
+            // Create an instance of the framework.
+            FrameworkFactory factory = getFrameworkFactory();
+            m_fwk = factory.newFramework(configProps);
+            // Initialize the framework, but don't start it yet.
+            m_fwk.init();
+            // Use the system bundle context to process the auto-deploy
+            // and auto-install/auto-start properties.
+            AutoProcessor.process(configProps, m_fwk.getBundleContext());
+            // Start the framework.
+            m_fwk.start();
             // Wait for framework to stop to exit the VM.
-            m_felix.waitForStop(0);
+            m_fwk.waitForStop(0);
             System.exit(0);
         }
         catch (Exception ex)
@@ -259,6 +257,41 @@
     }
 
     /**
+     * Simple method to parse META-INF/services file for framework factory.
+     * Currently, it assumes the first non-commented line is the class name
+     * of the framework factory implementation.
+     * @return The created <tt>FrameworkFactory</tt> instance.
+     * @throws Exception if any errors occur.
+    **/
+    private static FrameworkFactory getFrameworkFactory() throws Exception
+    {
+        URL url = Main.class.getClassLoader().getResource(
+            "META-INF/services/org.osgi.framework.launch.FrameworkFactory");
+        if (url != null)
+        {
+            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
+            try
+            {
+                for (String s = br.readLine(); s != null; s = br.readLine())
+                {
+                    s = s.trim();
+                    // Try to load first non-empty, non-commented line.
+                    if ((s.length() > 0) && (s.charAt(0) != '#'))
+                    {
+                        return (FrameworkFactory) Class.forName(s).newInstance();
+                    }
+                }
+            }
+            finally
+            {
+                if (br != null) br.close();
+            }
+        }
+
+        throw new Exception("Could not find framework factory.");
+    }
+
+    /**
      * <p>
      * Loads the properties in the system property file associated with the
      * framework installation into <tt>System.setProperty()</tt>. These properties