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:
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private static int | |
| private final GroovyShell | |
| private final GroovyCodeSource | |
| private static final Pattern | |
| private URL |
| Access | Constructor 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. |
| Modifier and Type | Method and Description |
|---|---|
| private static synchronized String | |
| private static GroovyCodeSource | |
| public void |
| autoOutput | back to summary |
|---|---|
| private final boolean autoOutput | |
| counter | back to summary |
|---|---|
| private static int counter | |
| groovy | back to summary |
|---|---|
| private final GroovyShell groovy | |
| source | back to summary |
|---|---|
| private final GroovyCodeSource source | |
| URI_PATTERN | back to summary |
|---|---|
| private static final Pattern URI_PATTERN | |
| url | back to summary |
|---|---|
| private URL url | |
| GroovySocketServer | back 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.
| |
| GroovySocketServer | back 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.
| |
| generateScriptName | back to summary |
|---|---|
| private static synchronized String generateScriptName() | |
| getCodeSource | back to summary |
|---|---|
| private static GroovyCodeSource getCodeSource(boolean scriptFile, String scriptFilenameOrText) | |
| run | back to summary |
|---|---|
| public void run() Implements java. 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. | |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private BufferedReader | |
| private Script | |
| private Socket | |
| private PrintWriter |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void |
| autoOutputFlag | back to summary |
|---|---|
| private boolean autoOutputFlag | |
| reader | back to summary |
|---|---|
| private BufferedReader reader | |
| script | back to summary |
|---|---|
| private Script script | |
| socket | back to summary |
|---|---|
| private Socket socket | |
| writer | back to summary |
|---|---|
| private PrintWriter writer | |
| GroovyClientConnection | back to summary |
|---|---|
| pack-priv GroovyClientConnection(Script script, boolean autoOutput, Socket socket) throws IOException | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation. | |