Author: cschneider
Date: Fri Mar 30 14:14:43 2012
New Revision: 1307435

URL: http://svn.apache.org/viewvc?rev=1307435&view=rev
Log:
KARAF-713 Factor out property loading and custom Activator handling

Added:
    
karaf/trunk/main/src/main/java/org/apache/karaf/main/KarafActivatorManager.java
    karaf/trunk/main/src/main/java/org/apache/karaf/main/PropertiesLoader.java
Modified:
    karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java
    karaf/trunk/main/src/main/java/org/apache/karaf/main/Stop.java
    karaf/trunk/main/src/main/java/org/apache/karaf/main/util/Utils.java

Added: 
karaf/trunk/main/src/main/java/org/apache/karaf/main/KarafActivatorManager.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/KarafActivatorManager.java?rev=1307435&view=auto
==============================================================================
--- 
karaf/trunk/main/src/main/java/org/apache/karaf/main/KarafActivatorManager.java 
(added)
+++ 
karaf/trunk/main/src/main/java/org/apache/karaf/main/KarafActivatorManager.java 
Fri Mar 30 14:14:43 2012
@@ -0,0 +1,70 @@
+package org.apache.karaf.main;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.jar.Manifest;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.karaf.main.util.BootstrapLogManager;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.launch.Framework;
+
+public class KarafActivatorManager {
+    public static final String KARAF_ACTIVATOR = "Karaf-Activator";
+    Logger LOG = Logger.getLogger(this.getClass().getName());
+
+    private List<BundleActivator> karafActivators = new 
ArrayList<BundleActivator>();
+    private final ClassLoader classLoader;
+    private final Framework framework;
+    
+    public KarafActivatorManager(ClassLoader classLoader, Framework framework) 
{
+        this.classLoader = classLoader;
+        this.framework = framework;
+        LOG.addHandler(BootstrapLogManager.getDefaultHandler());
+    }
+
+    void startKarafActivators() throws IOException {
+        Enumeration<URL> urls = 
classLoader.getResources("META-INF/MANIFEST.MF");
+        while (urls != null && urls.hasMoreElements()) {
+            URL url = urls.nextElement();
+            String className = null;
+            InputStream is = url.openStream();
+            try {
+                Manifest mf = new Manifest(is);
+                className = mf.getMainAttributes().getValue(KARAF_ACTIVATOR);
+                if (className != null) {
+                    BundleActivator activator = (BundleActivator) 
classLoader.loadClass(className).newInstance();
+                    activator.start(framework.getBundleContext());
+                    karafActivators.add(activator);
+                }
+            } catch (Throwable e) {
+                if (className != null) {
+                    System.err.println("Error starting karaf activator " + 
className + ": " + e.getMessage());
+                    LOG.log(Level.WARNING, "Error starting karaf activator " + 
className + " from url " + url, e);
+                }
+            } finally {
+                if (is != null) {
+                    try {
+                        is.close();
+                    } catch (IOException e) {
+                    }
+                }
+            }
+        }
+    }
+
+    void stopKarafActivators() {
+        for (BundleActivator activator : karafActivators) {
+            try {
+                activator.stop(framework.getBundleContext());
+            } catch (Throwable e) {
+                LOG.log(Level.WARNING, "Error stopping karaf activator " + 
activator.getClass().getName(), e);
+            }
+        }
+    }
+}

Modified: karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java?rev=1307435&r1=1307434&r2=1307435&view=diff
==============================================================================
--- karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java (original)
+++ karaf/trunk/main/src/main/java/org/apache/karaf/main/Main.java Fri Mar 30 
14:14:43 2012
@@ -23,7 +23,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
@@ -38,15 +37,12 @@ import java.net.URLClassLoader;
 import java.security.Provider;
 import java.security.Security;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
-import java.util.jar.Manifest;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -58,10 +54,8 @@ import org.apache.karaf.main.util.Bootst
 import org.apache.karaf.main.util.ServerInfoImpl;
 import org.apache.karaf.main.util.SimpleMavenResolver;
 import org.apache.karaf.main.util.StringMap;
-import org.apache.karaf.main.util.SubstHelper;
 import org.apache.karaf.main.util.Utils;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
