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;
|
---|
16 |
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.nio.charset.Charset;
|
---|
19 | import java.util.logging.Level;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.util.console.listener.IErrorListener;
|
---|
22 | import de.ugoe.cs.util.console.listener.IExceptionListener;
|
---|
23 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
24 | import de.ugoe.cs.util.console.listener.ITraceListener;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * <p>
|
---|
28 | * Implements a simple console observer that prints normal text to
|
---|
29 | * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}.
|
---|
30 | * </p>
|
---|
31 | *
|
---|
32 | * @author Steffen Herbold
|
---|
33 | * @version 1.0
|
---|
34 | */
|
---|
35 | public class TextConsole implements IOutputListener, IErrorListener,
|
---|
36 | ITraceListener, IExceptionListener {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>Defines the trace level used by this console.</p>
|
---|
40 | */
|
---|
41 | private Level traceLevel;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * <p>
|
---|
45 | * Creates a new text console and automatically registers it as observer. The trace level is {@link Level#WARNING}.
|
---|
46 | * </p>
|
---|
47 | */
|
---|
48 | public TextConsole() {
|
---|
49 | this(Level.WARNING);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * <p>
|
---|
54 | * Creates a new text console and automatically registers it as observer.
|
---|
55 | * </p>
|
---|
56 | * @param traceLevel trace level used by this text console
|
---|
57 | */
|
---|
58 | public TextConsole(Level traceLevel) {
|
---|
59 | Console.getInstance().registerOutputListener(this);
|
---|
60 | Console.getInstance().registerErrorListener(this);
|
---|
61 | Console.getInstance().registerTraceListener(this);
|
---|
62 | Console.getInstance().registerExceptionListener(this);
|
---|
63 | this.traceLevel = traceLevel;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * <p>
|
---|
68 | * Prints messages to {@code stdout}.
|
---|
69 | * </p>
|
---|
70 | *
|
---|
71 | * @see ConsoleObserver#outputMsg(java.lang.String)
|
---|
72 | */
|
---|
73 | public void outputMsg(String newMessage) {
|
---|
74 | System.out.print(newMessage);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * <p>
|
---|
79 | * Prints messages to {@code stderr}.
|
---|
80 | * </p>
|
---|
81 | *
|
---|
82 | * @see ConsoleObserver#errorMsg(String)
|
---|
83 | */
|
---|
84 | @Override
|
---|
85 | public void errorMsg(String errMessage) {
|
---|
86 | System.err.print(errMessage);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * <p>
|
---|
91 | * Prints the stacktrace of an exception to {@code stderr}.
|
---|
92 | * </p>
|
---|
93 | *
|
---|
94 | * @see ConsoleObserver#logException(Exception)
|
---|
95 | */
|
---|
96 | @Override
|
---|
97 | public void logException(Exception e) {
|
---|
98 | System.err.println(e.getMessage());
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * <p>
|
---|
103 | * Prints messages to {@code stdout}. These messages are only printed, if
|
---|
104 | * the console is run in debug mode.
|
---|
105 | * </p>
|
---|
106 | */
|
---|
107 | @Override
|
---|
108 | public void traceMsg(String traceMessage, Level level) {
|
---|
109 | if (level.intValue()>=traceLevel.intValue()) {
|
---|
110 | System.out.print("[" + level.toString() + "] " + traceMessage);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * <p>
|
---|
116 | * Starts a new TextConsole. If the text console is started, it can be used
|
---|
117 | * not only to print message, but also to execute commands by reading
|
---|
118 | * {@code stdin}.
|
---|
119 | * </p>
|
---|
120 | */
|
---|
121 | public void run() {
|
---|
122 | CommandExecuter exec = CommandExecuter.getInstance();
|
---|
123 | while (true) {
|
---|
124 | System.out.print("> ");
|
---|
125 | String command = getCommand().trim();
|
---|
126 | if (!command.equals("")) {
|
---|
127 | exec.exec(command);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * <p>
|
---|
134 | * Reads a new command from {@code stdin}.
|
---|
135 | * </p>
|
---|
136 | *
|
---|
137 | * @return a string with a command
|
---|
138 | */
|
---|
139 | protected String getCommand() {
|
---|
140 | byte[] buffer = new byte[1024];
|
---|
141 | int bytesRead = 0;
|
---|
142 | String command;
|
---|
143 | try {
|
---|
144 | bytesRead = System.in.read(buffer);
|
---|
145 | } catch (IOException e) {
|
---|
146 |
|
---|
147 | }
|
---|
148 | if (bytesRead == 0) {
|
---|
149 | command = "";
|
---|
150 | } else {
|
---|
151 | command = new String(buffer, Charset.defaultCharset());
|
---|
152 | }
|
---|
153 | return command;
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|