source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/CommandExecuter.java @ 2283

Last change on this file since 2283 was 2283, checked in by pharms, 5 years ago
File size: 16.4 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util.console;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FilenameFilter;
20import java.io.IOException;
21import java.lang.reflect.InvocationTargetException;
22import java.net.URL;
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.Comparator;
26import java.util.Enumeration;
27import java.util.List;
28import java.util.SortedSet;
29import java.util.TreeSet;
30import java.util.jar.JarEntry;
31import java.util.jar.JarInputStream;
32import java.util.logging.Level;
33
34import de.ugoe.cs.util.StringTools;
35
36/**
37 * <p>
38 * Executes commands. The commands have to implement the {@link Command} interface and be in
39 * packages registered using addCommandPackage(). Additionally, default commands are implemented in
40 * the de.ugoe.cs.util.console.defaultcommands package.
41 * </p>
42 * <p>
43 * This class is implemented as a <i>Singleton</i>.
44 * </p>
45 *
46 * @author Steffen Herbold
47 * @version 1.0
48 */
49public class CommandExecuter {
50
51    /**
52     * <p>
53     * Handle of the CommandExecuter instance.
54     * </p>
55     */
56    private final static CommandExecuter theInstance = new CommandExecuter();
57
58    /**
59     * <p>
60     * Prefix of all command classes.
61     * </p>
62     */
63    private static final String cmdPrefix = "CMD";
64
65    /**
66     * <p>
67     * Name of the package for default commands.
68     * </p>
69     */
70    private static final String defaultPackage = "de.ugoe.cs.util.console.defaultcommands";
71
72    /**
73     * <p>
74     * List of packages in which commands may be defined. The exec methods trys to load command from
75     * these packages in the order they have been added.
76     * </p>
77     * <p>
78     * The de.ugoe.cs.util.console.defaultcommands package has always lowest priority, unless it is
79     * specifically added.
80     * </p>
81     */
82    private List<CommandPackage> commandPackageList;
83
84    /**
85     * <p>
86     * the list of available commands (lazy instantiation in the method
87     * {@link #getAvailableCommands()})
88     * <p>
89     */
90    private Command[] availableCommands;
91
92    /**
93     * <p>
94     * Returns the instance of CommandExecuter. If no instances exists yet, a new one is created.
95     * </p>
96     *
97     * @return the instance of CommandExecuter
98     */
99    public static synchronized CommandExecuter getInstance() {
100        return theInstance;
101    }
102
103    /**
104     * <p>
105     * Creates a new CommandExecuter. Private to prevent multiple instances (Singleton).
106     * </p>
107     */
108    private CommandExecuter() {
109        commandPackageList = new ArrayList<CommandPackage>();
110    }
111
112    /**
113     * <p>
114     * Adds a package that will be used by {@link #exec(String)} to load command from.
115     * </p>
116     *
117     * @param pkg
118     *            package where commands are located
119     * @throws IllegalArgumentException
120     *             thrown if the package name is null or empty string
121     */
122    public void addCommandPackage(String pkg, ClassLoader loader) {
123        if ("".equals(pkg) || pkg == null) {
124            throw new IllegalArgumentException("package name must not be null or empty string");
125        }
126        commandPackageList.add(new CommandPackage(pkg, loader));
127        availableCommands = null;
128    }
129
130    /**
131     * <p>
132     * Executes the command defined by string. A command has the following form (mix of EBNF and
133     * natural language):
134     * </p>
135     * <code>
136     * &lt;command&gt; := &lt;commandname&gt;&lt;whitespace&gt;{&lt;parameter&gt;}<br>
137     * &lt;commandname&gt; := String without whitespaces. Has to be a valid Java class name<br>
138     * &lt;parameter&gt; := &lt;string&gt;|&lt;stringarray&gt;<br>
139     * &lt;string&gt; := &lt;stringwithoutwhitespaces&gt;|&lt;stringwithwhitespaces&gt;
140     * &lt;stringwithoutwhitespaces&gt; := a string without whitespaces<br>
141     * &lt;stringwithoutwhitespaces&gt; := a string, that can have whitespaces, but must be in double quotes<br>
142     * &lt;stringarray&gt; := "["&lt;string&gt;{&lt;whitespace&gt;&lt;string&gt;"]"
143     * </code>
144     *
145     * @param command
146     *            the command as a string
147     */
148    public void exec(String command) {
149        Console.commandNotification(command);
150        CommandParser parser = new CommandParser();
151        parser.parse(command);
152
153        Command cmd = getCMD(parser.getCommandName());
154
155        if (cmd == null) {
156            Console.println("Unknown command");
157        }
158        else {
159            try {
160                cmd.run(parser.getParameters());
161            }
162            catch (IllegalArgumentException e) {
163                Console.println("invalid parameter provided: " + e.getMessage());
164                Console.println("Usage: " + cmd.help());
165            }
166            catch (Exception e) {
167                Console.println("error executing command: " + e);
168                Console.logException(e);
169                Console.println("Usage: " + cmd.help());
170            }
171        }
172    }
173
174    /**
175     * <p>
176     * Helper method that loads a class and tries to cast it to {@link Command}.
177     * </p>
178     *
179     * @param className
180     *            qualified name of the class (including package name)
181     * @return if class is available and implement {@link Command} and instance of the class, null
182     *         otherwise
183     */
184    public Command getCMD(String commandName) {
185        for (Command candidate : getAvailableCommands()) {
186            if (candidate.getClass().getSimpleName().equals(cmdPrefix + commandName)) {
187                return candidate;
188            }
189        }
190
191        return null;
192    }
193
194    /**
195     * <p>
196     * reads all available commands from the registered command packages and returns a list of their
197     * names
198     * </p>
199     *
200     * @return an array containing the names of the available commands.
201     */
202    public Command[] getAvailableCommands() {
203        if (availableCommands == null) {
204            // List<Command> commands = new ArrayList<Command>();
205            List<CommandPackage> packages = new ArrayList<CommandPackage>();
206            packages.addAll(commandPackageList);
207            packages.add(new CommandPackage(defaultPackage, this.getClass().getClassLoader()));
208
209            FilenameFilter filter = new FilenameFilter() {
210                @Override
211                public boolean accept(File dir, String name) {
212                    return (name != null) && (name.startsWith(cmdPrefix)) &&
213                        (name.endsWith(".class"));
214                }
215            };
216
217            SortedSet<Command> commands = new TreeSet<Command>(new Comparator<Command>() {
218                @Override
219                public int compare(Command arg1, Command arg2) {
220                    String str1 = arg1.getClass().getSimpleName().substring(cmdPrefix.length());
221                    String str2 = arg2.getClass().getSimpleName().substring(cmdPrefix.length());
222                    return str1.compareTo(str2);
223                }
224
225            });
226
227            for (CommandPackage commandPackage : packages) {
228                String path = commandPackage.getPackageName().replace('.', '/');
229                try {
230                    ClassLoader loader = commandPackage.getClassLoader();
231
232                    if (loader == null) {
233                        loader = ClassLoader.getSystemClassLoader();
234                    }
235
236                    Enumeration<URL> resources = loader.getResources(path);
237
238                    while (resources.hasMoreElements()) {
239                        URL resource = resources.nextElement();
240                        File packageDir = new File(resource.getFile());
241
242                        if (packageDir.isDirectory()) {
243                            File[] classFiles = packageDir.listFiles(filter);
244                            if (classFiles != null) {
245                                for (File classFile : classFiles) {
246                                    String className = classFile.getName()
247                                        .substring(0, classFile.getName().lastIndexOf('.'));
248                                   
249                                    Class<?> clazz =
250                                        loader.loadClass(commandPackage.getPackageName() + "." +
251                                            className);
252                                   
253                                    if (Command.class.isAssignableFrom(clazz) &&
254                                        clazz.getSimpleName().startsWith(cmdPrefix))
255                                    {
256                                        commands.add((Command) clazz.getConstructor().newInstance());
257                                    }
258                                }
259                            }
260                        }
261                        else {
262                            int index = resource.getFile().lastIndexOf('!');
263                            if ((index > 0) && (resource.getFile().startsWith("file:")) &&
264                                (resource.getFile().endsWith("!/" + path)))
265                            {
266                                String jarFile =
267                                    resource.getFile().substring("file:".length(), index);
268
269                                // we have to read the package content from a jar file
270                                JarInputStream jarInputStream = null;
271                                try {
272                                    jarInputStream =
273                                        new JarInputStream(new FileInputStream(jarFile));
274                                    JarEntry entry = null;
275                                    do {
276                                        entry = jarInputStream.getNextJarEntry();
277                                        if ((entry != null) && (!entry.isDirectory()) &&
278                                            (entry.getName().startsWith(path)))
279                                        {
280                                            String className = entry.getName()
281                                                .substring(path.length() + 1,
282                                                           entry.getName().lastIndexOf('.'));
283                                           
284                                            Class<?> clazz =
285                                                loader.loadClass(commandPackage.getPackageName() +
286                                                    "." + className);
287                                           
288                                            if (Command.class.isAssignableFrom(clazz) &&
289                                                clazz.getSimpleName().startsWith(cmdPrefix))
290                                            {
291                                                commands.add((Command) clazz.getConstructor().newInstance());
292                                            }
293                                        }
294                                    }
295                                    while (entry != null);
296                                }
297                                finally {
298                                    if (jarInputStream != null) {
299                                        jarInputStream.close();
300                                    }
301                                }
302
303                            }
304                        }
305                    }
306                }
307                catch (IOException e) {
308                    Console.traceln(Level.WARNING, "could not read commands of package " +
309                        commandPackage.getPackageName());
310                }
311                catch (ClassNotFoundException e) {
312                    Console.traceln(Level.WARNING, "could not load a command of package " +
313                        commandPackage.getPackageName() + ": " + e);
314                }
315                catch (InstantiationException e) {
316                    Console.traceln(Level.WARNING, "could not load a command of package " +
317                        commandPackage.getPackageName() + ": " + e);
318                }
319                catch (IllegalAccessException e) {
320                    Console.traceln(Level.WARNING, "could not load a command of package " +
321                        commandPackage.getPackageName() + ": " + e);
322                }
323                catch (IllegalArgumentException e) {
324                    Console.traceln(Level.WARNING, "could not load a command of package " +
325                        commandPackage.getPackageName() + ": " + e);
326                }
327                catch (InvocationTargetException e) {
328                    Console.traceln(Level.WARNING, "could not load a command of package " +
329                        commandPackage.getPackageName() + ": " + e);
330                }
331                catch (NoSuchMethodException e) {
332                    Console.traceln(Level.WARNING, "could not load a command of package " +
333                        commandPackage.getPackageName() + ": " + e);
334                }
335                catch (SecurityException e) {
336                    Console.traceln(Level.WARNING, "could not load a command of package " +
337                        commandPackage.getPackageName() + ": " + e);
338                }
339            }
340
341            availableCommands = commands.toArray(new Command[commands.size()]);
342        }
343
344        return Arrays.copyOf(availableCommands, availableCommands.length);
345    }
346
347    /**
348     * <p>
349     * Get a copy of the currently registered command packages.
350     * </p>
351     *
352     * @return currently registered command packages
353     */
354    public List<String> getCommandPackages() {
355        List<String> commandPackageListCopy = new ArrayList<>();
356
357        commandPackageListCopy.add(defaultPackage);
358
359        for (CommandPackage pkg : commandPackageList) {
360            commandPackageListCopy.add(pkg.getPackageName());
361        }
362
363        return commandPackageListCopy;
364    }
365
366    /**
367     * <p>
368     * this method method performs an auto completion of the provided String as far as possible
369     * regarding the available commands. It auto completes to the full command name, if only one
370     * command matches the given prefix. It auto completes to the common denominator, if several
371     * commands match the prefix
372     * </p>
373     *
374     * @param commandPrefix
375     *            the prefix to be auto completed
376     *
377     * @return as described
378     */
379    public String autoCompleteCommand(String commandPrefix) {
380        Command[] commands = getAvailableCommands();
381
382        String[] completions = new String[commands.length];
383
384        for (int i = 0; i < commands.length; i++) {
385            completions[i] = commands[i].getClass().getSimpleName().substring(3);
386        }
387
388        return StringTools.autocomplete(commandPrefix, completions);
389    }
390
391    /**
392     * represents a command package with a package name and the class loader to use
393     */
394    private class CommandPackage {
395        /**
396         * the name of the represented package
397         */
398        private String packageName;
399
400        /**
401         * the class loader to use to load the package
402         */
403        private ClassLoader classLoader;
404
405        /**
406         * <p>
407         * instantiate the fields
408         * </p>
409         */
410        public CommandPackage(String packageName, ClassLoader classLoader) {
411            super();
412            this.packageName = packageName;
413            this.classLoader = classLoader;
414        }
415
416        /**
417         * @return the packageName
418         */
419        public String getPackageName() {
420            return packageName;
421        }
422
423        /**
424         * @return the classLoader
425         */
426        public ClassLoader getClassLoader() {
427            return classLoader;
428        }
429
430    }
431}
Note: See TracBrowser for help on using the repository browser.