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 | * Implements a command to execute batchs of {@link Command}s, defined by batch
|
---|
17 | * file, to allow scripted executions.
|
---|
18 | * </p>
|
---|
19 | *
|
---|
20 | * @author Steffen Herbold
|
---|
21 | */
|
---|
22 | public class CMDexec implements Command {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * <p>
|
---|
26 | * Executes a batch of {@link Command}s that are listed line by line in the
|
---|
27 | * given file.
|
---|
28 | * </p>
|
---|
29 | * <p>
|
---|
30 | * Usage: <code>exec filename</code>
|
---|
31 | * </p>
|
---|
32 | *
|
---|
33 | * @see de.ugoe.cs.util.console.commands.Command#run(java.util.List)
|
---|
34 | */
|
---|
35 | public void run(List<Object> parameters) {
|
---|
36 | String script;
|
---|
37 | try {
|
---|
38 | script = (String) parameters.get(0);
|
---|
39 | } catch (Exception e) {
|
---|
40 | throw new InvalidParameterException();
|
---|
41 | }
|
---|
42 | try {
|
---|
43 | String[] commands;
|
---|
44 | File f = new File(script);
|
---|
45 | FileReader reader = new FileReader(f);
|
---|
46 | char[] buffer = new char[(int) f.length()];
|
---|
47 | reader.read(buffer);
|
---|
48 | commands = (new String(buffer)).split("\n");
|
---|
49 | for (String command : commands) {
|
---|
50 | Console.println(command);
|
---|
51 | CommandExecuter.getInstance().exec(command);
|
---|
52 | }
|
---|
53 | reader.close();
|
---|
54 | } catch (FileNotFoundException e) {
|
---|
55 | Console.printerrln(e.getMessage());
|
---|
56 | } catch (IOException e) {
|
---|
57 | Console.printerrln(e.getMessage());
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * (non-Javadoc)
|
---|
63 | *
|
---|
64 | * @see databasebuilder.console.commands.Command#help()
|
---|
65 | */
|
---|
66 | @Override
|
---|
67 | public void help() {
|
---|
68 | Console.println("Usage: exec filename");
|
---|
69 | }
|
---|
70 | }
|
---|