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.PrintStream;
023:
024: import java.util.ArrayList;
025:
026: import java.util.HashMap;
027:
028: import java.util.Iterator;
029:
030: import java.util.StringTokenizer;
031:
032: public class Command {
033:
034: protected static final HashMap<String, Object> commands = new HashMap();
035:
036: static
037:
038: {
039:
040: loadCommandList();
041:
042: }
043:
044: protected static final Command unknownCommand = new Command();
045:
046: protected static void register(String name, Command cmd) {
047:
048: commands.put(name, cmd);
049:
050: }
051:
052: protected static void register(String name, Class cmd) {
053:
054: commands.put(name, cmd);
055:
056: }
057:
058: public static Command getCommand(String name) {
059:
060: Object cmd = commands.get(name);
061:
062: if (cmd instanceof Class) {
063:
064: cmd = loadCommand((Class) cmd);
065:
066: register(name, (Command) cmd);
067:
068: }
069:
070: return (Command) cmd;
071:
072: }
073:
074: public void exec(Arguments args, DataInputStream in, PrintStream out)
075: throws IOException
076:
077: {
078:
079: out.println("not implemented");
080:
081: }
082:
083: protected static Command loadCommand(Class commandClass) {
084:
085: Command cmd = null;
086:
087: try {
088:
089: cmd = (Command) commandClass.newInstance();
090:
091: } catch (Exception e) {
092:
093: }
094:
095: return cmd;
096:
097: }
098:
099: /*
100:
101: TODO:
102:
103: - Create the basic list in ant
104:
105: - Add the regexp package to the ant scripts
106:
107: - update the loadCommandList to read the list
108:
109: made in the ant script
110:
111: */
112:
113: protected static void loadCommandList() {
114:
115: Exit.register();
116:
117: Help.register();
118:
119: Lookup.register();
120:
121: Ls.register();
122:
123: Stop.register();
124:
125: Version.register();
126:
127: GroovySh.register();
128:
129: }
130:
131: public static class Arguments {
132:
133: private String args;
134:
135: private String[] argsArray = new String[0];
136:
137: private boolean alreadyParsed = false;
138:
139: Arguments(String args) {
140:
141: this .args = args;
142:
143: }
144:
145: String get() {
146:
147: return args;
148:
149: }
150:
151: String get(int i) {
152:
153: parseArgs();
154:
155: return (argsArray != null ? argsArray[i] : null);
156:
157: }
158:
159: int count() {
160:
161: parseArgs();
162:
163: return (argsArray != null ? argsArray.length : 0);
164:
165: }
166:
167: Iterator iterator() {
168:
169: return new Iterator() {
170:
171: StringTokenizer st = new StringTokenizer(args);
172:
173: public boolean hasNext() {
174:
175: return st.hasMoreTokens();
176:
177: }
178:
179: public Object next() {
180:
181: return st.nextToken();
182:
183: }
184:
185: public void remove() {
186:
187: }
188:
189: };
190:
191: }
192:
193: private void parseArgs() {
194:
195: if (!alreadyParsed) {
196:
197: ArrayList arrayList = new ArrayList();
198:
199: Iterator it = iterator();
200:
201: while (it.hasNext()) {
202:
203: arrayList.add(it.next());
204:
205: }
206:
207: argsArray = (String[]) arrayList.toArray(argsArray);
208:
209: alreadyParsed = true;
210:
211: }
212:
213: }
214:
215: }
216:
217: }
|