@@ -198,8 +192,6 @@ public class Main {
     
     public static final String OPTIONALS_PROPERTY = "${optionals}"; // 
optionals includes
 
-    public static final String KARAF_ACTIVATOR = "Karaf-Activator";
-
     public static final String SECURITY_PROVIDERS = 
"org.apache.karaf.security.providers";
 
     Logger LOG = Logger.getLogger(this.getClass().getName());
@@ -219,7 +211,7 @@ public class Main {
     private int shutdownTimeout = 5 * 60 * 1000;
     private boolean exiting = false;
     private ShutdownCallback shutdownCallback;
-    private List<BundleActivator> karafActivators = new 
ArrayList<BundleActivator>();
+    private KarafActivatorManager activatorManager;
 
 
     public Main(String[] args) {
@@ -234,6 +226,12 @@ public class Main {
         karafHome = Utils.getKarafHome(Main.class, Main.PROP_KARAF_HOME, 
Main.ENV_KARAF_HOME);
         karafBase = Utils.getKarafDirectory(Main.PROP_KARAF_BASE, 
Main.ENV_KARAF_BASE, karafHome, false, true);
         karafData = Utils.getKarafDirectory(Main.PROP_KARAF_DATA, 
Main.ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
+        
+        if (Boolean.getBoolean("karaf.restart.clean")) {
+            Utils.deleteDirectory(karafData);
+            karafData = Utils.getKarafDirectory(Main.PROP_KARAF_DATA, 
Main.ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
+        }
+        
         karafInstances = Utils.getKarafDirectory(Main.PROP_KARAF_INSTANCES, 
Main.ENV_KARAF_INSTANCES, new File(karafHome, "instances"), false, false);
 
         Package p = Package.getPackage("org.apache.karaf.main");
@@ -244,20 +242,13 @@ public class Main {
         System.setProperty(PROP_KARAF_DATA, karafData.getPath());
         System.setProperty(PROP_KARAF_INSTANCES, karafInstances.getPath());
 
-        // Load system properties.
-        loadSystemProperties(karafBase);
+        PropertiesLoader.loadSystemProperties(karafBase);
 
         updateInstancePid();
 
-        // Read configuration properties.
-        configProps = loadConfigProperties();
+        configProps = PropertiesLoader.loadConfigProperties(karafBase);
         BootstrapLogManager.setProperties(configProps);
         LOG.addHandler(BootstrapLogManager.getDefaultHandler());
-        
-        // Copy framework properties from the system properties.
-        Main.copySystemProperties(configProps);
-
-        ClassLoader classLoader = createClassLoader(configProps);
 
         processSecurityProperties(configProps);
 
@@ -278,6 +269,7 @@ public class Main {
         shutdownTimeout = 
Integer.parseInt(configProps.getProperty(KARAF_SHUTDOWN_TIMEOUT, 
Integer.toString(shutdownTimeout)));
 
         // Start up the OSGI framework
+        ClassLoader classLoader = createClassLoader(configProps);
         FrameworkFactory factory = loadFrameworkFactory(classLoader);
         framework = factory.newFramework(new StringMap(configProps, false));
         framework.init();
@@ -290,7 +282,8 @@ public class Main {
         framework.getBundleContext().registerService(ServerInfo.class, 
serverInfo, null);
 
         // Start custom activators
-        startKarafActivators(classLoader);
+        activatorManager = new KarafActivatorManager(classLoader, framework);
+        activatorManager.startKarafActivators();
         // Start lock monitor
         new Thread() {
             public void run() {
@@ -311,45 +304,7 @@ public class Main {
         return factory;
     }
 
-    private void startKarafActivators(ClassLoader classLoader) throws 
IOException {
-        Enumeration<URL> urls = 
classLoader.getResources("META-INF/MANIFEST.MF");
-        while (urls != null && urls.hasMoreElements()) {
-            URL url = urls.nextElement();
-            String className = null;
-            InputStream is = url.openStream();
-            try {
-                Manifest mf = new Manifest(is);
-                className = mf.getMainAttributes().getValue(KARAF_ACTIVATOR);
-                if (className != null) {
-                    BundleActivator activator = (BundleActivator) 
classLoader.loadClass(className).newInstance();
-                    activator.start(framework.getBundleContext());
-                    karafActivators.add(activator);
-                }
-            } catch (Throwable e) {
-                if (className != null) {
-                    System.err.println("Error starting karaf activator " + 
className + ": " + e.getMessage());
-                    LOG.log(Level.WARNING, "Error starting karaf activator " + 
className + " from url " + url, e);
-                }
-            } finally {
-                if (is != null) {
-                    try {
-                        is.close();
-                    } catch (IOException e) {
-                    }
-                }
-            }
-        }
-    }
 
-    private void stopKarafActivators() {
-        for (BundleActivator activator : karafActivators) {
-            try {
-                activator.stop(framework.getBundleContext());
-            } catch (Throwable e) {
-                LOG.log(Level.WARNING, "Error stopping karaf activator " + 
activator.getClass().getName(), e);
-            }
-        }
-    }
 
     public void awaitShutdown() throws Exception {
         if (framework == null) {
@@ -400,7 +355,7 @@ public class Main {
                 }
                 FrameworkEvent event = framework.waitForStop(step);
                 if (event.getType() != FrameworkEvent.WAIT_TIMEDOUT) {
-                    stopKarafActivators();
+                    activatorManager.stopKarafActivators();
                     return true;
                 }
             }
@@ -491,12 +446,6 @@ public class Main {
         while (true) {
             boolean restart = false;
             System.setProperty("karaf.restart", "false");
-            if (Boolean.getBoolean("karaf.restart.clean")) {
-                File karafHome = Utils.getKarafHome(Main.class, 
Main.PROP_KARAF_HOME, Main.ENV_KARAF_HOME);
-                File karafBase = Utils.getKarafDirectory(Main.PROP_KARAF_BASE, 
Main.ENV_KARAF_BASE, karafHome, false, true);
-                File karafData = Utils.getKarafDirectory(Main.PROP_KARAF_DATA, 
Main.ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
-                Utils.deleteDirectory(karafData);
-            }
             final Main main = new Main(args);
             try {
                 main.launch();
@@ -682,7 +631,7 @@ public class Main {
             if (st.countTokens() > 0) {
                 String location;
                 do {
-                    location = nextLocation(st);
+                    location = Utils.nextLocation(st);
                     if (location != null) {
                         try {
                             String[] parts = 
Utils.convertToMavenUrlsIfNeeded(location, convertToMavenUrls);
@@ -715,161 +664,6 @@ public class Main {
         return bundles;
     }
 
-    private static String nextLocation(StringTokenizer st) {
-        String retVal = null;
-
-        if (st.countTokens() > 0) {
-            String tokenList = "\" ";
-            StringBuffer tokBuf = new StringBuffer(10);
-            String tok;
-            boolean inQuote = false;
-            boolean tokStarted = false;
-            boolean exit = false;
-            while ((st.hasMoreTokens()) && (!exit)) {
-                tok = st.nextToken(tokenList);
-                if (tok.equals("\"")) {
-                    inQuote = !inQuote;
-                    if (inQuote) {
-                        tokenList = "\"";
-                    } else {
-                        tokenList = "\" ";
-                    }
-
-                } else if (tok.equals(" ")) {
-                    if (tokStarted) {
-                        retVal = tokBuf.toString();
-                        tokStarted = false;
-                        tokBuf = new StringBuffer(10);
-                        exit = true;
-                    }
-                } else {
-                    tokStarted = true;
-                    tokBuf.append(tok.trim());
-                }
-            }
-
-            // Handle case where end of token stream and
-            // still got data
-            if ((!exit) && (tokStarted)) {
-                retVal = tokBuf.toString();
-            }
-        }
-
-        return retVal;
-    }
-
-    /**
-     * <p>
-     * Loads the properties in the system property file associated with the
-     * framework installation into <tt>System.setProperty()</tt>. These 
properties
-     * are not directly used by the framework in anyway. By default, the system
-     * property file is located in the <tt>conf/</tt> directory of the Felix
-     * installation directory and is called "<tt>system.properties</tt>". The
-     * installation directory of Felix is assumed to be the parent directory of
-     * the <tt>felix.jar</tt> file as found on the system class path property.
-     * The precise file from which to load system properties can be set by
-     * initializing the "<tt>felix.system.properties</tt>" system property to 
an
-     * arbitrary URL.
-     * </p>
-     *
-     * @param karafBase the karaf base folder
-     */
-    protected static void loadSystemProperties(File karafBase) {
-        // The system properties file is either specified by a system
-        // property or it is in the same directory as the Felix JAR file.
-        // Try to load it from one of these places.
-
-        // See if the property URL was specified as a property.
-        URL propURL;
-        try {
-            File file = new File(new File(karafBase, "etc"), 
SYSTEM_PROPERTIES_FILE_NAME);
-            propURL = file.toURI().toURL();
-        }
-        catch (MalformedURLException ex) {
-            System.err.print("Main: " + ex);
-            return;
-        }
-
-        // Read the properties file.
-        Properties props = new Properties();
-        InputStream is = null;
-        try {
-            is = propURL.openConnection().getInputStream();
-            props.load(is);
-            is.close();
-        }
-        catch (FileNotFoundException ex) {
-            // Ignore file not found.
-        }
-        catch (Exception ex) {
-            System.err.println(
-                    "Main: Error loading system properties from " + propURL);
-            System.err.println("Main: " + ex);
-            try {
-                if (is != null) is.close();
-            }
-            catch (IOException ex2) {
-                // Nothing we can do.
-            }
-            return;
-        }
-
-        // Perform variable substitution on specified properties.
-        for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
-            String name = (String) e.nextElement();
-            String value = System.getProperty(name, props.getProperty(name));
-            System.setProperty(name, SubstHelper.substVars(value, name, null, 
props));
-        }
-    }
-
-    /**
-     * <p>
-     * Loads the configuration properties in the configuration property file
-     * associated with the framework installation; these properties
-     * are accessible to the framework and to bundles and are intended
-     * for configuration purposes. By default, the configuration property
-     * file is located in the <tt>conf/</tt> directory of the Felix
-     * installation directory and is called "<tt>config.properties</tt>".
-     * The installation directory of Felix is assumed to be the parent
-     * directory of the <tt>felix.jar</tt> file as found on the system class
-     * path property. The precise file from which to load configuration
-     * properties can be set by initializing the 
"<tt>felix.config.properties</tt>"
-     * system property to an arbitrary URL.
-     * </p>
-     *
-     * @return A <tt>Properties</tt> instance or <tt>null</tt> if there was an 
error.
-     * @throws Exception if something wrong occurs
-     */
-    private Properties loadConfigProperties() throws Exception {
-        // See if the property URL was specified as a property.
-        URL configPropURL;
-
-        try {
-            File etcFolder = new File(karafBase, "etc");
-            if (!etcFolder.exists()) {
-                throw new FileNotFoundException("etc folder not found: " + 
etcFolder.getAbsolutePath());
-            }
-            File file = new File(etcFolder, CONFIG_PROPERTIES_FILE_NAME);
-            configPropURL = file.toURI().toURL();
-        }
-        catch (MalformedURLException ex) {
-            System.err.print("Main: " + ex);
-            return null;
-        }
-
-
-        Properties configProps = loadPropertiesFile(configPropURL, false);
-
-        // Perform variable substitution for system properties.
-        for (Enumeration e = configProps.propertyNames(); 
e.hasMoreElements();) {
-            String name = (String) e.nextElement();
-            configProps.setProperty(name,
-                    SubstHelper.substVars(configProps.getProperty(name), name, 
null, configProps));
-        }
-
-        return configProps;
-    }
-
     private void loadStartupProperties(Properties configProps) throws 
Exception {
         // The config properties file is either specified by a system
         // property or it is in the conf/ directory of the Felix
@@ -887,7 +681,7 @@ public class Main {
         }
         File file = new File(etcFolder, STARTUP_PROPERTIES_FILE_NAME);
         startupPropURL = file.toURI().toURL();
-        Properties startupProps = loadPropertiesFile(startupPropURL, true);
+        Properties startupProps = 
PropertiesLoader.loadPropertiesFile(startupPropURL, true);
 
         String defaultRepo = System.getProperty(DEFAULT_REPO, "system");
         if (karafBase.equals(karafHome)) {
@@ -911,7 +705,7 @@ public class Main {
             if (st.countTokens() > 0) {
                 String location;
                 do {
-                    location = nextLocation(st);
+                    location = Utils.nextLocation(st);
                     if (location != null) {
                         File f;
                         if (karafBase.equals(karafHome)) {
@@ -936,89 +730,8 @@ public class Main {
         Main.processConfigurationProperties(configProps, startupProps, 
bundleDirs);
     }
 
-    protected static Properties loadPropertiesFile(URL configPropURL, boolean 
failIfNotFound) throws Exception {
-        // Read the properties file.
-        Properties configProps = new Properties();
-        InputStream is = null;
-        try {
-            is = configPropURL.openConnection().getInputStream();
-            configProps.load(is);
-            is.close();
-        } catch (FileNotFoundException ex) {
-            if (failIfNotFound) {
-                throw ex;
-            } else {
-                System.err.println("WARN: " + configPropURL + " is not found, 
so not loaded");
-            }
-        } catch (Exception ex) {
-            System.err.println("Error loading config properties from " + 
configPropURL);
-            System.err.println("Main: " + ex);
-            return configProps;
-        } finally {
-            try {
-                if (is != null) {
-                    is.close();
-                }
-            }
-            catch (IOException ex2) {
-                // Nothing we can do.
-            }
-        }
-        String includes = configProps.getProperty(INCLUDES_PROPERTY);
-        if (includes != null) {
-            StringTokenizer st = new StringTokenizer(includes, "\" ", true);
-            if (st.countTokens() > 0) {
-                String location;
-                do {
-                    location = nextLocation(st);
-                    if (location != null) {
-                        URL url = new URL(configPropURL, location);
-                        Properties props = loadPropertiesFile(url, true);
-                        configProps.putAll(props);
-                    }
-                }
-                while (location != null);
-            }
-            configProps.remove(INCLUDES_PROPERTY);
-        }
-        String optionals = configProps.getProperty(OPTIONALS_PROPERTY);
-        if (optionals != null) {
-            StringTokenizer st = new StringTokenizer(optionals, "\" ", true);
-            if (st.countTokens() > 0) {
-                String location;
-                do {
-                    location = nextLocation(st);
-                    if (location != null) {
-                        URL url = new URL(configPropURL, location);
-                        Properties props = loadPropertiesFile(url, false);
-                        configProps.putAll(props);
-                    }
-                } while (location != null);
-            }
-            configProps.remove(OPTIONALS_PROPERTY);
-        }
-        for (Enumeration e = configProps.propertyNames(); 
e.hasMoreElements();) {
-            Object key = e.nextElement();
-            if (key instanceof String) {
-                String v = configProps.getProperty((String) key);
-                configProps.put(key, v.trim());
-            }
-        }
-        return configProps;
-    }
 
-    protected static void copySystemProperties(Properties configProps) {
-        for (Enumeration e = System.getProperties().propertyNames();
-             e.hasMoreElements();) {
-            String key = (String) e.nextElement();
-            if (key.startsWith("felix.") ||
-                    key.startsWith("karaf.") ||
-                    key.startsWith("org.osgi.framework.")) {
-                configProps.setProperty(key, System.getProperty(key));
-            }
-        }
-    }
-    
+
     private ClassLoader createClassLoader(Properties configProps) throws 
Exception {
        String framework = configProps.getProperty(KARAF_FRAMEWORK);
         if (framework == null) {

Added: 
karaf/trunk/main/src/main/java/org/apache/karaf/main/PropertiesLoader.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/PropertiesLoader.java?rev=1307435&view=auto
==============================================================================
--- karaf/trunk/main/src/main/java/org/apache/karaf/main/PropertiesLoader.java 
(added)
+++ karaf/trunk/main/src/main/java/org/apache/karaf/main/PropertiesLoader.java 
Fri Mar 30 14:14:43 2012
@@ -0,0 +1,231 @@
+/*
+ * 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.karaf.main;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import org.apache.karaf.main.util.SubstHelper;
+import org.apache.karaf.main.util.Utils;
+
+public class PropertiesLoader {
+
+    /**
+     * <p>
+     * Loads the configuration properties in the configuration property file
+     * associated with the framework installation; these properties
+     * are accessible to the framework and to bundles and are intended
+     * for configuration purposes. By default, the configuration property
+     * file is located in the <tt>conf/</tt> directory of the Felix
+     * installation directory and is called "<tt>config.properties</tt>".
+     * The installation directory of Felix is assumed to be the parent
+     * directory of the <tt>felix.jar</tt> file as found on the system class
+     * path property. The precise file from which to load configuration
+     * properties can be set by initializing the 
"<tt>felix.config.properties</tt>"
+     * system property to an arbitrary URL.
+     * </p>
+     *
+     * @return A <tt>Properties</tt> instance or <tt>null</tt> if there was an 
error.
+     * @throws Exception if something wrong occurs
+     */
+    static Properties loadConfigProperties(File karafBase) throws Exception {
+        // See if the property URL was specified as a property.
+        URL configPropURL;
+
+        try {
+            File etcFolder = new File(karafBase, "etc");
+            if (!etcFolder.exists()) {
+                throw new FileNotFoundException("etc folder not found: " + 
etcFolder.getAbsolutePath());
+            }
+            File file = new File(etcFolder, Main.CONFIG_PROPERTIES_FILE_NAME);
+            configPropURL = file.toURI().toURL();
+        }
+        catch (MalformedURLException ex) {
+            System.err.print("Main: " + ex);
+            return null;
+        }
+
+
+        Properties configProps = loadPropertiesFile(configPropURL, false);
+
+        // Perform variable substitution for system properties.
+        for (Enumeration<?> e = configProps.propertyNames(); 
e.hasMoreElements();) {
+            String name = (String) e.nextElement();
+            configProps.setProperty(name,
+                    SubstHelper.substVars(configProps.getProperty(name), name, 
null, configProps));
+        }
+
+        copySystemProperties(configProps);
+        return configProps;
+    }
+    
+    /**
+     * <p>
+     * Loads the properties in the system property file associated with the
+     * framework installation into <tt>System.setProperty()</tt>. These 
properties
+     * are not directly used by the framework in anyway. By default, the system
+     * property file is located in the <tt>conf/</tt> directory of the Felix
+     * installation directory and is called "<tt>system.properties</tt>". The
+     * installation directory of Felix is assumed to be the parent directory of
+     * the <tt>felix.jar</tt> file as found on the system class path property.
+     * The precise file from which to load system properties can be set by
+     * initializing the "<tt>felix.system.properties</tt>" system property to 
an
+     * arbitrary URL.
+     * </p>
+     *
+     * @param karafBase the karaf base folder
+     */
+    static void loadSystemProperties(File karafBase) {
+        // The system properties file is either specified by a system
+        // property or it is in the same directory as the Felix JAR file.
+        // Try to load it from one of these places.
+    
+        // See if the property URL was specified as a property.
+        URL propURL;
+        try {
+            File file = new File(new File(karafBase, "etc"), 
Main.SYSTEM_PROPERTIES_FILE_NAME);
+            propURL = file.toURI().toURL();
+        }
+        catch (MalformedURLException ex) {
+            System.err.print("Main: " + ex);
+            return;
+        }
+    
+        // Read the properties file.
+        Properties props = new Properties();
+        InputStream is = null;
+        try {
+            is = propURL.openConnection().getInputStream();
+            props.load(is);
+            is.close();
+        }
+        catch (FileNotFoundException ex) {
+            // Ignore file not found.
+        }
+        catch (Exception ex) {
+            System.err.println(
+                    "Main: Error loading system properties from " + propURL);
+            System.err.println("Main: " + ex);
+            try {
+                if (is != null) is.close();
+            }
+            catch (IOException ex2) {
+                // Nothing we can do.
+            }
+            return;
+        }
+    
+        // Perform variable substitution on specified properties.
+        for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
+            String name = (String) e.nextElement();
+            String value = System.getProperty(name, props.getProperty(name));
+            System.setProperty(name, SubstHelper.substVars(value, name, null, 
props));
+        }
+    }
+
+    static void copySystemProperties(Properties configProps) {
+        for (Enumeration<?> e = System.getProperties().propertyNames();
+             e.hasMoreElements();) {
+            String key = (String) e.nextElement();
+            if (key.startsWith("felix.") ||
+                    key.startsWith("karaf.") ||
+                    key.startsWith("org.osgi.framework.")) {
+                configProps.setProperty(key, System.getProperty(key));
+            }
+        }
+    }
+    
+    static Properties loadPropertiesFile(URL configPropURL, boolean 
failIfNotFound) throws Exception {
+        // Read the properties file.
+        Properties configProps = new Properties();
+        InputStream is = null;
+        try {
+            is = configPropURL.openConnection().getInputStream();
+            configProps.load(is);
+            is.close();
+        } catch (FileNotFoundException ex) {
+            if (failIfNotFound) {
+                throw ex;
+            } else {
+                System.err.println("WARN: " + configPropURL + " is not found, 
so not loaded");
+            }
+        } catch (Exception ex) {
+            System.err.println("Error loading config properties from " + 
configPropURL);
+            System.err.println("Main: " + ex);
+            return configProps;
+        } finally {
+            try {
+                if (is != null) {
+                    is.close();
+                }
+            }
+            catch (IOException ex2) {
+                // Nothing we can do.
+            }
+        }
+        String includes = configProps.getProperty(Main.INCLUDES_PROPERTY);
+        if (includes != null) {
+            StringTokenizer st = new StringTokenizer(includes, "\" ", true);
+            if (st.countTokens() > 0) {
+                String location;
+                do {
+                    location = Utils.nextLocation(st);
+                    if (location != null) {
+                        URL url = new URL(configPropURL, location);
+                        Properties props = loadPropertiesFile(url, true);
+                        configProps.putAll(props);
+                    }
+                }
+                while (location != null);
+            }
+            configProps.remove(Main.INCLUDES_PROPERTY);
+        }
+        String optionals = configProps.getProperty(Main.OPTIONALS_PROPERTY);
+        if (optionals != null) {
+            StringTokenizer st = new StringTokenizer(optionals, "\" ", true);
+            if (st.countTokens() > 0) {
+                String location;
+                do {
+                    location = Utils.nextLocation(st);
+                    if (location != null) {
+                        URL url = new URL(configPropURL, location);
+                        Properties props = loadPropertiesFile(url, false);
+                        configProps.putAll(props);
+                    }
+                } while (location != null);
+            }
+            configProps.remove(Main.OPTIONALS_PROPERTY);
+        }
+        for (Enumeration<?> e = configProps.propertyNames(); 
e.hasMoreElements();) {
+            Object key = e.nextElement();
+            if (key instanceof String) {
+                String v = configProps.getProperty((String) key);
+                configProps.put(key, v.trim());
+            }
+        }
+        return configProps;
+    }
+}

Modified: karaf/trunk/main/src/main/java/org/apache/karaf/main/Stop.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/Stop.java?rev=1307435&r1=1307434&r2=1307435&view=diff
==============================================================================
--- karaf/trunk/main/src/main/java/org/apache/karaf/main/Stop.java (original)
+++ karaf/trunk/main/src/main/java/org/apache/karaf/main/Stop.java Fri Mar 30 
14:14:43 2012
@@ -21,13 +21,12 @@ package org.apache.karaf.main;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.Socket;
-import java.net.URL;
-import java.util.Enumeration;
 import java.util.Properties;
 
-import org.apache.karaf.main.util.SubstHelper;
 import org.apache.karaf.main.util.Utils;
 
 /**
@@ -35,6 +34,13 @@ import org.apache.karaf.main.util.Utils;
  */
 public class Stop {
 
+    /**
+     * Sends the shutdown command to the running karaf instance. Uses either a 
shut down port configured in config.properties or
+     * the port from the shutdown port file.
+     * 
+     * @param args
+     * @throws Exception
+     */
     public static void main(String[] args) throws Exception {
         File karafHome = Utils.getKarafHome(Stop.class, Main.PROP_KARAF_HOME, 
Main.ENV_KARAF_HOME);
         File karafBase = Utils.getKarafDirectory(Main.PROP_KARAF_BASE, 
Main.ENV_KARAF_BASE, karafHome, false, true);
@@ -45,29 +51,16 @@ public class Stop {
         System.setProperty(Main.PROP_KARAF_DATA, karafData.getPath());
 
         // Load system properties.
-        Main.loadSystemProperties(karafBase);
+        PropertiesLoader.loadSystemProperties(karafBase);
 
-        File file = new File(new File(karafBase, "etc"), 
Main.CONFIG_PROPERTIES_FILE_NAME);
-        URL configPropURL = file.toURI().toURL();
-        Properties props = Main.loadPropertiesFile(configPropURL, false);
-        Main.copySystemProperties(props);
-
-        // Perform variable substitution for system properties.
-        for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
-            String name = (String) e.nextElement();
-            props.setProperty(name,
-                    SubstHelper.substVars(props.getProperty(name), name, null, 
props));
-        }
+        Properties props = PropertiesLoader.loadConfigProperties(karafBase);
 
         int port = 
Integer.parseInt(props.getProperty(Main.KARAF_SHUTDOWN_PORT, "0"));
         String host = props.getProperty(Main.KARAF_SHUTDOWN_HOST, "localhost");
         String portFile = props.getProperty(Main.KARAF_SHUTDOWN_PORT_FILE);
         String shutdown = props.getProperty(Main.KARAF_SHUTDOWN_COMMAND, 
Main.DEFAULT_SHUTDOWN_COMMAND);
         if (port == 0 && portFile != null) {
-            BufferedReader r = new BufferedReader(new InputStreamReader(new 
FileInputStream(portFile)));
-            String portStr = r.readLine();
-            port = Integer.parseInt(portStr);
-            r.close();
+            port = getPortFromShutdownPortFile(portFile);
         }
         if (port > 0) {
             Socket s = new Socket(host, port);
@@ -78,4 +71,13 @@ public class Stop {
         }
 
     }
+
+    private static int getPortFromShutdownPortFile(String portFile) throws 
FileNotFoundException, IOException {
+        int port;
+        BufferedReader r = new BufferedReader(new InputStreamReader(new 
FileInputStream(portFile)));
+        String portStr = r.readLine();
+        port = Integer.parseInt(portStr);
+        r.close();
+        return port;
+    }
 }

Modified: karaf/trunk/main/src/main/java/org/apache/karaf/main/util/Utils.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/main/src/main/java/org/apache/karaf/main/util/Utils.java?rev=1307435&r1=1307434&r2=1307435&view=diff
==============================================================================
--- karaf/trunk/main/src/main/java/org/apache/karaf/main/util/Utils.java 
(original)
+++ karaf/trunk/main/src/main/java/org/apache/karaf/main/util/Utils.java Fri 
Mar 30 14:14:43 2012
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.net.JarURLConnection;
 import java.net.URI;
 import java.net.URL;
+import java.util.StringTokenizer;
 
 public class Utils {
 
@@ -262,6 +263,49 @@ public class Utils {
                return parts;
        }
 
+    public static String nextLocation(StringTokenizer st) {
+        String retVal = null;
+    
+        if (st.countTokens() > 0) {
+            String tokenList = "\" ";
+            StringBuffer tokBuf = new StringBuffer(10);
+            String tok;
+            boolean inQuote = false;
+            boolean tokStarted = false;
+            boolean exit = false;
+            while ((st.hasMoreTokens()) && (!exit)) {
+                tok = st.nextToken(tokenList);
+                if (tok.equals("\"")) {
+                    inQuote = !inQuote;
+                    if (inQuote) {
+                        tokenList = "\"";
+                    } else {
+                        tokenList = "\" ";
+                    }
+    
+                } else if (tok.equals(" ")) {
+                    if (tokStarted) {
+                        retVal = tokBuf.toString();
+                        tokStarted = false;
+                        tokBuf = new StringBuffer(10);
+                        exit = true;
+                    }
+                } else {
+                    tokStarted = true;
+                    tokBuf.append(tok.trim());
+                }
+            }
+    
+            // Handle case where end of token stream and
+            // still got data
+            if ((!exit) && (tokStarted)) {
+                retVal = tokBuf.toString();
+            }
+        }
+    
+        return retVal;
+    }
+
 
 
 }


Reply via email to