FELIX-1755: provide a script that would be loaded when starting a new shell

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@826748 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/assembly/src/main/distribution/text/etc/shell.init.script b/karaf/assembly/src/main/distribution/text/etc/shell.init.script
new file mode 100644
index 0000000..b58d16d
--- /dev/null
+++ b/karaf/assembly/src/main/distribution/text/etc/shell.init.script
@@ -0,0 +1,24 @@
+//
+// 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.
+//
+// This script is run each time a shell is created.
+// You can define here closures or variables that will be available
+// in each session.
+//
+ld = { log:display $args } ;
+lde = { log:display-exception $args } ;
diff --git a/karaf/assembly/src/main/distribution/text/etc/system.properties b/karaf/assembly/src/main/distribution/text/etc/system.properties
index 330c978..40aa8bc 100644
--- a/karaf/assembly/src/main/distribution/text/etc/system.properties
+++ b/karaf/assembly/src/main/distribution/text/etc/system.properties
@@ -20,4 +20,5 @@
 org.ops4j.pax.logging.DefaultServiceLog.level=ERROR
 karaf.name=root
 karaf.default.repository=system
+karaf.shell.init.script=${karaf.home}/etc/shell.init.script
 xml.catalog.files=
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
index fe27da1..045daf6 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
@@ -18,38 +18,47 @@
  */
 package org.apache.felix.karaf.shell.console.jline;
 
+import java.io.CharArrayWriter;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.InterruptedIOException;
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.io.Reader;
 import java.util.Arrays;
 import java.util.Properties;
-import java.util.concurrent.Callable;
-import java.util.regex.Pattern;
-import java.util.regex.Matcher;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
+import jline.AnsiWindowsTerminal;
 import jline.ConsoleReader;
 import jline.Terminal;
 import jline.UnsupportedTerminal;
-import jline.AnsiWindowsTerminal;
 import org.apache.felix.karaf.shell.console.Completer;
 import org.apache.felix.karaf.shell.console.completer.AggregateCompleter;
 import org.apache.felix.karaf.shell.console.completer.SessionScopeCompleter;
 import org.osgi.service.command.CommandProcessor;
 import org.osgi.service.command.CommandSession;
 import org.osgi.service.command.Converter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class Console implements Runnable
 {
 
+    public static final String SHELL_INIT_SCRIPT = "karaf.shell.init.script";
     public static final String PROMPT = "PROMPT";
     public static final String DEFAULT_PROMPT = "\u001B[1m${USER}\u001B[0m@${APPLICATION}> ";
     public static final String PRINT_STACK_TRACES = "karaf.printStackTraces";
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(Console.class);
+
     private CommandSession session;
     private ConsoleReader reader;
     private BlockingQueue<Integer> queue;
@@ -125,6 +134,32 @@
         running = true;
         pipe.start();
         welcome();
+        String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
+        if (scriptFileName != null) {
+            Reader r = null;
+            try {
+                File scriptFile = new File(scriptFileName);
+                r = new InputStreamReader(new FileInputStream(scriptFile));
+                CharArrayWriter w = new CharArrayWriter();
+                int n;
+                char[] buf = new char[8192];
+                while ((n = r.read(buf)) > 0) {
+                    w.write(buf, 0, n);
+                }
+                session.execute(new String(w.toCharArray()));
+            } catch (Exception e) {
+                LOGGER.debug("Error in initialization script", e);
+                System.err.println("Error in initialization script: " + e.getMessage());
+            } finally {
+                if (r != null) {
+                    try {
+                        r.close();
+                    } catch (IOException e) {
+                        // Ignore
+                    }
+                }
+            }
+        }
         while (running) {
             try {
                 String line = reader.readLine(getPrompt());