diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java b/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java index efbbb38..55f240d 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/console/Main.java @@ -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 { } /** + * Allows 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. * diff --git a/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java b/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java index 2ad9601..3d202fe 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Console.java @@ -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 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 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); diff --git a/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java b/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java new file mode 100644 index 0000000..fbd45cb --- /dev/null +++ b/shell/console/src/test/java/org/apache/karaf/shell/console/ExampleSubclassMain.java @@ -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; + +/** + *

+ * 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. + *

+ * + * @author Hiram Chirino + */ +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: + *