SpringShell聲明的命令, 在默認(rèn)情況下都是可用的. 但有時(shí)我們要實(shí)現(xiàn)命令依賴, 就是說要執(zhí)行這條命令必須先執(zhí)行依賴的命令, 就像ftp命令, 需要先使用open 命令連接到ftp服務(wù)器之后, 才能執(zhí)行其它命令. SpringShell 中可借助于自定義命令是否可用方法的方式來實(shí)現(xiàn)命令依賴, 對(duì)此, SpringShell提供了三種方式來限制命令是否可用. 但無論哪種方式都需要提供一個(gè)判斷命令是否可用的方法.
1. 校驗(yàn)是否可用方法
- 方法返回值類型為 Availability, 當(dāng)不可用時(shí)需要設(shè)置提示信息
- 方法為無參方法
public Availability $methodName(){
String message = "Sorry! You are not connected";
return $boolen ? Availability.available() : Availability.unavailable(message);
}
2. 三種方式
2.1 默認(rèn)方法名方式
方法名必須為 "命令名Availability", 一個(gè)方法只能限制一條命令
@ShellComponent
public class AvailabilityCommands {
private boolean connected;
@ShellMethod("Connect to the server.")
public void connect(String user, String password) {
connected = true;
}
@ShellMethod("Download the nuclear codes.")
public String download() {
return "execute downloading ...";
}
// 只能限制download 命令
public Availability downloadAvailability(){
String message = "Sorry! You are not connected";
return connected ? Availability.available() : Availability.unavailable(message);
}
}
2.2 注解修飾命令
聲明命令時(shí), 使用@ShellMethodAvailability 注解指定該命令是否可用的校驗(yàn)方法, 可指定多個(gè)校驗(yàn)方法
@ShellComponent
public class AvailabilityCommands {
private boolean connected;
@ShellMethod("Connect to the server.")
public void connect(String user, String password) {
connected = true;
}
@ShellMethod("Download the nuclear codes.")
@ShellMethodAvailability({"checkAuth"})
public String download() {
return "execute downloading ...";
}
public Availability checkAuth() {
String message = "Sorry! You are not connected";
return connected ? Availability.available() : Availability.unavailable(message);
}
}
2.3 定義校驗(yàn)方法時(shí), 指定約束的方法
定義校驗(yàn)方法時(shí), 聲明該校驗(yàn)方法約束的命令, 可聲明多個(gè)命令.
@ShellComponent
public class AvailabilityCommands {
private boolean connected;
@ShellMethod("Connect to the server.")
public void connect(String user, String password) {
connected = true;
}
@ShellMethod("Download the nuclear codes.")
public String download() {
return "execute downloading ...";
}
// 指定該方法限制的命令
@ShellMethodAvailability({"download"})
public Availability checkAuth() {
String message = "Sorry! You are not connected";
return connected ? Availability.available() : Availability.unavailable(message);
}
}
3. 測(cè)試
3.1 查看命令列表
不可用命令, 會(huì)添加*號(hào)修飾
shell:>help
AVAILABLE COMMANDS
Availability Commands
connect: Connect to the server.
* download: Download the nuclear codes.
3.2 執(zhí)行不可用命令
執(zhí)行不可用命令時(shí), 會(huì)彈出提示信息. beacause 后就是自定義的錯(cuò)誤提示信息
shell:>download
Command 'download' exists but is not currently available because Sorry! You are not connected
Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.
3.3 正確執(zhí)行方式
# 先執(zhí)行connect 命令
shell:>connect root 123456
# 執(zhí)行connect命令之后, download命令可執(zhí)行
shell:>download
execute downloading ...
# 再次查看可用命令, 會(huì)發(fā)現(xiàn)download前已無*號(hào)
shell:>help
AVAILABLE COMMANDS
Availability Commands
connect: Connect to the server.
download: Download the nuclear codes.