Add quit command

This commit is contained in:
Eric Bottard
2015-12-26 16:58:40 +01:00
parent 2f1a42bde0
commit d68589a69f
5 changed files with 85 additions and 3 deletions

View File

@@ -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();
}

View File

@@ -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()));

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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();
}
}