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.IOException;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.security.InvalidParameterException;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import de.ugoe.cs.util.console.Command;
|
---|
12 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
13 | import de.ugoe.cs.util.console.Console;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * <p>
|
---|
17 | * Command to execute a batch of {@link Command}s. The batch is defined as a
|
---|
18 | * text file, where each line defines one command.
|
---|
19 | * </p>
|
---|
20 | *
|
---|
21 | * @author Steffen Herbold
|
---|
22 | * @version 1.0
|
---|
23 | */
|
---|
24 | public class CMDexec implements Command {
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * (non-Javadoc)
|
---|
28 | *
|
---|
29 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
30 | */
|
---|
31 | public void run(List<Object> parameters) {
|
---|
32 | String script;
|
---|
33 | try {
|
---|
34 | script = (String) parameters.get(0);
|
---|
35 | } catch (Exception e) {
|
---|
36 | throw new InvalidParameterException();
|
---|
37 | }
|
---|
38 | try {
|
---|
39 | String[] commands;
|
---|
40 | File f = new File(script);
|
---|
41 | FileInputStream fis = new FileInputStream(f);
|
---|
42 | InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
|
---|
43 | char[] buffer = new char[(int) f.length()];
|
---|
44 | reader.read(buffer);
|
---|
45 | commands = (new String(buffer)).split("\n");
|
---|
46 | for (String command : commands) {
|
---|
47 | Console.traceln(command.trim());
|
---|
48 | CommandExecuter.getInstance().exec(command);
|
---|
49 | }
|
---|
50 | reader.close();
|
---|
51 | } catch (FileNotFoundException e) {
|
---|
52 | Console.printerrln(e.getMessage());
|
---|
53 | } catch (IOException e) {
|
---|
54 | Console.printerrln(e.getMessage());
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * (non-Javadoc)
|
---|
60 | *
|
---|
61 | * @see databasebuilder.console.commands.Command#help()
|
---|
62 | */
|
---|
63 | @Override
|
---|
64 | public void help() {
|
---|
65 | Console.println("Usage: exec <filename>");
|
---|
66 | }
|
---|
67 | }
|
---|