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

Last change on this file since 310 was 310, checked in by sherbold, 13 years ago
  • now specifies character encoding of file explicitly instead of assuming default encoding
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.FileReader;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.security.InvalidParameterException;
10import java.util.List;
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(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 void help() {
66                Console.println("Usage: exec filename");
67        }
68}
Note: See TracBrowser for help on using the repository browser.