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