use of com.terran4j.commons.jfinger.CommandGroupDefine in project commons by terran4j.
the class CommandLineApplicationListener method onApplicationEvent.
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext app = event.getApplicationContext();
// 从配置上判断用户是否禁用了命令行服务。
String cliDisable = app.getEnvironment().getProperty(PROP_KEY_DISABLE);
if (cliDisable != null && cliDisable.trim().equalsIgnoreCase("true")) {
if (log.isWarnEnabled()) {
log.warn("Command Line Service was disable, will not be started.");
}
return;
}
// 如果没有任何命令被注册,也不需要启用命令行服务。
Map<String, Object> beans = app.getBeansWithAnnotation(CommandGroup.class);
if (beans == null || beans.size() == 0) {
if (log.isWarnEnabled()) {
//
log.warn(//
"No Bean has Annotation: @{}. Command Line Service will not be started.", CommandGroup.class.getSimpleName());
}
return;
}
// 获取提示符。
String cliPrompt = app.getEnvironment().getProperty(PROP_KEY_PROMPT);
if (!StringUtils.isEmpty(cliPrompt)) {
cliPrompt = cliPrompt.trim();
} else {
cliPrompt = PROMPT_DEFAULT;
}
PROMPT = cliPrompt;
// 解析所有的命令行。
CommandGroups groups = new CommandGroups();
Iterator<String> it = beans.keySet().iterator();
while (it.hasNext()) {
String beanName = it.next();
Object bean = app.getBean(beanName);
Class<?> beanClass = bean.getClass();
List<Method> commandMethods = getCommandMethod(beanClass);
if (commandMethods.size() == 0) {
continue;
}
// 解析命令组。
CommandGroup commandGroup = beanClass.getAnnotation(CommandGroup.class);
CommandGroupDefine commandGroupDefine = new CommandGroupDefine(commandGroup);
if (StringUtils.isEmpty(commandGroup.name())) {
String groupName = beanName;
if (groupName.endsWith("Command") && groupName.length() > "Command".length()) {
groupName = groupName.substring(0, groupName.length() - "Command".length());
}
commandGroupDefine.setName(groupName);
}
// 解析通用命令选项。
List<CommandOptionDefine> commonOptions = new ArrayList<CommandOptionDefine>();
CommandOption[] commandGroupOptions = commandGroup.options();
if (commandGroupOptions != null && commandGroupOptions.length > 0) {
for (CommandOption commandOption : commandGroupOptions) {
CommandOptionDefine commonOption = new CommandOptionDefine(commandOption);
commonOptions.add(commonOption);
}
}
// 解析本组命令。
for (Method commandMethod : commandMethods) {
CommandDefine commandDefine = new CommandDefine(commandGroupDefine, bean, commandMethod, beanName);
commandDefine.addOptions(commonOptions);
commandGroupDefine.addCommand(commandDefine);
}
// 记录下本命令组。
groups.addCommandGroup(commandGroupDefine);
}
service = new CommandLineService();
service.setCommands(groups);
service.setPrompt(cliPrompt);
thread = new CommandLineTask(service);
// 线程一直存在,不让主进程退出。
thread.setDaemon(false);
thread.setName("JFinger Demo Thread");
thread.start();
if (log.isInfoEnabled()) {
log.info("Command Line Service is started.");
}
}
use of com.terran4j.commons.jfinger.CommandGroupDefine in project commons by terran4j.
the class CommandLineService method getHelp.
public String getHelp() {
StringBuilder sb = new StringBuilder();
sb.append("欢迎使用由 terran4j 提供的命令行服务。\n\n");
sb.append("当前程序有以下命令可供使用:\n");
Iterator<String> it = commands.keySet().iterator();
while (it.hasNext()) {
String groupName = it.next();
if (StringUtils.isEmpty(groupName)) {
continue;
}
CommandGroupDefine group = commands.get(groupName);
if (group == null || group.size() == 0) {
continue;
}
sb.append("命令: ").append(groupName).append(" [");
List<CommandDefine> commands = group.getCommands();
boolean notFirst = false;
for (int i = 0; i < commands.size(); i++) {
CommandDefine command = commands.get(i);
String name = command.getName();
if (notFirst) {
sb.append(" | ");
} else {
notFirst = true;
}
sb.append(name);
}
sb.append("]\n");
sb.append("说明: ").append(group.getDesc()).append("\n\n");
}
sb.append("\n").append(getHelpPrompt());
return sb.toString();
}
use of com.terran4j.commons.jfinger.CommandGroupDefine in project commons by terran4j.
the class CommandLineService method execute.
public final boolean execute(String command, PrintStream out) {
if (StringUtils.isEmpty(command)) {
return true;
}
if (log.isInfoEnabled()) {
log.info("command: {}", command);
}
command = command.trim();
if ("quit".equals(command)) {
return false;
}
// Print All Commands Help
if (command.startsWith("help ") || command.equals("help")) {
String[] helpArgs = util.split(command, " ");
if (helpArgs.length > 2) {
out.println(getHelp(helpArgs[1], helpArgs[2]));
return true;
}
if (helpArgs.length > 1) {
out.println(getHelp(helpArgs[1]));
return true;
}
if (helpArgs.length > 0) {
out.println(getHelp());
return true;
}
}
try {
String[] args = parseCommand(command);
if (args.length < 2) {
out.println(getHelpPrompt());
out.println("命令格式不正确,至少要输入: [命令组名称] [命令名称] ,请用上面的 help 命令查询其详细用法");
return true;
}
String group = args[0];
CommandGroupDefine commandGroup = commands.get(group);
if (commandGroup == null) {
out.println(getHelpPrompt());
out.println("不存在此命令组: " + group + ",请用上面的 help 命令查询其详细用法。\n");
return true;
}
if (commandGroup.size() == 0) {
out.println("命令组【 " + group + "】中没有任何命令!\n");
return true;
}
String commandName = args[1];
CommandDefine commandConfig = commandGroup.getCommand(commandName);
if (commandConfig == null) {
out.println("在命令组【" + group + "】中,不存在此命令: " + commandName);
out.println(getHelp(group));
return true;
}
String[] commandArgs = Arrays.copyOfRange(args, 2, args.length);
execute(group, commandName, commandConfig, commandArgs, out);
} catch (CommandException ce) {
String msg = ce.getMessage();
out.println(msg);
} catch (Throwable e) {
printHelpPrompt(e, out);
}
return true;
}
use of com.terran4j.commons.jfinger.CommandGroupDefine in project commons by terran4j.
the class CommandLineService method getHelp.
public String getHelp(String groupName) {
if (StringUtils.isEmpty(groupName)) {
return getHelp();
}
groupName = groupName.trim();
CommandGroupDefine group = commands.get(groupName);
if (group == null) {
return groupName + " not found, all command group list:\n" + getHelp();
}
StringBuilder sb = new StringBuilder();
sb.append("命令组: ").append(groupName).append("\n");
sb.append("说明: ").append(group.getDesc()).append("\n");
sb.append("命令列表:\n");
List<CommandDefine> commandList = group.getCommands();
for (int i = 0; i < commandList.size(); i++) {
CommandDefine command = commandList.get(i);
String name = command.getName();
if (StringUtils.isEmpty(name)) {
continue;
}
// 描述中有多行时,每行前加 8 个空格。
String desc = command.getDesc();
desc = desc.replaceAll("\n", "\n ");
sb.append(" ").append(name).append(": ").append(desc).append("\n");
}
return sb.toString();
}
Aggregations