05.SpringShell命令限制可用-@ShellMethodAvailability

SpringShell声明的命令, 在默认情况下都是可用的. 但有时我们要实现命令依赖, 就是说要执行这条命令必须先执行依赖的命令, 就像ftp命令, 需要先使用open 命令连接到ftp服务器之后, 才能执行其它命令. SpringShell 中可借助于自定义命令是否可用方法的方式来实现命令依赖, 对此, SpringShell提供了三种方式来限制命令是否可用. 但无论哪种方式都需要提供一个判断命令是否可用的方法.

1. 校验是否可用方法

  • 方法返回值类型为 Availability, 当不可用时需要设置提示信息
  • 方法为无参方法
public Availability $methodName(){
    String message = "Sorry! You are not connected";
    return $boolen ? Availability.available() : Availability.unavailable(message);
}

2. 三种方式

2.1 默认方法名方式

方法名必须为 “命令名Availability”, 一个方法只能限制一条命令


@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 注解修饰命令

声明命令时, 使用@ShellMethodAvailability 注解指定该命令是否可用的校验方法, 可指定多个校验方法

@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 定义校验方法时, 指定约束的方法

定义校验方法时, 声明该校验方法约束的命令, 可声明多个命令.

@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. 测试

3.1 查看命令列表

不可用命令, 会添加*号修饰

shell:>help
AVAILABLE COMMANDS

Availability Commands
        connect: Connect to the server.
      * download: Download the nuclear codes.

3.2 执行不可用命令

执行不可用命令时, 会弹出提示信息. beacause 后就是自定义的错误提示信息

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 正确执行方式

# 先执行connect 命令
shell:>connect root 123456

# 执行connect命令之后, download命令可执行
shell:>download
execute downloading ...

# 再次查看可用命令, 会发现download前已无*号
shell:>help
AVAILABLE COMMANDS

Availability Commands
        connect: Connect to the server.
        download: Download the nuclear codes.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值