patterntesting.runtime.monitor
Class ClasspathDigger

Package class diagram package ClasspathDigger
java.lang.Object
  extended by patterntesting.runtime.monitor.ClasspathDigger

public final class ClasspathDigger
extends Object

This helper class digs into found 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. If you want to support an unknown classloader you can subclass this class and together with ClasspathMonitor.

Since:
15.05.2009
Version:
$Revision: 1.11 $
Author:
oliver

Field Summary
protected static ObjectName AGENT_MBEAN
          The ClasspathAgent as MBean.
 
Constructor Summary
ClasspathDigger()
          Instantiates a new classpath digger.
ClasspathDigger(ClassLoader cloader)
          Instantiates a new classpath digger.
 
Method Summary
 String[] getBootClasspath()
          To get the boot classpath the sytem property "sun.boot.class.path" is used to get them.
 ClassLoader getClassLoader()
          Gets the class loader.
 String[] getClasspath()
          We can use the system property "java.class.path" to get the classpath.
protected static String[] getClasspath(String key)
          Gets the classpath.
protected  String[] getClasspathFromPackages()
          Here we use the loaded packages to calculate the classpath.
 List<Class<?>> getLoadedClassList()
          Returns a list of classes which were loaded by the given classloader.
protected  List<Class<?>> getLoadedClassListFromAgent()
          Gets the loaded class list from patterntesting-agent.
 Package[] getLoadedPackageArray()
          Returns the packages which were loaded by the classloader.
static boolean isAgentAvailable()
          Checks if the ClasspathAgent is available as MBean.
 boolean isClassloaderSupported()
          Checks if is classloader supported.
 boolean isLoaded(String classname)
          Checks if the given classname is loaded.
 String toString()
          Puts also the classloader in the toString representation.
 URI whichResource(String name)
          Returns the URI of the given resource and the given classloader.
static URI whichResource(String name, ClassLoader cloader)
          Returns the URI of the given resource and the given classloader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

AGENT_MBEAN

protected static final ObjectName AGENT_MBEAN
The ClasspathAgent as MBean.

Constructor Detail

ClasspathDigger

public ClasspathDigger()
Instantiates a new classpath digger.


ClasspathDigger

public ClasspathDigger(ClassLoader cloader)
Instantiates a new classpath digger.

Parameters:
cloader - the cloader
Since:
1.2
Method Detail

getClassLoader

public ClassLoader getClassLoader()
Gets the class loader.

Returns:
the classLoader

isClassloaderSupported

public boolean isClassloaderSupported()
Checks if is classloader supported.

Returns:
true, if is classloader supported

isAgentAvailable

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

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

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 enginle (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

protected static String[] getClasspath(String key)
Gets the classpath.

Parameters:
key - the key
Returns:
the classpath as String array

getLoadedPackageArray

public Package[] getLoadedPackageArray()
Returns the packages which were loaded by the classloader.
Unfortunately ClassLoader.getPackages() is protected - ok, let's do the hard way using reflexion.

Returns:
array with the loaded packages

getClasspathFromPackages

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

whichResource

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)

whichResource

public static URI whichResource(String name,
                                ClassLoader cloader)
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")
cloader - class loader
Returns:
URI of the given resource (or null if resource was not found)

isLoaded

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

public String toString()
Puts also the classloader in the toString representation.

Overrides:
toString in class Object
Returns:
string containing the class laoder
See Also:
Object.toString()

getLoadedClassList

public List<Class<?>> getLoadedClassList()
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:
list of classes

getLoadedClassListFromAgent

protected List<Class<?>> getLoadedClassListFromAgent()
Gets the loaded class list from patterntesting-agent. For this method you must start the Java VM with PatternTesting Agent as Java agent (java -javaagent:patterntesting-agent-1.x.x.jar ...) because this MBean is needed for the loaded classes.
This class is protected for test reason.

Returns:
the loaded class list from agent


Copyright © 2002–2014 PatternTesting Team. All rights reserved.