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

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