You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2009/07/09 12:58:52 UTC

svn commit: r792494 - /felix/trunk/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/TerminalFactory.java

Author: gnodet
Date: Thu Jul  9 10:58:51 2009
New Revision: 792494

URL: http://svn.apache.org/viewvc?rev=792494&view=rev
Log:
Correctly restore terminal at shutdown

Modified:
    felix/trunk/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/TerminalFactory.java

Modified: felix/trunk/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/TerminalFactory.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/TerminalFactory.java?rev=792494&r1=792493&r2=792494&view=diff
==============================================================================
--- felix/trunk/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/TerminalFactory.java (original)
+++ felix/trunk/karaf/gshell/gshell-console/src/main/java/org/apache/felix/karaf/gshell/console/jline/TerminalFactory.java Thu Jul  9 10:58:51 2009
@@ -1,6 +1,8 @@
 package org.apache.felix.karaf.gshell.console.jline;
 
 import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.io.IOException;
 
 import jline.Terminal;
 import jline.WindowsTerminal;
@@ -13,7 +15,9 @@
     private Thread hook;
 
     public Terminal getTerminal() throws Exception {
-        init();
+        if (term == null) {
+            init();
+        }
         return term;
     }
 
@@ -26,23 +30,8 @@
                 t.initializeTerminal();
                 term = t;
             } else {
-                UnixTerminal t = new UnixTerminal();
-                Method mth = UnixTerminal.class.getDeclaredMethod("stty", String.class);
-                mth.setAccessible(true);
-                mth.invoke(null, "intr undef");
+                NoInterruptUnixTerminal t = new NoInterruptUnixTerminal();
                 t.initializeTerminal();
-                hook = new Thread() {
-                    public void run() {
-                        try {
-                            Method mth = UnixTerminal.class.getDeclaredMethod("stty", String.class);
-                            mth.setAccessible(true);
-                            mth.invoke(null, "intr ^C");
-                        } catch (Throwable t) {
-                            t.printStackTrace();
-                        }
-                    }
-                };
-                Runtime.getRuntime().addShutdownHook(hook);
                 term = t;
             }
         } catch (Throwable e) {
@@ -51,10 +40,41 @@
     }
 
     public synchronized void destroy() throws Exception {
-        if (hook != null) {
-            hook.run();
-            Runtime.getRuntime().removeShutdownHook(hook);
-            hook = null;
+        if (term instanceof UnixTerminal) {
+            ((UnixTerminal) term).restoreTerminal();
+        }
+        term = null;
+    }
+
+    public static class NoInterruptUnixTerminal extends UnixTerminal {
+        @Override
+        public void initializeTerminal() throws IOException, InterruptedException {
+            super.initializeTerminal();
+            stty("intr undef");
+        }
+
+        @Override
+        public void restoreTerminal() throws Exception {
+            stty("intr ^C");
+            super.restoreTerminal();
+        }
+
+        protected static String stty(final String args) throws IOException, InterruptedException {
+            try {
+                try {
+                    Method mth = UnixTerminal.class.getDeclaredMethod("stty", String.class);
+                    mth.setAccessible(true);
+                    return (String) mth.invoke(null, args);
+                } catch (InvocationTargetException e) {
+                    throw e.getTargetException();
+                }
+            } catch (IOException e) {
+                throw e;
+            } catch (InterruptedException e) {
+                throw e;
+            } catch (Throwable e) {
+                throw new RuntimeException(e);
+            }
         }
     }