Coverage Report - org.apache.xbean.command.CommandShell
 
Classes in this File Line Coverage Branch Coverage Complexity
CommandShell
0%
0/42
0%
0/10
3.25
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.xbean.command;
 18  
 
 19  
 import java.io.DataInputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.PrintStream;
 23  
 import java.util.StringTokenizer;
 24  
 import java.util.Vector;
 25  
 
 26  
 
 27  
 public class CommandShell implements Command {
 28  
 
 29  
     private final String prompt;
 30  
 
 31  0
     public CommandShell(String serverName) {
 32  0
         serverName = serverName.toLowerCase();
 33  0
         prompt = TTY_Reset + TTY_Bright + "["+serverName+"]$ " + TTY_Reset;
 34  0
     }
 35  
 
 36  0
     private boolean stop = false;
 37  0
     private int rc = 0;
 38  
 
 39  
     public static final char ESC = (char) 27;
 40  
     public static final String TTY_Reset = ESC + "[0m";
 41  
     public static final String TTY_Bright = ESC + "[1m";
 42  
     public static final String TTY_Dim = ESC + "[2m";
 43  
     public static final String TTY_Underscore = ESC + "[4m";
 44  
     public static final String TTY_Blink = ESC + "[5m";
 45  
     public static final String TTY_Reverse = ESC + "[7m";
 46  
     public static final String TTY_Hidden = ESC + "[8m";
 47  
     /* Foreground Colors */
 48  
     public static final String TTY_FG_Black = ESC + "[30m";
 49  
     public static final String TTY_FG_Red = ESC + "[31m";
 50  
     public static final String TTY_FG_Green = ESC + "[32m";
 51  
     public static final String TTY_FG_Yellow = ESC + "[33m";
 52  
     public static final String TTY_FG_Blue = ESC + "[34m";
 53  
     public static final String TTY_FG_Magenta = ESC + "[35m";
 54  
     public static final String TTY_FG_Cyan = ESC + "[36m";
 55  
     public static final String TTY_FG_White = ESC + "[37m";
 56  
     /* Background Colors */
 57  
     public static final String TTY_BG_Black = ESC + "[40m";
 58  
     public static final String TTY_BG_Red = ESC + "[41m";
 59  
     public static final String TTY_BG_Green = ESC + "[42m";
 60  
     public static final String TTY_BG_Yellow = ESC + "[43m";
 61  
     public static final String TTY_BG_Blue = ESC + "[44m";
 62  
     public static final String TTY_BG_Magenta = ESC + "[45m";
 63  
     public static final String TTY_BG_Cyan = ESC + "[46m";
 64  
     public static final String TTY_BG_White = ESC + "[47m";
 65  
 
 66  
     public int main(String[] args, InputStream input, PrintStream out) {
 67  
 
 68  0
         DataInputStream in = new DataInputStream(input);
 69  0
         while (!stop) {
 70  0
             prompt(in, out);
 71  
         }
 72  0
         return rc;
 73  
     }
 74  
 
 75  
     protected void prompt(DataInputStream in, PrintStream out) {
 76  
         try {
 77  0
             out.print(prompt);
 78  0
             out.flush();
 79  
 
 80  0
             String commandline = in.readLine();
 81  0
             if( commandline == null ) {
 82  0
                 this.stop = true;
 83  0
                 return;
 84  
             }
 85  0
             commandline = commandline.trim();
 86  0
             if (commandline.length() < 1) {
 87  0
                 return;
 88  
             }
 89  
 
 90  0
             String command = commandline;
 91  
 
 92  0
             StringTokenizer cmdstr = new StringTokenizer(command);
 93  0
             command = cmdstr.nextToken();
 94  
 
 95  
             // Get parameters
 96  0
             Vector p = new Vector();
 97  0
             while ( cmdstr.hasMoreTokens() ) {
 98  0
                 p.add(cmdstr.nextToken());
 99  
             }
 100  0
             String[] args = new String[p.size()];
 101  0
             p.copyInto(args);
 102  
 
 103  0
             Command cmd = CommandRegistry.getCommand(command);
 104  
 
 105  0
             if (cmd == null) {
 106  0
                 out.print(command);
 107  0
                 out.println(": command not found");
 108  
             } else {
 109  0
                 cmd.main(args, in, out);
 110  
             }
 111  0
         } catch (UnsupportedOperationException e) {
 112  0
             this.rc=-1;
 113  0
             this.stop = true;
 114  0
         } catch (Throwable e) {
 115  0
             e.printStackTrace(out);
 116  0
             this.rc=-1;
 117  0
             this.stop = true;
 118  0
         }
 119  0
     }
 120  
 
 121  
     protected void badCommand(DataInputStream in, PrintStream out) throws IOException {
 122  
         //asdf: command not found
 123  0
     }
 124  
 
 125  
 }