001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.telnet;
017:
018: import java.io.DataInputStream;
019:
020: import java.io.IOException;
021:
022: import java.io.InputStream;
023:
024: import java.io.PrintStream;
025:
026: import java.util.Properties;
027:
028: import org.apache.openejb.util.LogCategory;
029: import org.apache.openejb.util.Logger;
030:
031: public class TextConsole {
032:
033: Logger logger = Logger.getInstance(LogCategory.OPENEJB_ADMIN,
034: "org.apache.openejb.server.util.resources");
035:
036: Properties props;
037:
038: public TextConsole() {
039:
040: }
041:
042: public void init(Properties props) throws Exception {
043:
044: this .props = props;
045:
046: }
047:
048: boolean stop = false;
049:
050: DataInputStream in = null;
051:
052: PrintStream out = null;
053:
054: public static final char ESC = (char) 27;
055:
056: public static final String TTY_Reset = ESC + "[0m";
057:
058: public static final String TTY_Bright = ESC + "[1m";
059:
060: public static final String TTY_Dim = ESC + "[2m";
061:
062: public static final String TTY_Underscore = ESC + "[4m";
063:
064: public static final String TTY_Blink = ESC + "[5m";
065:
066: public static final String TTY_Reverse = ESC + "[7m";
067:
068: public static final String TTY_Hidden = ESC + "[8m";
069:
070: /* Foreground Colors */
071:
072: public static final String TTY_FG_Black = ESC + "[30m";
073:
074: public static final String TTY_FG_Red = ESC + "[31m";
075:
076: public static final String TTY_FG_Green = ESC + "[32m";
077:
078: public static final String TTY_FG_Yellow = ESC + "[33m";
079:
080: public static final String TTY_FG_Blue = ESC + "[34m";
081:
082: public static final String TTY_FG_Magenta = ESC + "[35m";
083:
084: public static final String TTY_FG_Cyan = ESC + "[36m";
085:
086: public static final String TTY_FG_White = ESC + "[37m";
087:
088: /* Background Colors */
089:
090: public static final String TTY_BG_Black = ESC + "[40m";
091:
092: public static final String TTY_BG_Red = ESC + "[41m";
093:
094: public static final String TTY_BG_Green = ESC + "[42m";
095:
096: public static final String TTY_BG_Yellow = ESC + "[43m";
097:
098: public static final String TTY_BG_Blue = ESC + "[44m";
099:
100: public static final String TTY_BG_Magenta = ESC + "[45m";
101:
102: public static final String TTY_BG_Cyan = ESC + "[46m";
103:
104: public static final String TTY_BG_White = ESC + "[47m";
105:
106: static String PROMPT = TTY_Reset + TTY_Bright + "[openejb]$ "
107: + TTY_Reset;
108:
109: protected void exec(InputStream input, PrintStream out) {
110:
111: DataInputStream in = new DataInputStream(input);
112:
113: while (!stop) {
114:
115: prompt(in, out);
116:
117: }
118:
119: }
120:
121: protected void prompt(DataInputStream in, PrintStream out) {
122:
123: try {
124:
125: out.print(PROMPT);
126:
127: out.flush();
128:
129: String commandline = in.readLine();
130:
131: logger.debug("command: " + commandline);
132:
133: commandline = commandline.trim();
134:
135: if (commandline.length() < 1)
136: return;
137:
138: String command = commandline;
139:
140: Command.Arguments args = null;
141:
142: int spacePosition = commandline.indexOf(' ');
143:
144: int tabPosition = commandline.indexOf('\t');
145:
146: if (spacePosition != -1 || tabPosition != -1) {
147:
148: int cutPosition = (spacePosition > tabPosition ? spacePosition
149: : tabPosition);
150:
151: command = commandline.substring(0, cutPosition);
152:
153: args = new Command.Arguments(commandline
154: .substring(cutPosition + 1));
155:
156: }
157:
158: Command cmd = Command.getCommand(command);
159:
160: if (cmd == null) {
161:
162: out.print(command);
163:
164: out.println(": command not found");
165:
166: } else {
167:
168: cmd.exec(args, in, out);
169:
170: }
171:
172: } catch (UnsupportedOperationException e) {
173:
174: this .stop = true;
175:
176: } catch (Throwable e) {
177:
178: e.printStackTrace(new PrintStream(out));
179:
180: this .stop = true;
181:
182: }
183:
184: }
185:
186: protected void badCommand(DataInputStream in, PrintStream out)
187: throws IOException
188:
189: {
190:
191: }
192:
193: }
|