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/14 10:48:46 UTC

svn commit: r726403 - in /geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap: ConfigurationImpl.java ExpressionEvaluator.java

Author: jdillon
Date: Sun Dec 14 01:48:46 2008
New Revision: 726403

URL: http://svn.apache.org/viewvc?rev=726403&view=rev
Log:
(GSHELL-127) Working on making the bootstrap a little bit more configurable

Added:
    geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java   (with props)
Modified:
    geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java

Modified: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java?rev=726403&r1=726402&r2=726403&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java (original)
+++ geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ConfigurationImpl.java Sun Dec 14 01:48:46 2008
@@ -84,8 +84,13 @@
             input.close();
         }
 
-        // TODO: Resolve properties, merge in system?
+        // HACK: Should probably have gshell.home.detected property and pre-set gshell.home=${gshell.home.detected}
+        props.setProperty(GSHELL_HOME, detectHomeDir().getAbsolutePath());
 
+        //
+        // TODO: Load user configuration properties as configured via gshell.properties
+        //
+        
         if (Log.DEBUG) {
             Log.debug("Properties:");
             for (Map.Entry entry : props.entrySet()) {
@@ -96,10 +101,38 @@
         return props;
     }
 
+    /**
+     * 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("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();
+    }
+
+    /**
+     * Get the value of a property, checking system properties, then configuration properties and evaluating the result.
+     */
     private String getProperty(final String name) {
         assert name != null;
         assert props != null;
-        return props.getProperty(name);
+
+        String value = ExpressionEvaluator.getSystemProperty(name, props.getProperty(name));
+
+        return ExpressionEvaluator.evaluate(value, props);
+    }
+
+    private File getPropertyAsFile(final String name) {
+        String path = getProperty(name);
+        if (path != null) {
+            return new File(path);
+        }
+        return null;
     }
 
     public void configure() throws Exception {
@@ -108,9 +141,7 @@
         this.props = loadProperties();
 
         // Export some configuration
-
         setSystemProperty(GSHELL_HOME, getHomeDir().getAbsolutePath());
-
         setSystemProperty(GSHELL_PROGRAM, getProgramName());
     }
 
@@ -124,37 +155,18 @@
         ensureConfigured();
 
         if (homeDir == null) {
-            try {
-                homeDir = detectHomeDir();
-            }
-            catch (Exception e) {
-                throw new RuntimeException(e);
-            }
+            homeDir = getPropertyAsFile(GSHELL_HOME);
         }
 
         return homeDir;
     }
 
-    /**
-     * 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("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 File getLibDir() {
         ensureConfigured();
 
         if (libDir == null) {
-            // FIXME: Check for gshell.lib property
-            libDir = new File(getHomeDir(), "lib");
+            libDir = getPropertyAsFile(GSHELL_LIB);
+            // TODO: If relative root under homeDir?
         }
 
         return libDir;
@@ -164,8 +176,8 @@
         ensureConfigured();
 
         if (etcDir == null) {
-            // FIXME: Check for gshell.etc property
-            etcDir = new File(getHomeDir(), "etc");
+            etcDir = getPropertyAsFile(GSHELL_ETC);
+            // TODO: If relative root under homeDir?
         }
 
         return etcDir;
@@ -187,6 +199,10 @@
 
         classPath.add(getEtcDir().toURI().toURL());
 
+        if (Log.DEBUG) {
+            Log.debug("Finding jars under: " + getLibDir());
+        }
+
         File[] files = getLibDir().listFiles(new FileFilter() {
             public boolean accept(final File file) {
                 return file.isFile() && file.getName().toLowerCase().endsWith(".jar");

Added: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java?rev=726403&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java (added)
+++ geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java Sun Dec 14 01:48:46 2008
@@ -0,0 +1,116 @@
+/*
+ * 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.geronimo.gshell.bootstrap;
+
+import java.util.Properties;
+
+/**
+ * Simple expression evaluator for handling <tt>${variable}</tt> expansion.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ExpressionEvaluator
+{
+    private static final String DELIM_START = "${";
+
+    private static final int DELIM_START_LEN = 2;
+
+    private static final char DELIM_STOP = '}';
+
+    private static final int DELIM_STOP_LEN = 1;
+
+    private final Properties props;
+
+    public ExpressionEvaluator(final Properties props) {
+        // props could be null
+        this.props = props;
+    }
+
+    public String evaluate(final String input) throws IllegalArgumentException {
+        return evaluate(input, props);
+    }
+    
+    public static String evaluate(final String input, final Properties props) throws IllegalArgumentException {
+        StringBuffer sbuf = new StringBuffer();
+
+        int i = 0;
+        int j, k;
+
+        while (true) {
+            j = input.indexOf(DELIM_START, i);
+            if (j == -1) {
+                // no more variables
+                if (i == 0) { // this is a simple string
+                    return input;
+                }
+                else { // add the tail string which contails no variables and return the result.
+                    sbuf.append(input.substring(i, input.length()));
+                    return sbuf.toString();
+                }
+            }
+            else {
+                sbuf.append(input.substring(i, j));
+                k = input.indexOf(DELIM_STOP, j);
+
+                if (k == -1) {
+                    throw new IllegalArgumentException('"' + input + "\" has no closing brace. Opening brace at position " + j);
+                }
+                else {
+                    j += DELIM_START_LEN;
+                    String key = input.substring(j, k);
+
+                    // first try in System properties
+                    String replacement = getSystemProperty(key);
+
+                    // then try props parameter
+                    if (replacement == null && props != null) {
+                        replacement = props.getProperty(key);
+                    }
+
+                    if (replacement != null) {
+                        // Do variable substitution on the replacement string
+                        // such that we can solve "Hello ${x2}" as "Hello p1"
+                        // the where the properties are
+                        // x1=p1
+                        // x2=${x1}
+                        String recursiveReplacement = evaluate(replacement, props);
+                        sbuf.append(recursiveReplacement);
+                    }
+
+                    i = k + DELIM_STOP_LEN;
+                }
+            }
+        }
+    }
+
+    public static String getSystemProperty(final String key, final String def) {
+        try {
+            return System.getProperty(key, def);
+        }
+        catch (Throwable e) {
+            Log.debug("Was not allowed to read system property: " + key);
+            return def;
+        }
+    }
+
+    public static String getSystemProperty(final String key) {
+        return getSystemProperty(key, null);
+    }
+}
\ No newline at end of file

Propchange: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Propchange: geronimo/gshell/trunk/gshell-bootstrap/src/main/java/org/apache/geronimo/gshell/bootstrap/ExpressionEvaluator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain