Class ClasspathDigger

java.lang.Object
clazzfish.monitor.internal.AbstractDigger
clazzfish.monitor.internal.ClasspathDigger
Direct Known Subclasses:
ResourcepathDigger

public class ClasspathDigger extends AbstractDigger
This helper class digs into the classloader for information like used classpath and other things. It was extracted from ClasspathMonitor to separate the classloader specific part of it into its own class.

Originally this class was part of the PatternTesting project where it was moved into an internal package. It is not intended for external use.

Author:
oliver
  • Field Details Link icon

  • Constructor Details Link icon

    • ClasspathDigger Link icon

      public ClasspathDigger()
      Instantiates a new classpath digger.
    • ClasspathDigger Link icon

      public ClasspathDigger(ClassLoader cloader)
      Instantiates a new classpath digger.
      Parameters:
      cloader - the cloader
      Since:
      1.2
  • Method Details Link icon

    • getClassLoader Link icon

      public ClassLoader getClassLoader()
      Gets the class loader.
      Returns:
      the classLoader
    • isClassloaderSupported Link icon

      public boolean isClassloaderSupported()
      Checks if is classloader supported.
      Returns:
      true, if is classloader supported
    • isAgentAvailable Link icon

      public static boolean isAgentAvailable()
      Checks if the ClasspathAgent is available as MBean. The ClasspathAgent is needed for classloaders which are not directly supported (e.g. IBM's classloader of their JDK).
      Returns:
      true, if is agent available
    • getBootClasspath Link icon

      public String[] getBootClasspath()
      To get the boot classpath the sytem property "sun.boot.class.path" is used to get them. This will work of course only for the SunVM.
      Returns:
      the boot classpath as String array
    • getClasspath Link icon

      public String[] getClasspath()
      We can use the system property "java.class.path" to get the classpath. But this works not inside an application server or servlet engine (e.g. inside Tomcat) because they have their own classloader to load the classes.

      In the past we tried to use the private (and undocoumented) attribute "domains" of the classloader. This works for a normal application but Tomcat's WebappClassLoader listed also classes in the "domains"-Set. Now we will try to detect the different classloader to access some private and secret attributes of this classloader.

      At the moment only org.apache.catalina.loader.WebappClassLoader is supported. For all other classloaders the standard approach using the system property "java.class.path" is used.

      Returns:
      the classpath as String array
    • getClasspath Link icon

      protected static String[] getClasspath(String key)
      Gets the classpath.
      Parameters:
      key - the key
      Returns:
      the classpath as String array
    • getPackageArray Link icon

      public String[] getPackageArray()
      Get all packages found in the classpath.

      In contradiction to getLoadedPackageArray() we don't ask the classloader (e.g. by calling classLoader.getResourceAsStream("")) for the packages because the classloader would return only these packages it has already loaded.

      Returns:
      the packages
    • getLoadedPackageArray Link icon

      public Package[] getLoadedPackageArray()
      Returns the packages which were loaded by the classloader.
      Returns:
      array with the loaded packages
    • getClasspathFromPackages Link icon

      protected String[] getClasspathFromPackages()
      Here we use the loaded packages to calculate the classpath. For each loaded package we will look from which jar file or directory this package is loaded.
      Returns:
      the found classpath as string array
      Since:
      27-Jul-2009
    • getResources Link icon

      public Enumeration<URI> getResources(String name)
      Gets the resources of the given name. Normally that would only be one element but some resources (like the MANIFEST.MF file) can appear several times in the classpath.
      Specified by:
      getResources in class AbstractDigger
      Parameters:
      name - the name of the resource
      Returns:
      all resources with the given name
    • getResourcepathSet Link icon

      public SortedSet<URI> getResourcepathSet(Collection<String> resources)
      Gets the resourcepathes which belongs to the given collection of resources.
      Parameters:
      resources - the resources
      Returns:
      the resourcepath set
      Since:
      1.6.3
    • whichResource Link icon

      public URI whichResource(String name)
      Returns the URI of the given resource and the given classloader. If the resource is not found it will be tried again with/without a leading "/" and with the parent classloader.
      Parameters:
      name - resource name (e.g. "log4j.properties")
      Returns:
      URI of the given resource (or null if resource was not found)
    • isLoaded Link icon

      public boolean isLoaded(String classname)
      Checks if the given classname is loaded. Why does we use not Class as parameter here? If you would allow a parameter of type "Class" this class will be problably loaded before and this method will return always true!
      Parameters:
      classname - name of the class
      Returns:
      true if class is loaded
    • toString Link icon

      public String toString()
      Puts also the classloader in the toString representation.
      Overrides:
      toString in class Object
      Returns:
      string containing the class laoder
      See Also:
    • getLoadedClasses Link icon

      public Set<Class<?>> getLoadedClasses()
      Returns a list of classes which were loaded by the given classloader.

      Ok, we must do some hacks here: there is an undocumented attribute "classes" which contains the loaded classes.

      HANDLE WITH CARE (it's a hack and it depends on the used classloader)

      Returns:
      set of classes
    • getLoadedResources Link icon

      public List<String> getLoadedResources()
      Asks the classloader for the resources which it already loaded - either directly or because the corresponding package was already loaded.
      Returns:
      the loaded resources
      Since:
      1.6.3 (15.05.2016)
    • getClasses Link icon

      public Set<String> getClasses()
      Digs into the classpath and returns the found classes.

      NOTE: This logic was formerly part of the createClasspathSet(..) method of the ClasspathMonitor class.

      Returns:
      classes of the classpath
      Since:
      1.7.2