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

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