
We embed groovy shell in application
At work, sometimes we need to get access to a working java application to look at some data or twitch methods. We used to do what we wrote jsp, drop it into the directory in which our application is deployed, and then request this jsp. It was not very convenient.
And once I had the idea to make my life easier by integrating the groovy shell into the application, making access to it through telnet.
We use spring, so the service itself is implemented as a spring bean and the service gives access to the context in which it is defined.
In order to enable the telnet server, you need to add the following lines to the config:
For security reasons, the socket only binds to the loopback interface.
Now, after starting the project, you can connect to a running java machine using telnet, in which we will see the groovy shell interface, where we can write arbitrary groovy code.
Special variable names are available in the shell: context - for accessing the spring context in which our GroovyShellService is located and many bin identifiers defined in this context for direct access to them.
In the groovy shell, autocompletion of bin identifiers and called methods works (by pressing the tab key).
Session Example:
The source code is on github .
And once I had the idea to make my life easier by integrating the groovy shell into the application, making access to it through telnet.
We use spring, so the service itself is implemented as a spring bean and the service gives access to the context in which it is defined.
In order to enable the telnet server, you need to add the following lines to the config:
For security reasons, the socket only binds to the loopback interface.
Now, after starting the project, you can connect to a running java machine using telnet, in which we will see the groovy shell interface, where we can write arbitrary groovy code.
Special variable names are available in the shell: context - for accessing the spring context in which our GroovyShellService is located and many bin identifiers defined in this context for direct access to them.
In the groovy shell, autocompletion of bin identifiers and called methods works (by pressing the tab key).
Session Example:
$ telnet 127.0.0.1 3333
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Groovy Shell (1.8.5, JVM: 1.7.0_51)
Type 'help' or '\h' for help.
-----------------------------------------------------------------------------------------------------------
groovy:000> context.isActive()
===> true
groovy:000> Arrays.toString(context.getBeanDefinitionNames())
===> [org.springframework.context.annotation.internalConfigurationAnnotationProcessor, ....]
groovy:000> userDAO.findAll();
===> [XXX, YYY]
The source code is on github .