source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/defaultcommands/CMDexec.java

Last change on this file was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 2.4 KB
Line 
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
15package de.ugoe.cs.util.console.defaultcommands;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.IOException;
21import java.io.InputStreamReader;
22import java.util.List;
23import java.util.logging.Level;
24
25import de.ugoe.cs.util.console.Command;
26import de.ugoe.cs.util.console.CommandExecuter;
27import 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 */
38public 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}
Note: See TracBrowser for help on using the repository browser.