At work we sometimes have to get access to a running java application in order to view some data or pull methods. Previously, we managed to write jsp, threw it into the directory in which our application was deployed, and then requested this jsp. It was not very convenient.
And once I had the idea to make my life easier by embedding a groovy shell into an application, making it available via 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, add the following lines to the config:
<bean class="ru.ind.tgs.GroovyShellService" p:listenPort="3333"/>
For security reasons, the socket is only looped to the 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 an arbitrary groovy code.
In the shell, special variable names are available: context - to access the spring context in which our GroovyShellService is located and the set of identifiers of the beans defined in this context for direct access to them.
In groovy shell, autocompletion of the identifiers of the beans and the methods being invoked (by the tab key) works.
Sample session:
$ 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 .