From d68589a69f1c12ccef49cb5140d938c0b1e41845 Mon Sep 17 00:00:00 2001 From: Eric Bottard Date: Sat, 26 Dec 2015 16:58:40 +0100 Subject: [PATCH] Add quit command --- .../org/springframework/shell2/Bootstrap.java | 2 +- .../shell2/DefaultMethodTargetResolver.java | 2 +- .../springframework/shell2/ExitRequest.java | 39 +++++++++++++++++++ .../springframework/shell2/JLineShell.java | 10 ++++- .../springframework/shell2/commands/Quit.java | 35 +++++++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/springframework/shell2/ExitRequest.java create mode 100644 src/main/java/org/springframework/shell2/commands/Quit.java diff --git a/src/main/java/org/springframework/shell2/Bootstrap.java b/src/main/java/org/springframework/shell2/Bootstrap.java index dadb1aaa..011c7198 100644 --- a/src/main/java/org/springframework/shell2/Bootstrap.java +++ b/src/main/java/org/springframework/shell2/Bootstrap.java @@ -36,7 +36,7 @@ import org.springframework.shell.converters.SimpleFileConverter; value = {AvailableCommandsConverter.class, SimpleFileConverter.class})) public class Bootstrap { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(Bootstrap.class); context.getBean(JLineShell.class).run(); } diff --git a/src/main/java/org/springframework/shell2/DefaultMethodTargetResolver.java b/src/main/java/org/springframework/shell2/DefaultMethodTargetResolver.java index 406aaa65..ba520d9d 100644 --- a/src/main/java/org/springframework/shell2/DefaultMethodTargetResolver.java +++ b/src/main/java/org/springframework/shell2/DefaultMethodTargetResolver.java @@ -39,7 +39,7 @@ public class DefaultMethodTargetResolver implements MethodTargetResolver { ShellMethod shellMapping = method.getAnnotation(ShellMethod.class); String[] keys = shellMapping.value(); if (keys.length == 0) { - keys = new String[]{method.getName()}; + keys = new String[] {method.getName()}; } for (String key : keys) { methodTargets.put(key, new MethodTarget(method, bean, shellMapping.help())); diff --git a/src/main/java/org/springframework/shell2/ExitRequest.java b/src/main/java/org/springframework/shell2/ExitRequest.java new file mode 100644 index 00000000..f5d4d9b6 --- /dev/null +++ b/src/main/java/org/springframework/shell2/ExitRequest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed 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.springframework.shell2; + +/** + * This exception, when thrown and caught, will ask the shell to gracefully shutdown. + * + * @author Eric Bottard + */ +public class ExitRequest extends RuntimeException { + + private final int code; + + public ExitRequest() { + this(0); + } + + public ExitRequest(int code) { + this.code = code; + } + + public int status() { + return 0; + } +} diff --git a/src/main/java/org/springframework/shell2/JLineShell.java b/src/main/java/org/springframework/shell2/JLineShell.java index 4432b288..3b32d8e1 100644 --- a/src/main/java/org/springframework/shell2/JLineShell.java +++ b/src/main/java/org/springframework/shell2/JLineShell.java @@ -16,6 +16,8 @@ package org.springframework.shell2; +import java.io.Closeable; +import java.io.IOException; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.Arrays; @@ -123,7 +125,7 @@ public class JLineShell implements Shell { } - public void run() { + public void run() throws IOException { while (true) { lineReader.readLine("shell:>"); String separator = ""; @@ -177,6 +179,12 @@ public class JLineShell implements Shell { try { result = ReflectionUtils.invokeMethod(methodTarget.getMethod(), methodTarget.getBean(), rawArgs); } + catch (ExitRequest e) { + if (applicationContext instanceof Closeable) { + ((Closeable)applicationContext).close(); + System.exit(e.status()); + } + } catch (Exception e) { result = e; } diff --git a/src/main/java/org/springframework/shell2/commands/Quit.java b/src/main/java/org/springframework/shell2/commands/Quit.java new file mode 100644 index 00000000..272ddec2 --- /dev/null +++ b/src/main/java/org/springframework/shell2/commands/Quit.java @@ -0,0 +1,35 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed 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.springframework.shell2.commands; + +import org.springframework.shell2.ExitRequest; +import org.springframework.shell2.ShellComponent; +import org.springframework.shell2.ShellMethod; + +/** + * A command that terminates the running shell. + * + * @author Eric Bottard + */ +@ShellComponent("") +public class Quit { + + @ShellMethod(help = "Exit the shell") + public void quit() { + throw new ExitRequest(); + } +}