Class ClasspathAgent

All Implemented Interfaces:
ClasspathAgentMBean, ClassLoading, Shutdownable, Serializable, Runnable

public class ClasspathAgent extends ShutdownHook implements ClasspathAgentMBean
This is a simple Java agent to be able to watch the classpath. We need it because some classloaders (e.g. the classloader of the IBM JDK) does not provide the information which classes are loaded.

Originally this class was part of patterntesting-agent.

Since:
0.8
Author:
oboehm
See Also:
  • Field Details

    • MBEAN_NAME

      public static final String MBEAN_NAME
      The Constant MBEAN_NAME for the registered name in JMX.
      See Also:
  • Method Details

    • getInstance

      public static ClasspathAgent getInstance()
      Gets the single instance of ClasspathAgent.
      Returns:
      single instance of ClasspathAgent
    • agentmain

      public static void agentmain(String agentArgs, Instrumentation inst)
      This method will be called if the class is loaded dynmically by ClasspathAgentLoader.
      Parameters:
      agentArgs - the agent args
      inst - the inst
    • premain

      public static void premain(String agentArgs, Instrumentation inst)
      I guess this method will be called from the VM.
      Parameters:
      agentArgs - the agent args
      inst - the inst
    • getInstrumentation

      public static Instrumentation getInstrumentation()
      Gets the instrumentation.
      Returns:
      the instrumentation
    • getArgs

      public String getArgs()
      Get the arguments from the call as agent.
      Specified by:
      getArgs in interface ClasspathAgentMBean
      Returns:
      the args
    • getLoadedClasses

      public Class<?>[] getLoadedClasses()
      Gets the loaded classes.
      Specified by:
      getLoadedClasses in interface ClassLoading
      Returns:
      the loaded classes
    • getLoadedClasses

      public Class<?>[] getLoadedClasses(ClassLoader classloader)
      Gets the loaded classes.
      Specified by:
      getLoadedClasses in interface ClasspathAgentMBean
      Parameters:
      classloader - the classloader
      Returns:
      the loaded classes
      See Also:
    • getLoadedClassnames

      public String[] getLoadedClassnames()
      Returns the classes which were loaded by the classloader. The loaded packages are returned as string array so that it can be displayed by the 'jconsole'.

      Note: Because we had in the past some doublets in the resulting array we use now a SortedSet for sorting.

      Specified by:
      getLoadedClassnames in interface ClassLoading
      Returns:
      the classnames as string array
    • isActive

      public boolean isActive()
      Checks if is active. This is true if this class here was started as Java agent.
      Specified by:
      isActive in interface ClasspathAgentMBean
      Returns:
      true, if started as Java agent
    • logLoadedClasses

      public void logLoadedClasses()
      Prints the loaded classes to the log output.
      Specified by:
      logLoadedClasses in interface ClasspathAgentMBean
      See Also:
    • dumpLoadedClasses

      public File dumpLoadedClasses()
      This operation dumps the loaded classes to a temporary file with the prefix "dumpLoadedClasses" and the extension ".txt".

      To be able to see the name of the temporary file in the 'jconsole' it is returned as value.

      Specified by:
      dumpLoadedClasses in interface ClasspathAgentMBean
      Returns:
      the temporary file
      See Also:
    • getAllClasses

      public String[] getAllClasses()
      Gets all classes which are available thru the classpath
      Specified by:
      getAllClasses in interface ClasspathAgentMBean
      Returns:
      all classes of the classpath
      Since:
      3.0
    • getUnusedClasses

      public String[] getUnusedClasses()
      Gets unused classes.
      Specified by:
      getUnusedClasses in interface ClasspathAgentMBean
      Returns:
      all unused classes of the classpath
      Since:
      3.0
    • getDumpURI

      public URI getDumpURI()
      Description copied from interface: ClasspathAgentMBean
      Returns the URI where a little statistic of loaded and unloaded classes are dumped to.
      Specified by:
      getDumpURI in interface ClasspathAgentMBean
      Returns:
      dump URI or "dev:/null" if nothing is dumped
    • setDumpURI

      public void setDumpURI(URI dumpURI)
      Description copied from interface: ClasspathAgentMBean
      It is possible to change the URI where the classes are dumped to. If the URI cannot be handled (e.g. it is not a file URI) it will be ignored.
      Specified by:
      setDumpURI in interface ClasspathAgentMBean
      Parameters:
      dumpURI - e.g. "file://tmp/test.csv"
    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread