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.
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
;
32 * Simple server that executes supplied script against a socket
35 * @author Jeremy Rayner
37 public class GroovySocketServer
implements Runnable
{
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
) {
46 this.isScriptFile
= isScriptFile
;
47 this.scriptFilenameOrText
= scriptFilenameOrText
;
48 this.autoOutput
= autoOutput
;
50 url
= new URL("http", InetAddress
.getLocalHost().getHostAddress(), port
, "/");
51 System
.out
.println("groovy is listening on port " + port
);
52 } catch (IOException e
) {
55 new Thread(this).start();
60 ServerSocket serverSocket
= new ServerSocket(url
.getPort());
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.
68 GroovyMain gm
= new GroovyMain();
69 script
= groovy
.parse(new FileInputStream(gm
.huntForTheScriptFile(scriptFilenameOrText
)));
71 script
= groovy
.parse(scriptFilenameOrText
);
73 new GroovyClientConnection(script
, autoOutput
, serverSocket
.accept());
75 } catch (Exception e
) {
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
{
89 this.autoOutputFlag
= autoOutput
;
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();
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
);
107 if ("success".equals(o
)) {
108 break; // to close sockets gracefully etc...
110 if (autoOutputFlag
) {
117 } catch (IOException e
) {
126 } catch (IOException e3
) {
127 e3
.printStackTrace();