Invoker.java
上传用户:hensond
上传日期:2021-12-27
资源大小:817k
文件大小:1k
- package com.company;
- import com.company.command.Command;
- import com.company.command.LSCommand;
- /**
- * @author cbf4Life cbf4life@126.com
- * I'm glad to share my knowledge with you all.
- * 调用类
- */
- public class Invoker {
-
- //执行命令
- public String exec(String _commandStr){
- //定义返回值
- String result = "";
- //首先解析命令
- CommandVO vo = new CommandVO(_commandStr);
- //检查是否支持支持该命令
- if(CommandEnum.getNames().contains(vo.getCommandName())){
- //产生命令对象
- String className = CommandEnum.valueOf(vo.getCommandName()).getValue();
- Command command;
- try {
- command = (Command)Class.forName(className).newInstance();
- result = command.execute(vo);
- }catch(Exception e){
- // TODO 异常处理
- }
- }else{
- result = "无法执行命令,请检查命令格式";
- }
- return result;
- }
-
-
- public static void main(String[] args) {
- String cmd = "ls -a";
- Invoker invoker = new Invoker();
- System.out.println(invoker.exec(cmd));
- }
- }