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