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