source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/defaultcommands/CMDexec.java @ 664

Last change on this file since 664 was 664, checked in by sherbold, 12 years ago
  • modified Command.help(): the help now returns a string with its usage instead of writing directly to the console
  • modified listCommands: now writes the help string of each command instead of just the name; this way, the parameters are displayed directly
  • moved listCommands from quest-ui-core to java-utils
File size: 1.8 KB
Line 
1package de.ugoe.cs.util.console.defaultcommands;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStreamReader;
8import java.security.InvalidParameterException;
9import java.util.List;
10import java.util.logging.Level;
11
12import de.ugoe.cs.util.console.Command;
13import de.ugoe.cs.util.console.CommandExecuter;
14import de.ugoe.cs.util.console.Console;
15
16/**
17 * <p>
18 * Command to execute a batch of {@link Command}s. The batch is defined as a
19 * text file, where each line defines one command.
20 * </p>
21 *
22 * @author Steffen Herbold
23 * @version 1.0
24 */
25public class CMDexec implements Command {
26
27        /*
28         * (non-Javadoc)
29         *
30         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
31         */
32        public void run(List<Object> parameters) {
33                String script;
34                try {
35                        script = (String) parameters.get(0);
36                } catch (Exception e) {
37                        throw new InvalidParameterException();
38                }
39                try {
40                        String[] commands;
41                        File f = new File(script);
42                        FileInputStream fis = new FileInputStream(f);
43                        InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
44                        char[] buffer = new char[(int) f.length()];
45                        reader.read(buffer);
46                        commands = (new String(buffer)).split("\n");
47                        for (String command : commands) {
48                                Console.traceln(Level.INFO, "Executing \"" + command.trim() + "\"");
49                                CommandExecuter.getInstance().exec(command);
50                        }
51                        reader.close();
52                } catch (FileNotFoundException e) {
53                        Console.printerrln(e.getMessage());
54                } catch (IOException e) {
55                        Console.printerrln(e.getMessage());
56                }
57        }
58
59        /*
60         * (non-Javadoc)
61         *
62         * @see databasebuilder.console.commands.Command#help()
63         */
64        @Override
65        public String help() {
66                return "exec <filename>";
67        }
68}
Note: See TracBrowser for help on using the repository browser.