Clover coverage report - groovy - 1.0-beta-8
Coverage timestamp: Fri Dec 17 2004 14:55:55 GMT
file stats: LOC: 104   Methods: 4
NCLOC: 91   Classes: 2
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
GroovySocketServer.java 0% 0% 0% 0%
coverage
 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  0
     public GroovySocketServer(GroovyShell groovy, boolean isScriptFile, String scriptLocation, boolean autoOutput, int port) {
 19  0
         this.groovy = groovy;
 20  0
         this.isScriptFile = isScriptFile;
 21  0
         this.scriptLocation = scriptLocation;
 22  0
         this.autoOutput = autoOutput;
 23  0
         try {
 24  0
             url = new URL("http", InetAddress.getLocalHost().getHostAddress(), port, "/");
 25  0
             System.out.println("groovy is listening on port " + port);
 26   
         } catch (IOException e) { 
 27  0
             e.printStackTrace();
 28   
         }
 29  0
         new Thread(this).start();
 30   
     }
 31   
 
 32  0
     public void run() {
 33  0
         try {
 34  0
             ServerSocket serverSocket = new ServerSocket(url.getPort());
 35  0
             while (true) {
 36   
                 // create one script per socket connection
 37  0
                 Script script;
 38  0
                 if (isScriptFile) {
 39  0
                     script = groovy.parse(new File(scriptLocation));
 40   
                 } else {
 41  0
                     script = groovy.parse(scriptLocation, "main");
 42   
                 }
 43  0
                 new GroovyClientConnection(script, autoOutput, serverSocket.accept());
 44   
             }
 45   
         } catch (Exception e) {
 46  0
             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  0
         GroovyClientConnection(Script script, boolean autoOutput,Socket socket) throws IOException {
 58  0
             this.script = script;
 59  0
             this.autoOutput = autoOutput;
 60  0
             this.socket = socket;
 61  0
             reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 62  0
             writer = new PrintWriter(socket.getOutputStream());
 63  0
             new Thread(this, "Groovy client connection - " + socket.getInetAddress().getHostAddress()).start();
 64   
         }
 65  0
         public void run() {
 66  0
             try {
 67  0
                 String line = null;
 68  0
                 script.setProperty("out", writer);
 69  0
                 script.setProperty("socket", socket);
 70  0
                 script.setProperty("init", Boolean.TRUE);
 71  0
                 while ((line = reader.readLine()) != null) {
 72   
                     // System.out.println(line);
 73  0
                     script.setProperty("line", line);
 74  0
                     Object o = script.run();
 75  0
                     script.setProperty("init", Boolean.FALSE);
 76  0
                     if (o != null) {
 77  0
                         if ("success".equals(o)) {
 78  0
                             break; // to close sockets gracefully etc...
 79   
                         } else {
 80  0
                             if (autoOutput) {
 81  0
                                 writer.println(o);
 82   
                             }
 83   
                         }
 84   
                     }
 85  0
                     writer.flush();
 86   
                 }
 87   
             } catch (IOException e) {
 88  0
                 e.printStackTrace();
 89   
             } finally {
 90  0
                 try {
 91  0
                     writer.flush();
 92  0
                     writer.close();
 93   
                 } finally {
 94  0
                     try {
 95  0
                         socket.close();
 96   
                     } catch (IOException e3) {
 97  0
                         e3.printStackTrace();
 98   
                     }
 99   
                 }
 100   
             }
 101   
         }
 102   
     }
 103   
 }
 104