| 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
|---|
| 2 | //
|
|---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 4 | // you may not use this file except in compliance with the License.
|
|---|
| 5 | // You may obtain a copy of the License at
|
|---|
| 6 | //
|
|---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 8 | //
|
|---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
|---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 12 | // See the License for the specific language governing permissions and
|
|---|
| 13 | // limitations under the License.
|
|---|
| 14 |
|
|---|
| 15 | package de.ugoe.cs.util.console.defaultcommands;
|
|---|
| 16 |
|
|---|
| 17 | import java.io.File;
|
|---|
| 18 | import java.io.FileInputStream;
|
|---|
| 19 | import java.io.FileNotFoundException;
|
|---|
| 20 | import java.io.IOException;
|
|---|
| 21 | import java.io.InputStreamReader;
|
|---|
| 22 | import java.util.List;
|
|---|
| 23 | import java.util.logging.Level;
|
|---|
| 24 |
|
|---|
| 25 | import de.ugoe.cs.util.console.Command;
|
|---|
| 26 | import de.ugoe.cs.util.console.CommandExecuter;
|
|---|
| 27 | import de.ugoe.cs.util.console.Console;
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * <p>
|
|---|
| 31 | * Command to execute a batch of {@link Command}s. The batch is defined as a
|
|---|
| 32 | * text file, where each line defines one command.
|
|---|
| 33 | * </p>
|
|---|
| 34 | *
|
|---|
| 35 | * @author Steffen Herbold
|
|---|
| 36 | * @version 1.0
|
|---|
| 37 | */
|
|---|
| 38 | public class CMDexec implements Command {
|
|---|
| 39 |
|
|---|
| 40 | /*
|
|---|
| 41 | * (non-Javadoc)
|
|---|
| 42 | *
|
|---|
| 43 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
|---|
| 44 | */
|
|---|
| 45 | public void run(List<Object> parameters) {
|
|---|
| 46 | String script;
|
|---|
| 47 | try {
|
|---|
| 48 | script = (String) parameters.get(0);
|
|---|
| 49 | } catch (Exception e) {
|
|---|
| 50 | throw new IllegalArgumentException();
|
|---|
| 51 | }
|
|---|
| 52 | try {
|
|---|
| 53 | String[] commands;
|
|---|
| 54 | File f = new File(script);
|
|---|
| 55 | FileInputStream fis = new FileInputStream(f);
|
|---|
| 56 | InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
|
|---|
| 57 | char[] buffer = new char[(int) f.length()];
|
|---|
| 58 | reader.read(buffer);
|
|---|
| 59 | commands = (new String(buffer)).split("\n");
|
|---|
| 60 | for (String command : commands) {
|
|---|
| 61 | if( !command.trim().startsWith("%")) {
|
|---|
| 62 | Console.traceln(Level.INFO, "Executing \"" + command.trim() + "\"");
|
|---|
| 63 | CommandExecuter.getInstance().exec(command);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | reader.close();
|
|---|
| 67 | } catch (FileNotFoundException e) {
|
|---|
| 68 | Console.printerrln(e.getMessage());
|
|---|
| 69 | } catch (IOException e) {
|
|---|
| 70 | Console.printerrln(e.getMessage());
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /*
|
|---|
| 75 | * (non-Javadoc)
|
|---|
| 76 | *
|
|---|
| 77 | * @see databasebuilder.console.commands.Command#help()
|
|---|
| 78 | */
|
|---|
| 79 | @Override
|
|---|
| 80 | public String help() {
|
|---|
| 81 | return "exec <filename>";
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|