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 groovy.lang.GroovyShell;
020    import org.codehaus.groovy.runtime.InvokerHelper;
021    
022    import java.io.BufferedReader;
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.InputStreamReader;
026    import java.io.PrintStream;
027    
028    public class GroovySh implements Command {
029        public static void register() {
030            CommandRegistry.register("groovysh", GroovySh.class);
031        }
032    
033        public int main(String[] args, InputStream in, PrintStream out) {
034            GroovyShell shell = new GroovyShell();
035            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
036            String version = InvokerHelper.getVersion();
037            out.println("Lets get Groovy!");
038            out.println("================");
039            out.println("Version: " + version + " JVM: " + System.getProperty("java.vm.version"));
040            out.println("Hit carriage return twice to execute a command");
041            out.println("The command 'quit' will terminate the shell");
042            int counter = 1;
043            while (true) {
044                StringBuffer buffer = new StringBuffer();
045                while (true) {
046                    out.print("groovy> ");
047                    String line;
048                    try {
049                        line = reader.readLine();
050                    } catch (IOException e) {
051                        out.println("Caught: " + e);
052                        e.printStackTrace();
053                        return -1;
054                    }
055                    if (line != null) {
056                        buffer.append(line);
057                        buffer.append('\n');
058                    }
059                    if (line == null || line.trim().length() == 0) {
060                        break;
061                    }
062                }
063                String command = buffer.toString().trim();
064                if (command == null || command.equals("quit")) {
065                    break;
066                }
067                try {
068                    Object answer = shell.evaluate(command, "CommandLine" + counter++ + ".groovy");
069                    out.println(InvokerHelper.inspect(answer));
070                } catch (Exception e) {
071                    out.println("Caught: " + e);
072                    e.printStackTrace();
073                }
074            }
075            return 0;
076        }
077    }