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     */
017    package org.apache.xbean.command;
018    
019    import java.io.DataInputStream;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.PrintStream;
023    import java.util.StringTokenizer;
024    import java.util.Vector;
025    
026    
027    public class CommandShell implements Command {
028    
029        private final String prompt;
030    
031        public CommandShell(String serverName) {
032            serverName = serverName.toLowerCase();
033            prompt = TTY_Reset + TTY_Bright + "["+serverName+"]$ " + TTY_Reset;
034        }
035    
036        private boolean stop = false;
037        private int rc = 0;
038    
039        public static final char ESC = (char) 27;
040        public static final String TTY_Reset = ESC + "[0m";
041        public static final String TTY_Bright = ESC + "[1m";
042        public static final String TTY_Dim = ESC + "[2m";
043        public static final String TTY_Underscore = ESC + "[4m";
044        public static final String TTY_Blink = ESC + "[5m";
045        public static final String TTY_Reverse = ESC + "[7m";
046        public static final String TTY_Hidden = ESC + "[8m";
047        /* Foreground Colors */
048        public static final String TTY_FG_Black = ESC + "[30m";
049        public static final String TTY_FG_Red = ESC + "[31m";
050        public static final String TTY_FG_Green = ESC + "[32m";
051        public static final String TTY_FG_Yellow = ESC + "[33m";
052        public static final String TTY_FG_Blue = ESC + "[34m";
053        public static final String TTY_FG_Magenta = ESC + "[35m";
054        public static final String TTY_FG_Cyan = ESC + "[36m";
055        public static final String TTY_FG_White = ESC + "[37m";
056        /* Background Colors */
057        public static final String TTY_BG_Black = ESC + "[40m";
058        public static final String TTY_BG_Red = ESC + "[41m";
059        public static final String TTY_BG_Green = ESC + "[42m";
060        public static final String TTY_BG_Yellow = ESC + "[43m";
061        public static final String TTY_BG_Blue = ESC + "[44m";
062        public static final String TTY_BG_Magenta = ESC + "[45m";
063        public static final String TTY_BG_Cyan = ESC + "[46m";
064        public static final String TTY_BG_White = ESC + "[47m";
065    
066        public int main(String[] args, InputStream input, PrintStream out) {
067    
068            DataInputStream in = new DataInputStream(input);
069            while (!stop) {
070                prompt(in, out);
071            }
072            return rc;
073        }
074    
075        protected void prompt(DataInputStream in, PrintStream out) {
076            try {
077                out.print(prompt);
078                out.flush();
079    
080                String commandline = in.readLine();
081                if( commandline == null ) {
082                    this.stop = true;
083                    return;
084                }
085                commandline = commandline.trim();
086                if (commandline.length() < 1) {
087                    return;
088                }
089    
090                String command = commandline;
091    
092                StringTokenizer cmdstr = new StringTokenizer(command);
093                command = cmdstr.nextToken();
094    
095                // Get parameters
096                Vector p = new Vector();
097                while ( cmdstr.hasMoreTokens() ) {
098                    p.add(cmdstr.nextToken());
099                }
100                String[] args = new String[p.size()];
101                p.copyInto(args);
102    
103                Command cmd = CommandRegistry.getCommand(command);
104    
105                if (cmd == null) {
106                    out.print(command);
107                    out.println(": command not found");
108                } else {
109                    cmd.main(args, in, out);
110                }
111            } catch (UnsupportedOperationException e) {
112                this.rc=-1;
113                this.stop = true;
114            } catch (Throwable e) {
115                e.printStackTrace(out);
116                this.rc=-1;
117                this.stop = true;
118            }
119        }
120    
121        protected void badCommand(DataInputStream in, PrintStream out) throws IOException {
122            //asdf: command not found
123        }
124    
125    }