| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
} |