This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Dynamically loading a java agent in a ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dynamically loading a java agent in a ServletContextListener" Watch "Dynamically loading a java agent in a ServletContextListener" New topic
Author

Dynamically loading a java agent in a ServletContextListener

Randy Baiad
Greenhorn

Joined: Jan 20, 2013
Posts: 1
My goal is to add tracing and other features to an existing application deployed.

My requirements:
- 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:/......

With that,
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,

http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/

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.


Is this feasible with dynamic javaagent?


Thanks,

Randy



 
Don't get me started about those stupid light bulbs.
 
subject: Dynamically loading a java agent in a ServletContextListener