You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/12/12 16:18:33 UTC

svn commit: r726036 - in /geronimo/gshell/trunk: ./ gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/

Author: jdillon
Date: Fri Dec 12 07:18:33 2008
New Revision: 726036

URL: http://svn.apache.org/viewvc?rev=726036&view=rev
Log:
Moving towards more configurable bootstrap

Added:
    geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java
      - copied, changed from r722797, geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java
    geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties   (with props)
Modified:
    geronimo/gshell/trunk/   (props changed)
    geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java
    geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Launcher.java

Propchange: geronimo/gshell/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Fri Dec 12 07:18:33 2008
@@ -1 +1,3 @@
 target-eclipse
+atlassian-ide-plugin.private.xml
+atlassian-ide-plugin.xml

Modified: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java?rev=726036&r1=726035&r2=726036&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java (original)
+++ geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java Fri Dec 12 07:18:33 2008
@@ -19,12 +19,7 @@
 
 package org.apache.geronimo.gshell.bootstrap;
 
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
 import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -32,148 +27,23 @@
  *
  * @version $Rev$ $Date$
  */
-public class Configuration
+public interface Configuration
 {
-    private static final String GSHELL_HOME = "gshell.home";
+    String GSHELL_HOME = "gshell.home";
 
-    private static final String PROGRAM_NAME = "program.name";
+    String GSHELL_ETC = "gshell.etc";
 
-    private static final String DEFAULT_PROGRAM_NAME = "gsh";
+    String GSHELL_LIB = "gshell.lib";
 
-    private static final String LOG4J_CONF = "log4j.configuration";
+    String GSHELL_PROGRAM = "gshell.program";
 
-    private static final String DEFAULT_LOG4J_CONF = "org/apache/geronimo/gshell/bootstrap/default-log4j.xml";
-
-    //
-    // TODO: Support loading an optional properties file to merge into system for persistent customized configuration
-    //
-
-    private File homeDir;
-
-    private String programName;
-
-    private String log4jConfig;
-
-    private ClassLoader classLoader;
-
-    public File getHomeDir() throws IOException {
-        if (homeDir == null) {
-            String path = System.getProperty(GSHELL_HOME);
-            File dir;
-
-            if (path == null) {
-                String jarPath = Launcher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
-                jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
-
-                // The jar containing this class is expected to be in <gshell.home>/lib/boot
-                File bootJar = new File(jarPath);
-                dir = bootJar.getParentFile().getParentFile().getParentFile().getCanonicalFile();
-            }
-            else {
-                dir = new File(path).getCanonicalFile();
-            }
-
-            homeDir = dir;
-        }
-
-        return homeDir;
-    }
-
-    public String getProgramName() {
-        if (programName == null) {
-            programName = System.getProperty(PROGRAM_NAME, DEFAULT_PROGRAM_NAME);
-        }
-
-        return programName;
-    }
-
-    public File getProgramConfigurationFile(final String filename) throws IOException {
-        assert filename != null;
-
-        File etcDir = new File(getHomeDir(), "etc");
-        File file = new File(etcDir, getProgramName() + "-" + filename);
-
-        if (!file.exists()) {
-            File unprefixedFile = new File(etcDir, filename);
-
-            if (unprefixedFile.exists()) {
-                file = unprefixedFile;
-            }
-        }
-
-        return file;
-    }
-
-    public String getLog4jConfig() throws IOException {
-        if (log4jConfig == null) {
-            String path = System.getProperty(LOG4J_CONF);
-            File file;
-
-            if (path == null) {
-                file = getProgramConfigurationFile("log4j.xml");
-            }
-            else {
-                file = new File(path).getCanonicalFile();
-            }
-
-            if (file.exists()) {
-                log4jConfig = file.toURI().toURL().toExternalForm();
-            }
-            else {
-                log4jConfig = DEFAULT_LOG4J_CONF;
-            }
-        }
-
-        return log4jConfig;
-    }
-
-    private void setProperty(final String name, final String value) {
-        System.setProperty(name, value);
-
-        if (Log.DEBUG) {
-            Log.debug("Property: " + name + "=" + value);
-        }
-    }
-
-    public void configure() throws Exception {
-        setProperty(PROGRAM_NAME, getProgramName());
-        setProperty(GSHELL_HOME, getHomeDir().getAbsolutePath());
-        setProperty(LOG4J_CONF, getLog4jConfig());
-    }
-
-    public ClassLoader getClassLoader() throws Exception {
-        if (classLoader == null) {
-            List<URL> classPath = new ArrayList<URL>();
-
-            // Add ${gshell.home}/etc
-            classPath.add(new File(getHomeDir(), "etc").toURI().toURL());
-
-            // Add ${gshell.home}/lib/*.jar
-            File libDir = new File(getHomeDir(), "lib");
-            File[] files = libDir.listFiles(new FileFilter() {
-                public boolean accept(final File file) {
-                    return file.isFile();
-                }
-            });
-
-            if (files == null) {
-                throw new Error("No jars found under: " + libDir);
-            }
-            
-            for (File file : files) {
-                classPath.add(file.toURI().toURL());
-            }
-
-            if (Log.DEBUG) {
-                Log.debug("Classpath:");
-                for (URL url : classPath) {
-                    Log.debug("    " + url);
-                }
-            }
+    void configure() throws Exception;
 
-            classLoader = new URLClassLoader(classPath.toArray(new URL[classPath.size()]), getClass().getClassLoader());
-        }
+    List<URL> getClassPath() throws Exception;
 
-        return classLoader;
-    }
+    String getMainClass();
+    
+    int getSuccessExitCode();
+
+    int getFailureExitCode();
 }
