source: trunk/JavaHelperLib/src/de/ugoe/cs/util/console/defaultcommands/CMDexec.java @ 159

Last change on this file since 159 was 122, checked in by sherbold, 13 years ago

fixed output bug in command exec

File size: 1.7 KB
Line 
1package de.ugoe.cs.util.console.defaultcommands;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.security.InvalidParameterException;
8import java.util.List;
9
10import de.ugoe.cs.util.console.Command;
11import de.ugoe.cs.util.console.CommandExecuter;
12import de.ugoe.cs.util.console.Console;
13
14/**
15 * <p>
16 * Implements a command to execute batchs of {@link Command}s, defined by batch
17 * file, to allow scripted executions.
18 * </p>
19 *
20 * @author Steffen Herbold
21 */
22public class CMDexec implements Command {
23
24        /**
25         * <p>
26         * Executes a batch of {@link Command}s that are listed line by line in the
27         * given file.
28         * </p>
29         * <p>
30         * Usage: <code>exec filename</code>
31         * </p>
32         *
33         * @see de.ugoe.cs.util.console.commands.Command#run(java.util.List)
34         */
35        public void run(List<Object> parameters) {
36                String script;
37                try {
38                        script = (String) parameters.get(0);
39                } catch (Exception e) {
40                        throw new InvalidParameterException();
41                }
42                try {
43                        String[] commands;
44                        File f = new File(script);
45                        FileReader reader = new FileReader(f);
46                        char[] buffer = new char[(int) f.length()];
47                        reader.read(buffer);
48                        commands = (new String(buffer)).split("\n");
49                        for (String command : commands) {
50                                Console.println(command.trim());
51                                CommandExecuter.getInstance().exec(command);
52                        }
53                        reader.close();
54                } catch (FileNotFoundException e) {
55                        Console.printerrln(e.getMessage());
56                } catch (IOException e) {
57                        Console.printerrln(e.getMessage());
58                }
59        }
60
61        /*
62         * (non-Javadoc)
63         *
64         * @see databasebuilder.console.commands.Command#help()
65         */
66        @Override
67        public void help() {
68                Console.println("Usage: exec filename");
69        }
70}
Note: See TracBrowser for help on using the repository browser.