[1] | 1 | package de.ugoe.cs.util.console;
|
---|
| 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * <p>
|
---|
| 9 | * Executes commands. The commands have to implement the {@link Command}
|
---|
| 10 | * interface and be in packages registered using addCommandPackage().
|
---|
| 11 | * Additionally, default commands are implemented in the
|
---|
| 12 | * de.ugoe.cs.util.console.defaultcommands package.
|
---|
| 13 | * </p>
|
---|
| 14 | * <p>
|
---|
[175] | 15 | * This class is implemented as a <i>Singleton</i>.
|
---|
[1] | 16 | * </p>
|
---|
| 17 | *
|
---|
| 18 | * @author Steffen Herbold
|
---|
[175] | 19 | * @version 1.0
|
---|
[1] | 20 | */
|
---|
| 21 | public class CommandExecuter {
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * <p>
|
---|
| 25 | * Handle of the CommandExecuter instance.
|
---|
| 26 | * </p>
|
---|
| 27 | */
|
---|
| 28 | private final static CommandExecuter theInstance = new CommandExecuter();
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * <p>
|
---|
| 32 | * Prefix of all command classes.
|
---|
| 33 | * </p>
|
---|
| 34 | */
|
---|
| 35 | private static final String cmdPrefix = "CMD";
|
---|
[175] | 36 |
|
---|
[1] | 37 | /**
|
---|
| 38 | * <p>
|
---|
| 39 | * Name of the package for default commands.
|
---|
| 40 | * </p>
|
---|
| 41 | */
|
---|
| 42 | private static final String defaultPackage = "de.ugoe.cs.util.console.defaultcommands";
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * <p>
|
---|
| 46 | * List of packages in which commands may be defined. The exec methods trys
|
---|
| 47 | * to load command from these packages in the order they have been added.
|
---|
| 48 | * </p>
|
---|
| 49 | * <p>
|
---|
[175] | 50 | * The de.ugoe.cs.util.console.defaultcommands package has always lowest
|
---|
| 51 | * priority, unless it is specifically added.
|
---|
[1] | 52 | * </p>
|
---|
| 53 | */
|
---|
| 54 | private List<String> commandPackageList;
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * <p>
|
---|
| 58 | * Returns the instance of CommandExecuter. If no instances exists yet, a
|
---|
| 59 | * new one is created.
|
---|
| 60 | * </p>
|
---|
| 61 | *
|
---|
| 62 | * @return the instance of CommandExecuter
|
---|
| 63 | */
|
---|
| 64 | public static synchronized CommandExecuter getInstance() {
|
---|
| 65 | return theInstance;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * <p>
|
---|
| 70 | * Creates a new CommandExecuter. Private to prevent multiple instances
|
---|
| 71 | * (Singleton).
|
---|
| 72 | * </p>
|
---|
| 73 | */
|
---|
| 74 | private CommandExecuter() {
|
---|
| 75 | commandPackageList = new ArrayList<String>();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * <p>
|
---|
[175] | 80 | * Adds a package that will be used by {@link #exec(String)} to load command
|
---|
| 81 | * from.
|
---|
[1] | 82 | * </p>
|
---|
| 83 | *
|
---|
| 84 | * @param pkg
|
---|
| 85 | * package where commands are located
|
---|
[320] | 86 | * @throws InvalidParameterException
|
---|
| 87 | * thrown if the package name is null or empty string
|
---|
[1] | 88 | */
|
---|
| 89 | public void addCommandPackage(String pkg) {
|
---|
[320] | 90 | if ("".equals(pkg) || pkg == null) {
|
---|
| 91 | throw new InvalidParameterException(
|
---|
| 92 | "package name must not be null or empty string");
|
---|
| 93 | }
|
---|
[1] | 94 | commandPackageList.add(pkg);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * <p>
|
---|
| 99 | * Executes the command defined by string. A command has the following form
|
---|
| 100 | * (mix of EBNF and natural language):
|
---|
| 101 | * </p>
|
---|
| 102 | * <code>
|
---|
| 103 | * <command> := <commandname><whitespace>{<parameter>}<br>
|
---|
| 104 | * <commandname> := String without whitespaces. Has to be a valid Java class name<br>
|
---|
| 105 | * <parameter> := <string>|<stringarray><br>
|
---|
| 106 | * <string> := <stringwithoutwhitespaces>|<stringwithwhitespaces>
|
---|
| 107 | * <stringwithoutwhitespaces> := a string without whitespaces<br>
|
---|
| 108 | * <stringwithoutwhitespaces> := a string, that can have whitespaces, but must be in double quotes<br>
|
---|
| 109 | * <stringarray> := "["<string>{<whitespace><string>"]"
|
---|
| 110 | * </code>
|
---|
| 111 | *
|
---|
| 112 | * @param command
|
---|
| 113 | * the command as a string
|
---|
| 114 | */
|
---|
| 115 | public void exec(String command) {
|
---|
[186] | 116 | Console.commandNotification(command);
|
---|
[1] | 117 | Command cmd = null;
|
---|
| 118 | CommandParser parser = new CommandParser();
|
---|
| 119 | parser.parse(command);
|
---|
| 120 | for (int i = 0; cmd == null && i < commandPackageList.size(); i++) {
|
---|
[175] | 121 | cmd = loadCMD(commandPackageList.get(i) + "." + cmdPrefix
|
---|
| 122 | + parser.getCommandName());
|
---|
[1] | 123 | }
|
---|
| 124 | if (cmd == null) { // check if command is available as default command
|
---|
[175] | 125 | cmd = loadCMD(defaultPackage + "." + cmdPrefix
|
---|
| 126 | + parser.getCommandName());
|
---|
[1] | 127 | }
|
---|
| 128 | if (cmd == null) {
|
---|
| 129 | Console.println("Unknown command");
|
---|
| 130 | } else {
|
---|
| 131 | try {
|
---|
| 132 | cmd.run(parser.getParameters());
|
---|
| 133 | } catch (InvalidParameterException e) {
|
---|
| 134 | cmd.help();
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
[175] | 140 | * <p>
|
---|
[1] | 141 | * Helper method that loads a class and tries to cast it to {@link Command}.
|
---|
[175] | 142 | * </p>
|
---|
[1] | 143 | *
|
---|
[175] | 144 | * @param className
|
---|
| 145 | * qualified name of the class (including package name)
|
---|
| 146 | * @return if class is available and implement {@link Command} and instance
|
---|
| 147 | * of the class, null otherwise
|
---|
[1] | 148 | */
|
---|
| 149 | private Command loadCMD(String className) {
|
---|
| 150 | Command cmd = null;
|
---|
| 151 | try {
|
---|
| 152 | Class<?> cmdClass = Class.forName(className);
|
---|
| 153 | cmd = (Command) cmdClass.newInstance();
|
---|
| 154 | } catch (NoClassDefFoundError e) {
|
---|
| 155 | String[] splitResult = e.getMessage().split("CMD");
|
---|
[175] | 156 | String correctName = splitResult[splitResult.length - 1].replace(
|
---|
| 157 | ")", "");
|
---|
[244] | 158 | Console.println("Did you mean " + correctName + "?");
|
---|
[1] | 159 | } catch (ClassNotFoundException e) {
|
---|
| 160 | } catch (IllegalAccessException e) {
|
---|
| 161 | } catch (InstantiationException e) {
|
---|
| 162 | } catch (ClassCastException e) {
|
---|
| 163 | Console.traceln(className + "found, but does not implement Command");
|
---|
| 164 | }
|
---|
| 165 | return cmd;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|