[1] | 1 | package de.ugoe.cs.util.console.defaultcommands;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
[310] | 4 | import java.io.FileInputStream;
|
---|
[1] | 5 | import java.io.FileNotFoundException;
|
---|
| 6 | import java.io.IOException;
|
---|
[310] | 7 | import java.io.InputStreamReader;
|
---|
[1] | 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>
|
---|
[175] | 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.
|
---|
[1] | 19 | * </p>
|
---|
| 20 | *
|
---|
| 21 | * @author Steffen Herbold
|
---|
[175] | 22 | * @version 1.0
|
---|
[1] | 23 | */
|
---|
| 24 | public class CMDexec implements Command {
|
---|
| 25 |
|
---|
[175] | 26 | /*
|
---|
| 27 | * (non-Javadoc)
|
---|
[1] | 28 | *
|
---|
[175] | 29 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
[1] | 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);
|
---|
[310] | 41 | FileInputStream fis = new FileInputStream(f);
|
---|
| 42 | InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
|
---|
[1] | 43 | char[] buffer = new char[(int) f.length()];
|
---|
| 44 | reader.read(buffer);
|
---|
| 45 | commands = (new String(buffer)).split("\n");
|
---|
| 46 | for (String command : commands) {
|
---|
[244] | 47 | Console.traceln(command.trim());
|
---|
[1] | 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 | }
|
---|