1 | package de.ugoe.cs.util.console.defaultcommands;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.FileReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.security.InvalidParameterException;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import de.ugoe.cs.util.console.Command;
|
---|
13 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
14 | import 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 | */
|
---|
25 | public 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 | }
|
---|