View Javadoc

1   /*** 
2    * Simple server that executes supplied script against a socket
3    * @author: Jeremy Rayner
4    */
5   
6   package groovy.ui;
7   import groovy.lang.*;
8   import java.io.*;
9   import java.net.*;
10  
11  public class GroovySocketServer implements Runnable {
12      private URL url;
13      private GroovyShell groovy;
14      private boolean isScriptFile;
15      private String scriptLocation;
16      private boolean autoOutput;
17      
18      public GroovySocketServer(GroovyShell groovy, boolean isScriptFile, String scriptLocation, boolean autoOutput, int port) {
19          this.groovy = groovy;
20          this.isScriptFile = isScriptFile;
21          this.scriptLocation = scriptLocation;
22          this.autoOutput = autoOutput;
23          try {
24              url = new URL("http", InetAddress.getLocalHost().getHostAddress(), port, "/");
25              System.out.println("groovy is listening on port " + port);
26          } catch (IOException e) { 
27              e.printStackTrace();
28          }
29          new Thread(this).start();
30      }
31  
32      public void run() {
33          try {
34              ServerSocket serverSocket = new ServerSocket(url.getPort());
35              while (true) {
36                  // create one script per socket connection
37                  Script script;
38                  if (isScriptFile) {
39                      script = groovy.parse(new File(scriptLocation));
40                  } else {
41                      script = groovy.parse(scriptLocation, "main");
42                  }
43                  new GroovyClientConnection(script, autoOutput, serverSocket.accept());
44              }
45          } catch (Exception e) {
46              e.printStackTrace();
47          }
48      }
49      
50      class GroovyClientConnection implements Runnable {
51          private Script script;
52          private Socket socket;
53          private BufferedReader reader;
54          private PrintWriter writer;
55          private boolean autoOutput;
56      
57          GroovyClientConnection(Script script, boolean autoOutput,Socket socket) throws IOException {
58              this.script = script;
59              this.autoOutput = autoOutput;
60              this.socket = socket;
61              reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
62              writer = new PrintWriter(socket.getOutputStream());
63              new Thread(this, "Groovy client connection - " + socket.getInetAddress().getHostAddress()).start();
64          }
65          public void run() {
66              try {
67                  String line = null;
68                  script.setProperty("out", writer);
69                  script.setProperty("socket", socket);
70                  script.setProperty("init", Boolean.TRUE);
71                  while ((line = reader.readLine()) != null) {
72                      // System.out.println(line);
73                      script.setProperty("line", line);
74                      Object o = script.run();
75                      script.setProperty("init", Boolean.FALSE);
76                      if (o != null) {
77                          if ("success".equals(o)) {
78                              break; // to close sockets gracefully etc...
79                          } else {
80                              if (autoOutput) {
81                                  writer.println(o);
82                              }
83                          }
84                      }
85                      writer.flush();
86                  }
87              } catch (IOException e) {
88                  e.printStackTrace();
89              } finally {
90                  try {
91                      writer.flush();
92                      writer.close();
93                  } finally {
94                      try {
95                          socket.close();
96                      } catch (IOException e3) {
97                          e3.printStackTrace();
98                      }
99                  }
100             }
101         }
102     }
103 }