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

Last change on this file since 819 was 819, checked in by sherbold, 12 years ago
  • upgraded command exec: lines that have a % as first non-whitespace character are now ignored
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.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                            if( !command.trim().startsWith("%")) {
48                                Console.traceln(Level.INFO, "Executing \"" + command.trim() + "\"");
49                                CommandExecuter.getInstance().exec(command);
50                            }
51                        }
52                        reader.close();
53                } catch (FileNotFoundException e) {
54                        Console.printerrln(e.getMessage());
55                } catch (IOException e) {
56                        Console.printerrln(e.getMessage());
57                }
58        }
59
60        /*
61         * (non-Javadoc)
62         *
63         * @see databasebuilder.console.commands.Command#help()
64         */
65        @Override
66        public String help() {
67                return "exec <filename>";
68        }
69}
Note: See TracBrowser for help on using the repository browser.