FELIX-2327: Disable and delay Jline terminal initialization

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@945099 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java
index 1a9d4ae..ff0a8df 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/ConsoleFactory.java
@@ -38,7 +38,7 @@
     private BundleContext bundleContext;
     private CommandProcessor commandProcessor;
     private List<Completer> completers;
-    private Terminal terminal;
+    private TerminalFactory terminalFactory;
     private Console console;
     private boolean start;
 
@@ -60,8 +60,8 @@
         this.completers = completers;
     }
 
-    public void setTerminal(Terminal terminal) {
-        this.terminal = terminal;
+    public void setTerminalFactory(TerminalFactory terminalFactory) {
+        this.terminalFactory = terminalFactory;
     }
 
     public void setStart(boolean start) {
@@ -82,6 +82,7 @@
                     }
                 }
             };
+            Terminal terminal = terminalFactory.getTerminal();
             this.console = new Console(commandProcessor,
                                        in,
                                        wrap(out),
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/TerminalFactory.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/TerminalFactory.java
index 20cac2c..585ca4d 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/TerminalFactory.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/TerminalFactory.java
@@ -27,14 +27,19 @@
 
     private Terminal term;
 
-    public Terminal getTerminal() throws Exception {
+    public synchronized Terminal getTerminal() throws Exception {
         if (term == null) {
             init();
         }
         return term;
     }
 
-    public synchronized void init() throws Exception {
+    public void init() throws Exception {
+        if ("jline.UnsupportedTerminal".equals(System.getProperty("jline.terminal"))) {
+            term = new UnsupportedTerminal();
+            return;
+        }
+        
         boolean windows = System.getProperty("os.name").toLowerCase().contains("windows");
         try {
             if (windows) {
@@ -54,8 +59,10 @@
     }
 
     public synchronized void destroy() throws Exception {
-        term.restoreTerminal();
-        term = null;
+        if (term != null) {
+            term.restoreTerminal();
+            term = null;
+        }
     }
 
 }
diff --git a/karaf/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml b/karaf/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml
index 93e6701..f0a7ee7 100644
--- a/karaf/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml
+++ b/karaf/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml
@@ -47,15 +47,10 @@
                 <ref component-id="commandCompleter"/>
             </list>
         </property>
-        <property name="terminal" ref="terminal"/>
+        <property name="terminalFactory" ref="terminalFactory"/>
     </bean>
 
-    <bean id="terminal"
-          factory-ref="terminalFactory"
-          factory-method="getTerminal"/>
-
     <bean id="terminalFactory" class="org.apache.felix.karaf.shell.console.jline.TerminalFactory"
-          init-method="init"
           destroy-method="destroy"/>