Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package groovy.ui;
 
 
 import java.net.URL;

Simple server that executes supplied script against a socket

Author(s):
Jeremy Rayner
Version:
$Id: GroovySocketServer.java 7252 2007-08-15 22:17:24Z user57 $
 
 public class GroovySocketServer implements Runnable {
     private URL url;
     private GroovyShell groovy;
     private boolean isScriptFile;
     private String scriptFilenameOrText;
     private boolean autoOutput;
     
     public GroovySocketServer(GroovyShell groovyboolean isScriptFileString scriptFilenameOrTextboolean autoOutputint port) {
         this. = groovy;
         this. = isScriptFile;
         this. = scriptFilenameOrText;
         this. = autoOutput;
         try {
              = new URL("http", InetAddress.getLocalHost().getHostAddress(), port"/");
             ..println("groovy is listening on port " + port);
         } catch (IOException e) { 
             e.printStackTrace();
         }
         new Thread(this).start();
     }
 
     public void run() {
         try {
             ServerSocket serverSocket = new ServerSocket(.getPort());
             while (true) {
                 // Create one script per socket connection.
                 // This is purposefully not caching the Script
                 // so that the script source file can be changed on the fly,
                 // as each connection is made to the server.
                 Script script;
                 if () {
                     GroovyMain gm = new GroovyMain();
                     script = .parse(new FileInputStream(gm.huntForTheScriptFile()));
                 } else {
                     script = .parse();
                 }
                 new GroovyClientConnection(scriptserverSocket.accept());
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
     
     class GroovyClientConnection implements Runnable {
         private Script script;
         private Socket socket;
         private BufferedReader reader;
         private PrintWriter writer;
         private boolean autoOutputFlag;
     
         GroovyClientConnection(Script scriptboolean autoOutput,Socket socketthrows IOException {
             this. = script;
             this. = autoOutput;
             this. = socket;
              = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              = new PrintWriter(socket.getOutputStream());
             new Thread(this"Groovy client connection - " + socket.getInetAddress().getHostAddress()).start();
         }
         public void run() {
             try {
                 String line = null;
                 .setProperty("out");
                 .setProperty("socket");
                .setProperty("init".);
                while ((line = .readLine()) != null) {
                    // System.out.println(line);
                    .setProperty("line"line);
                    Object o = .run();
                    .setProperty("init".);
                    if (o != null) {
                        if ("success".equals(o)) {
                            break// to close sockets gracefully etc...
                        } else {
                            if () {
                                .println(o);
                            }
                        }
                    }
                    .flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    .flush();
                    .close();
                } finally {
                    try {
                        .close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X