Coverage Report - org.apache.xbean.command.GroovySh
 
Classes in this File Line Coverage Branch Coverage Complexity
GroovySh
0%
0/37
0%
0/10
5
 
 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 groovy.lang.GroovyShell;
 20  
 import org.codehaus.groovy.runtime.InvokerHelper;
 21  
 
 22  
 import java.io.BufferedReader;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.InputStreamReader;
 26  
 import java.io.PrintStream;
 27  
 
 28  0
 public class GroovySh implements Command {
 29  
     public static void register() {
 30  0
         CommandRegistry.register("groovysh", GroovySh.class);
 31  0
     }
 32  
 
 33  
     public int main(String[] args, InputStream in, PrintStream out) {
 34  0
         GroovyShell shell = new GroovyShell();
 35  0
         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
 36  0
         String version = InvokerHelper.getVersion();
 37  0
         out.println("Lets get Groovy!");
 38  0
         out.println("================");
 39  0
         out.println("Version: " + version + " JVM: " + System.getProperty("java.vm.version"));
 40  0
         out.println("Hit carriage return twice to execute a command");
 41  0
         out.println("The command 'quit' will terminate the shell");
 42  0
         int counter = 1;
 43  
         while (true) {
 44  0
             StringBuffer buffer = new StringBuffer();
 45  
             while (true) {
 46  0
                 out.print("groovy> ");
 47  
                 String line;
 48  
                 try {
 49  0
                     line = reader.readLine();
 50  0
                 } catch (IOException e) {
 51  0
                     out.println("Caught: " + e);
 52  0
                     e.printStackTrace();
 53  0
                     return -1;
 54  0
                 }
 55  0
                 if (line != null) {
 56  0
                     buffer.append(line);
 57  0
                     buffer.append('\n');
 58  
                 }
 59  0
                 if (line == null || line.trim().length() == 0) {
 60  0
                     break;
 61  
                 }
 62  0
             }
 63  0
             String command = buffer.toString().trim();
 64  0
             if (command == null || command.equals("quit")) {
 65  0
                 break;
 66  
             }
 67  
             try {
 68  0
                 Object answer = shell.evaluate(command, "CommandLine" + counter++ + ".groovy");
 69  0
                 out.println(InvokerHelper.inspect(answer));
 70  0
             } catch (Exception e) {
 71  0
                 out.println("Caught: " + e);
 72  0
                 e.printStackTrace();
 73  0
             }
 74  0
         }
 75  0
         return 0;
 76  
     }
 77  
 }