fixed build console
[groovy.git] / src / main / groovy / ui / GroovySocketServer.java
blob8bfeb9b53c9c225778443edd75e58678aef24e27
1 /*
2 * Copyright 2003-2007 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package groovy.ui;
18 import groovy.lang.GroovyShell;
19 import groovy.lang.Script;
21 import java.io.BufferedReader;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.PrintWriter;
26 import java.net.InetAddress;
27 import java.net.ServerSocket;
28 import java.net.Socket;
29 import java.net.URL;
31 /**
32 * Simple server that executes supplied script against a socket
34 * @version $Id$
35 * @author Jeremy Rayner
37 public class GroovySocketServer implements Runnable {
38 private URL url;
39 private GroovyShell groovy;
40 private boolean isScriptFile;
41 private String scriptFilenameOrText;
42 private boolean autoOutput;
44 public GroovySocketServer(GroovyShell groovy, boolean isScriptFile, String scriptFilenameOrText, boolean autoOutput, int port) {
45 this.groovy = groovy;
46 this.isScriptFile = isScriptFile;
47 this.scriptFilenameOrText = scriptFilenameOrText;
48 this.autoOutput = autoOutput;
49 try {
50 url = new URL("http", InetAddress.getLocalHost().getHostAddress(), port, "/");
51 System.out.println("groovy is listening on port " + port);
52 } catch (IOException e) {
53 e.printStackTrace();
55 new Thread(this).start();
58 public void run() {
59 try {
60 ServerSocket serverSocket = new ServerSocket(url.getPort());
61 while (true) {
62 // Create one script per socket connection.
63 // This is purposefully not caching the Script
64 // so that the script source file can be changed on the fly,
65 // as each connection is made to the server.
66 Script script;
67 if (isScriptFile) {
68 GroovyMain gm = new GroovyMain();
69 script = groovy.parse(new FileInputStream(gm.huntForTheScriptFile(scriptFilenameOrText)));
70 } else {
71 script = groovy.parse(scriptFilenameOrText);
73 new GroovyClientConnection(script, autoOutput, serverSocket.accept());
75 } catch (Exception e) {
76 e.printStackTrace();
80 class GroovyClientConnection implements Runnable {
81 private Script script;
82 private Socket socket;
83 private BufferedReader reader;
84 private PrintWriter writer;
85 private boolean autoOutputFlag;
87 GroovyClientConnection(Script script, boolean autoOutput,Socket socket) throws IOException {
88 this.script = script;
89 this.autoOutputFlag = autoOutput;
90 this.socket = socket;
91 reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
92 writer = new PrintWriter(socket.getOutputStream());
93 new Thread(this, "Groovy client connection - " + socket.getInetAddress().getHostAddress()).start();
95 public void run() {
96 try {
97 String line = null;
98 script.setProperty("out", writer);
99 script.setProperty("socket", socket);
100 script.setProperty("init", Boolean.TRUE);
101 while ((line = reader.readLine()) != null) {
102 // System.out.println(line);
103 script.setProperty("line", line);
104 Object o = script.run();
105 script.setProperty("init", Boolean.FALSE);
106 if (o != null) {
107 if ("success".equals(o)) {
108 break; // to close sockets gracefully etc...
109 } else {
110 if (autoOutputFlag) {
111 writer.println(o);
115 writer.flush();
117 } catch (IOException e) {
118 e.printStackTrace();
119 } finally {
120 try {
121 writer.flush();
122 writer.close();
123 } finally {
124 try {
125 socket.close();
126 } catch (IOException e3) {
127 e3.printStackTrace();