You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2010/08/26 16:41:53 UTC

svn commit: r989717 - in /karaf/trunk/shell/console/src: main/java/org/apache/karaf/shell/console/ main/java/org/apache/karaf/shell/console/jline/ test/java/org/apache/karaf/ test/java/org/apache/karaf/shell/ test/java/org/apache/karaf/shell/console/

Author: gnodet
Date: Thu Aug 26 14:41:53 2010
New Revision: 989717

URL: http://svn.apache.org/viewvc?rev=989717&view=rev
Log:
KARAF-173: Make it easier to configure the Karaf Main shell by subclassing. Patch provided by Hiram Chirino

Added:
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/
    karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java
Modified:
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java
    karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java

Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java?rev=989717&r1=989716&r2=989717&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java Thu Aug 26 14:41:53 2010
@@ -127,12 +127,7 @@ public class Main {
     private void run(final CommandShellImpl commandProcessor, String[] args, final InputStream in, final PrintStream out, final PrintStream err) throws Exception {
         TerminalFactory terminalFactory = new TerminalFactory();
         Terminal terminal = terminalFactory.getTerminal();
-        Console console = new Console(commandProcessor, in, out, err, terminal, null, null) {
-            @Override
-            protected Properties loadBrandingProperties() {
-                return super.loadBrandingProperties();
-            }
-        };
+        Console console = createConsole(commandProcessor, in, out, err, terminal);
         CommandSession session = console.getSession();
         session.put("USER", user);
         session.put("APPLICATION", application);
@@ -157,6 +152,21 @@ public class Main {
     }
 
     /**
+     * Allow sub classes of main to change the Console implementation used.
+     * 
+     * @param commandProcessor
+     * @param in
+     * @param out
+     * @param err
+     * @param terminal
+     * @return
+     * @throws Exception
+     */
+    protected Console createConsole(CommandShellImpl commandProcessor, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception {
+        return new Console(commandProcessor, in, out, err, terminal, null, null);
+    }
+
+    /**
      * Sub classes can override so that their registered commands do not conflict with the default shell
      * implementation.
      *

Modified: karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java?rev=989717&r1=989716&r2=989717&view=diff
==============================================================================
--- karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java (original)
+++ karaf/trunk/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java Thu Aug 26 14:41:53 2010
@@ -62,7 +62,7 @@ public class Console implements Runnable
 
     private static final Logger LOGGER = LoggerFactory.getLogger(Console.class);
 
-    private CommandSession session;
+    protected CommandSession session;
     private ConsoleReader reader;
     private BlockingQueue<Integer> queue;
     private boolean interrupt;
@@ -138,9 +138,8 @@ public class Console implements Runnable
         thread = Thread.currentThread();
         running = true;
         pipe.start();
-        Properties props = loadBrandingProperties();
-        welcome(props);
-        setSessionProperties(props);
+        welcome();
+        setSessionProperties();
         String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
         if (scriptFileName != null) {
             Reader r = null;
@@ -232,14 +231,16 @@ public class Console implements Runnable
         return Boolean.parseBoolean(s.toString());
     }
 
-    protected void welcome(Properties props) {
+    protected void welcome() {
+        Properties props = loadBrandingProperties();
         String welcome = props.getProperty("welcome");
         if (welcome != null && welcome.length() > 0) {
             session.getConsole().println(welcome);
         }
     }
 
-    private void setSessionProperties(Properties props) {
+    protected void setSessionProperties() {
+        Properties props = loadBrandingProperties();
         for (Map.Entry<Object, Object> entry : props.entrySet()) {
             String key = (String) entry.getKey();
             if (key.startsWith("session.")) {
@@ -255,7 +256,7 @@ public class Console implements Runnable
         return props;
     }
 
-    private void loadProps(Properties props, String resource) {
+    protected void loadProps(Properties props, String resource) {
         InputStream is = null;
         try {
             is = getClass().getClassLoader().getResourceAsStream(resource);

Added: karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java?rev=989717&view=auto
==============================================================================
--- karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java (added)
+++ karaf/trunk/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java Thu Aug 26 14:41:53 2010
@@ -0,0 +1,115 @@
+/**
+ * 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.shell.console;
+
+import jline.Terminal;
+import org.apache.felix.gogo.runtime.shell.CommandShellImpl;
+import org.apache.karaf.shell.console.jline.Console;
+
+import java.io.InputStream;
+import java.io.PrintStream;
+
+/**
+ * <p>
+ * This class is mostly here so that folks can see an example of how you can extend the Karaf Main shell.  Also
+ * lets Karaf developers see how changes the Main class can affect the interface comparability
+ * with sub classes.
+ * </p>
+ *
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
+ */
+public class ExampleSubclassMain extends Main  {
+
+    public static void main(String args[]) throws Exception {
+        ExampleSubclassMain main = new ExampleSubclassMain();
+        main.run(args);
+    }
+
+    public void ExampleSubclassMain() {
+        // Sets the name of the shell and the current user.
+        setApplication("example");
+        setUser("unknown");
+    }
+
+    @Override
+    protected Console createConsole(CommandShellImpl commandProcessor, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception {
+        return new Console(commandProcessor, in, out, err, terminal, null, null) {
+
+            /**
+             * If you don't overwrite, then karaf will use the welcome message found in the
+             * following resource files:
+             * <ul>
+             *   <li>org/apache/karaf/shell/console/branding.properties</li>
+             *   <li>org/apache/karaf/branding/branding.properties</li>
+             * <ul>
+             */
+            @Override
+            protected void welcome() {
+                session.getConsole().println("===============================================");
+                session.getConsole().println(" Example Shell ");
+                session.getConsole().println("===============================================");
+            }
+
+            /**
+             * If you don't overwrite then Karaf builds a prompt based on the current app and user.
+             * @return
+             */
+            @Override
+            protected String getPrompt() {
+                return "example>";
+            }
+
+            /**
+             * If you don't overwrite then Karaf displays stack traces based on setting
+             * of the karaf.printStackTraces system property.
+             *
+             * @return
+             */
+            @Override
+            protected boolean isPrintStackTraces() {
+                return true;
+            }
+
+            /**
+             * If you don't overwrite, then karaf automatically adds session properties
+             * found in the following resource files:
+             * <ul>
+             *   <li>org/apache/karaf/shell/console/branding.properties</li>
+             *   <li>org/apache/karaf/branding/branding.properties</li>
+             * <ul>
+             */
+            @Override
+            protected void setSessionProperties() {
+                // we won't add any session properties.
+            }
+
+        };
+    }
+
+    /**
+     * if you don't override, then Karaf will discover the commands listed in the
+     * "META-INF/services/org/apache/karaf/shell/commands" resource file.
+     *
+     * @return
+     */
+    @Override
+    public String getDiscoveryResource() {
+        return "META-INF/services/org/example/commands.index";
+    }
+
+
+}