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