FELIX-1815: Introduce 'shell:logout' command to close the current shell
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@908230 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/LogoutAction.java b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/LogoutAction.java
new file mode 100644
index 0000000..420f6e5
--- /dev/null
+++ b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/LogoutAction.java
@@ -0,0 +1,35 @@
+/*
+ * 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.felix.karaf.shell.commands;
+
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.karaf.shell.console.CloseShellException;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Command(scope = "shell", name = "logout", description = "Disconnect shell from current session")
+public class LogoutAction extends OsgiCommandSupport {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ protected Object doExecute() throws Exception {
+ log.info("Disconnecting from current session...");
+ throw new CloseShellException();
+ }
+
+}
diff --git a/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/MoreAction.java b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/MoreAction.java
index 5f0b481..567ba93 100644
--- a/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/MoreAction.java
+++ b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/MoreAction.java
@@ -1,3 +1,19 @@
+/*
+ * 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.felix.karaf.shell.commands;
import org.apache.felix.gogo.commands.Command;
diff --git a/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml b/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
index b54758c..6ed39f8 100644
--- a/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
+++ b/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
@@ -57,6 +57,9 @@
<action class="org.apache.felix.karaf.shell.commands.MoreAction"/>
</command>
-->
+ <command name="shell/logout">
+ <action class="org.apache.felix.karaf.shell.commands.LogoutAction"/>
+ </command>
<command name="shell/printf">
<action class="org.apache.felix.karaf.shell.commands.PrintfAction"/>
</command>
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/CloseShellException.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/CloseShellException.java
new file mode 100644
index 0000000..2753112
--- /dev/null
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/CloseShellException.java
@@ -0,0 +1,26 @@
+/*
+ * 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.felix.karaf.shell.console;
+
+/**
+ * Exception thrown to indicate the console to close.
+ */
+public class CloseShellException extends Exception {
+
+}
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
index ed7e454..903c2f8 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/jline/Console.java
@@ -40,6 +40,7 @@
import jline.ConsoleReader;
import jline.Terminal;
import jline.UnsupportedTerminal;
+import org.apache.felix.karaf.shell.console.CloseShellException;
import org.apache.felix.karaf.shell.console.Completer;
import org.apache.felix.karaf.shell.console.completer.AggregateCompleter;
import org.apache.felix.karaf.shell.console.completer.SessionScopeCompleter;
@@ -182,6 +183,10 @@
//System.err.println("^C");
// TODO: interrupt current thread
}
+ catch (CloseShellException e)
+ {
+ break;
+ }
catch (Throwable t)
{
try {