wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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

Dynamically loading a java agent in a ServletContextListener

Randy Baiad

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,


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?



It is sorta covered in the JavaRanch Style Guide.
subject: Dynamically loading a java agent in a ServletContextListener
Similar Threads
Help on WebLogic ClassLoaders
How find all loaded classes/classloaders in JVM?
How to set a new Custom ClassLoader?
com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize
Thoroughly Confused about ClassLoading in JAVA