\ No newline at end of file

Copied: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java (from r722797, geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java?p2=geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java&p1=geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java&r1=722797&r2=726036&rev=726036&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Configuration.java (original)
+++ geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java Fri Dec 12 07:18:33 2008
@@ -21,159 +21,188 @@
 
 import java.io.File;
 import java.io.FileFilter;
-import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
-import java.net.URLClassLoader;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
 
 /**
- * Bootstrap configuration.
+ * Bootstrap configuration implementation.
  *
  * @version $Rev$ $Date$
  */
-public class Configuration
+public class ConfigurationImpl
+    implements Configuration
 {
-    private static final String GSHELL_HOME = "gshell.home";
+    private static final int SUCCESS_EXIT_CODE = 0;
 
-    private static final String PROGRAM_NAME = "program.name";
+    private static final int FAILURE_EXIT_CODE = 100;
 
-    private static final String DEFAULT_PROGRAM_NAME = "gsh";
+    private static final String MAIN_CLASS = "org.apache.geronimo.gshell.cli.Main";
 
-    private static final String LOG4J_CONF = "log4j.configuration";
+    private Properties props;
 
-    private static final String DEFAULT_LOG4J_CONF = "org/apache/geronimo/gshell/bootstrap/default-log4j.xml";
+    private File homeDir;
 
-    //
-    // TODO: Support loading an optional properties file to merge into system for persistent customized configuration
-    //
+    private File libDir;
 
-    private File homeDir;
+    private File etcDir;
 
     private String programName;
 
-    private String log4jConfig;
+    private void setSystemProperty(final String name, final String value) {
+        assert name != null;
+        assert value != null;
 
-    private ClassLoader classLoader;
+        if (Log.DEBUG) {
+            Log.debug(name + ": " + value);
+        }
 
-    public File getHomeDir() throws IOException {
-        if (homeDir == null) {
-            String path = System.getProperty(GSHELL_HOME);
-            File dir;
+        System.setProperty(name, value);
+    }
 
-            if (path == null) {
-                String jarPath = Launcher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
-                jarPath = java.net.URLDecoder.decode(jarPath, "UTF-8");
-
-                // The jar containing this class is expected to be in <gshell.home>/lib/boot
-                File bootJar = new File(jarPath);
-                dir = bootJar.getParentFile().getParentFile().getParentFile().getCanonicalFile();
-            }
-            else {
-                dir = new File(path).getCanonicalFile();
-            }
+    private Properties loadProperties() throws Exception {
+        Properties props = new Properties();
 
-            homeDir = dir;
-        }
+        URL defaults = getClass().getResource("default.properties");
+        assert defaults != null;
 
-        return homeDir;
-    }
+        if (Log.DEBUG) {
+            Log.debug("Merging default properties from: " + defaults);
+        }
 
-    public String getProgramName() {
-        if (programName == null) {
-            programName = System.getProperty(PROGRAM_NAME, DEFAULT_PROGRAM_NAME);
+        InputStream input = defaults.openStream();
+        try {
+            props.load(input);
+        }
+        finally {
+            input.close();
         }
 
-        return programName;
+        // TODO: Resolve properties, merge in system?
+
+        return props;
     }
 
-    public File getProgramConfigurationFile(final String filename) throws IOException {
-        assert filename != null;
+    private String getProperty(final String name) {
+        assert name != null;
+        assert props != null;
+        return props.getProperty(name);
+    }
 
-        File etcDir = new File(getHomeDir(), "etc");
-        File file = new File(etcDir, getProgramName() + "-" + filename);
+    public void configure() throws Exception {
+        Log.debug("Configuring");
+        
+        this.props = loadProperties();
 
-        if (!file.exists()) {
-            File unprefixedFile = new File(etcDir, filename);
+        // Export some configuration
 
-            if (unprefixedFile.exists()) {
-                file = unprefixedFile;
-            }
-        }
+        setSystemProperty(GSHELL_HOME, getHomeDir().getAbsolutePath());
 
-        return file;
+        setSystemProperty(GSHELL_PROGRAM, getProgramName());
     }
 
-    public String getLog4jConfig() throws IOException {
-        if (log4jConfig == null) {
-            String path = System.getProperty(LOG4J_CONF);
-            File file;
+    private void ensureConfigured() {
+        if (props == null) {
+            throw new IllegalStateException("Not configured");
+        }
+    }
 
-            if (path == null) {
-                file = getProgramConfigurationFile("log4j.xml");
-            }
-            else {
-                file = new File(path).getCanonicalFile();
-            }
+    public File getHomeDir() {
+        ensureConfigured();
 
-            if (file.exists()) {
-                log4jConfig = file.toURI().toURL().toExternalForm();
+        if (homeDir == null) {
+            try {
+                homeDir = detectHomeDir();
             }
-            else {
-                log4jConfig = DEFAULT_LOG4J_CONF;
+            catch (Exception e) {
+                throw new RuntimeException(e);
             }
         }
 
-        return log4jConfig;
+        return homeDir;
     }
 
-    private void setProperty(final String name, final String value) {
-        System.setProperty(name, value);
-
+    /**
+     * Attempt to detect the home directory, which is expected to be <tt>../../</tt> from the location of the jar containing this class.
+     */
+    private File detectHomeDir() throws Exception {
         if (Log.DEBUG) {
-            Log.debug("Property: " + name + "=" + value);
+            Log.debug("Detecting " + GSHELL_HOME);
         }
+
+        String path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
+        path = URLDecoder.decode(path, "UTF-8");
+        File file = new File(path);
+        return file.getParentFile().getParentFile().getParentFile().getCanonicalFile();
     }
 
-    public void configure() throws Exception {
-        setProperty(PROGRAM_NAME, getProgramName());
-        setProperty(GSHELL_HOME, getHomeDir().getAbsolutePath());
-        setProperty(LOG4J_CONF, getLog4jConfig());
-    }
-
-    public ClassLoader getClassLoader() throws Exception {
-        if (classLoader == null) {
-            List<URL> classPath = new ArrayList<URL>();
-
-            // Add ${gshell.home}/etc
-            classPath.add(new File(getHomeDir(), "etc").toURI().toURL());
-
-            // Add ${gshell.home}/lib/*.jar
-            File libDir = new File(getHomeDir(), "lib");
-            File[] files = libDir.listFiles(new FileFilter() {
-                public boolean accept(final File file) {
-                    return file.isFile();
-                }
-            });
+    public File getLibDir() {
+        ensureConfigured();
 
-            if (files == null) {
-                throw new Error("No jars found under: " + libDir);
-            }
-            
-            for (File file : files) {
-                classPath.add(file.toURI().toURL());
-            }
+        if (libDir == null) {
+            // FIXME: Check for gshell.lib property
+            libDir = new File(getHomeDir(), "lib");
+        }
+
+        return libDir;
+    }
+
+    public File getEtcDir() {
+        ensureConfigured();
+
+        if (etcDir == null) {
+            // FIXME: Check for gshell.etc property
+            etcDir = new File(getHomeDir(), "etc");
+        }
+
+        return etcDir;
+    }
+
+    public String getProgramName() {
+        ensureConfigured();
 
-            if (Log.DEBUG) {
-                Log.debug("Classpath:");
-                for (URL url : classPath) {
-                    Log.debug("    " + url);
-                }
+        if (programName == null) {
+            programName = getProperty(GSHELL_PROGRAM);
+        }
+        
+        return programName;
+    }
+
+    public List<URL> getClassPath() throws Exception {
+        ensureConfigured();
+        List<URL> classPath = new ArrayList<URL>();
+
+        classPath.add(getEtcDir().toURI().toURL());
+
+        File[] files = getLibDir().listFiles(new FileFilter() {
+            public boolean accept(final File file) {
+                return file.isFile() && file.getName().toLowerCase().endsWith(".jar");
             }
+        });
 
-            classLoader = new URLClassLoader(classPath.toArray(new URL[classPath.size()]), getClass().getClassLoader());
+        if (files == null) {
+            throw new Error("No jars found under: " + libDir);
         }
 
-        return classLoader;
+        for (File file : files) {
+            classPath.add(file.toURI().toURL());
+        }
+
+        return classPath;
+    }
+
+    public String getMainClass() {
+        return MAIN_CLASS;
+    }
+
+    public int getSuccessExitCode() {
+        return SUCCESS_EXIT_CODE;
+    }
+
+    public int getFailureExitCode() {
+        return FAILURE_EXIT_CODE;
     }
 }
\ No newline at end of file

Modified: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Launcher.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Launcher.java?rev=726036&r1=726035&r2=726036&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Launcher.java (original)
+++ geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/Launcher.java Fri Dec 12 07:18:33 2008
@@ -21,6 +21,9 @@
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.List;
 
 /**
  * Bootstrap launcher.
@@ -29,53 +32,74 @@
  */
 public class Launcher
 {
-    private static final int SUCCESS_EXIT_CODE = 0;
+    private final Configuration config;
 
-    private static final int FAILURE_EXIT_CODE = 100;
-
-    private static final String MAIN_CLASS = "org.apache.geronimo.gshell.cli.Main";
+    public Launcher() {
+        this.config = new ConfigurationImpl();
+    }
 
     public static void main(final String[] args) {
         assert args != null;
 
+        Launcher launcher = new Launcher();
+        launcher.run(args);
+    }
+
+    public void run(final String[] args) {
+        assert args != null;
+
         try {
+            config.configure();
+
             launch(args);
 
             Log.debug("Exiting");
 
-            System.exit(SUCCESS_EXIT_CODE);
+            System.exit(config.getSuccessExitCode());
         }
         catch (Throwable t) {
             Log.debug("Failure: " + t);
             
             t.printStackTrace(System.err);
             System.err.flush();
-            System.exit(FAILURE_EXIT_CODE);
+
+            System.exit(config.getFailureExitCode());
         }
     }
 
-    public static void launch(final String[] args) throws Exception {
+    public void launch(final String[] args) throws Exception {
         assert args != null;
 
-        Log.debug("Configuring");
+        Log.debug("Launching");
 
-        Configuration config = new Configuration();
-        config.configure();
+        ClassLoader cl = getClassLoader();
 
-        ClassLoader cl = config.getClassLoader();
-        Class type = cl.loadClass(MAIN_CLASS);
+        Class type = cl.loadClass(config.getMainClass());
         Method method = getMainMethod(type);
 
         Thread.currentThread().setContextClassLoader(cl);
 
         if (Log.DEBUG) {
-            Log.debug("Launching: " + method);
+            Log.debug("Invoking: " + method);
         }
-
+        
         method.invoke(null, new Object[] { args });
     }
 
-    private static Method getMainMethod(final Class type) throws Exception {
+    private ClassLoader getClassLoader() throws Exception {
+        List<URL> classPath = config.getClassPath();
+        if (Log.DEBUG) {
+            Log.debug("Classpath:");
+            for (URL url : classPath) {
+                Log.debug("    " + url);
+            }
+        }
+
+        ClassLoader parent = getClass().getClassLoader();
+        return new URLClassLoader(classPath.toArray(new URL[classPath.size()]), parent);
+    }
+
+    private Method getMainMethod(final Class type) throws Exception {
         assert type != null;
 
         Method method = type.getMethod("main", String[].class);

Added: geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties?rev=726036&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties (added)
+++ geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties Fri Dec 12 07:18:33 2008
@@ -0,0 +1,30 @@
+##
+## 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.
+##
+
+##
+## $Rev$ $Date$
+##
+
+gshell.home=
+
+gshell.etc=${gshell.home}/etc
+
+gshell.lib=${gshell.home}/lib
+
+gshell.program=gsh

Propchange: geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-bootstrap/src/main/resources/org/apache/geronimo/gshell/bootstrap/default.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain