The shell has a problem when being stopped using the BundleActivator.stop() method but not getting any input because it is blocked on System.in. This patch fixes the issue by using the ready() method to wait until input is available. There still is a race condition in case more then one Thread is listening on System.in but we can ignore this for now because in case the race condition happens we default to the old behaviour.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@642238 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java b/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java
index b8d025b..1b94a62 100644
--- a/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java
+++ b/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java
@@ -83,10 +83,13 @@
// since one might already be available.
initializeService();
- // Start impl thread.
- m_thread = new Thread(
- m_runnable = new ShellTuiRunnable(),
- "Felix Shell TUI");
+ synchronized (this)
+ {
+ // Start impl thread.
+ m_thread = new Thread(
+ m_runnable = new ShellTuiRunnable(),
+ "Felix Shell TUI");
+ }
m_thread.start();
}
@@ -107,16 +110,19 @@
public void stop(BundleContext context)
{
- if (m_runnable != null)
+ synchronized (this)
{
- m_runnable.stop();
- m_thread.interrupt();
+ if (m_runnable != null)
+ {
+ m_runnable.stop();
+ m_thread.interrupt();
+ }
}
}
private class ShellTuiRunnable implements Runnable
{
- private boolean stop = false;
+ private volatile boolean stop = false;
public void stop()
{
@@ -127,15 +133,22 @@
{
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
-
while (!stop)
{
System.out.print("-> ");
try
{
+ while (!in.ready())
+ {
+ Thread.sleep(20);
+ }
line = in.readLine();
}
+ catch (InterruptedException ex)
+ {
+ continue;
+ }
catch (IOException ex)
{
System.err.println("Could not read input, please try again.");
@@ -175,4 +188,4 @@
}
}
}
-}
\ No newline at end of file
+}