Top Description Inners Fields Constructors Methods
groovy.ui

public Class GroovySocketServer

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
groovy.lang.GroovyCodeSource, .GroovyRuntimeException, .GroovyShell, .Script, java.io.BufferedReader, .IOException, .InputStreamReader, .PrintWriter, java.net.InetAddress, .ServerSocket, .Socket, .URI, .URISyntaxException, .URL, java.util.regex.Pattern

Simple server that executes supplied script against a socket.

Typically this is used from the groovy command line agent but it can be invoked programmatically. To run this program from the command line please refer to the command line documentation at Running Groovy from the commandline.

Here is an example of how to use this class to open a listening socket on the server, listen for incoming data, and then echo the data back to the client in reverse order:

new GroovySocketServer(
        new GroovyShell(),      // evaluator
        false,                  // is not a file
        "println line.reverse()",         // script to evaluate
        true,                   // return result to client
        1960)                   //port
There are several variables in the script binding:

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final boolean
private static int
private final GroovyShell
private final GroovyCodeSource
private static final Pattern
private URL

Constructor Summary

AccessConstructor and Description
public
GroovySocketServer(GroovyShell
The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.
groovy
,
boolean
Whether the incoming socket data String will be a script or a file path.
isScriptFile
,
String
This will be a groovy script or a file location depending on the argument isScriptFile.
scriptFilenameOrText
,
boolean
whether output should be automatically echoed back to the client
autoOutput
,
int
the port to listen on
port
)

This creates and starts the socket server on a new Thread.

public
GroovySocketServer(GroovyShell
The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.
groovy
,
GroovyCodeSource
GroovyCodeSource for the Groovy script
source
,
boolean
whether output should be automatically echoed back to the client
autoOutput
,
int
the port to listen on
port
)

This creates and starts the socket server on a new Thread.

Method Summary

Modifier and TypeMethod and Description
private static synchronized String
private static GroovyCodeSource
getCodeSource(boolean scriptFile, String scriptFilenameOrText)

public void
run()

Implements java.lang.Runnable.run.

Runs this server.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoOutputback to summary
private final boolean autoOutput
counterback to summary
private static int counter
groovyback to summary
private final GroovyShell groovy
sourceback to summary
private final GroovyCodeSource source
URI_PATTERNback to summary
private static final Pattern URI_PATTERN
urlback to summary
private URL url

Constructor Detail

GroovySocketServerback to summary
public GroovySocketServer(GroovyShell groovy, boolean isScriptFile, String scriptFilenameOrText, boolean autoOutput, int port)

This creates and starts the socket server on a new Thread. There is no need to call run or spawn a new thread yourself.

Parameters
groovy:GroovyShell

The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.

isScriptFile:boolean

Whether the incoming socket data String will be a script or a file path.

scriptFilenameOrText:String

This will be a groovy script or a file location depending on the argument isScriptFile.

autoOutput:boolean

whether output should be automatically echoed back to the client

port:int

the port to listen on

GroovySocketServerback to summary
public GroovySocketServer(GroovyShell groovy, GroovyCodeSource source, boolean autoOutput, int port)

This creates and starts the socket server on a new Thread. There is no need to call run or spawn a new thread yourself.

Parameters
groovy:GroovyShell

The GroovyShell object that evaluates the incoming text. If you need additional classes in the classloader then configure that through this object.

source:GroovyCodeSource

GroovyCodeSource for the Groovy script

autoOutput:boolean

whether output should be automatically echoed back to the client

port:int

the port to listen on

Since
2.3.0

Method Detail

generateScriptNameback to summary
private static synchronized String generateScriptName()
getCodeSourceback to summary
private static GroovyCodeSource getCodeSource(boolean scriptFile, String scriptFilenameOrText)
runback to summary
public void run()

Implements java.lang.Runnable.run.

Runs this server. There is typically no need to call this method, as the object's constructor creates a new thread and runs this object automatically.

groovy.ui back to summary

pack-priv Class GroovySocketServer.GroovyClientConnection

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private boolean
private BufferedReader
private Script
private Socket
private PrintWriter

Constructor Summary

AccessConstructor and Description
pack-priv
GroovyClientConnection(Script script, boolean autoOutput, Socket socket)

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoOutputFlagback to summary
private boolean autoOutputFlag
readerback to summary
private BufferedReader reader
scriptback to summary
private Script script
socketback to summary
private Socket socket
writerback to summary
private PrintWriter writer

Constructor Detail

GroovyClientConnectionback to summary
pack-priv GroovyClientConnection(Script script, boolean autoOutput, Socket socket) throws IOException

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.