Dynamically loading a java agent in a ServletContextListener
posted 3 years ago
My goal is to add tracing and other features to an existing application deployed.
- Don't change the existing application if possible. (adding listeners are ok, but no changes to the existing source code ).
- No change to the Application Server startup... no -javaagent:/......
I started thinking that AspectJ would solve tracing with the weaver. But that didn't allow for loading the agent dynamically.
Sure I could modify the org.aspectj.weaver.loadtime.Agent and add agentmain, and I did that but still had problems.
Then I wrote my own very simple agent, thanks to the article from: Dhruba,
I wrote a ServletContextListener that, on contextInitialized, the agent is dynamically loaded:
The agent gets loaded successfully when the ContextListener is initialized.
When the ClassFileTransformer's transform method is called, none of my classes in my Servlet are being passed in.
Here is the simple agentmain:
If I can prove that my classes from my servlet are passed to the Transformer, then I may be able to use org.objectweb.asm to manipulate my classes to eventually add dynamic tracing of a particular object throughout the application.