org.rhq.core.pc.plugin
Class ClassLoaderManager

java.lang.Object
  extended by org.rhq.core.pc.plugin.ClassLoaderManager

public class ClassLoaderManager
extends Object

Manages the classloaders created and used by the plugin container and all plugins/resources.

Author:
John Mazzitelli

Constructor Summary
ClassLoaderManager(Map<String,URL> pluginNamesUrls, PluginDependencyGraph graph, ClassLoader rootClassLoader, File tmpDir, boolean createResourceClassLoaders)
          Creates the object that will manage all classloaders for the plugins deployed in the given plugin deployment graph.
 
Method Summary
 void destroy()
          Cleans up this object and all classloaders it has created.
 Map<String,ClassLoader> getDiscoveryClassLoaders()
          Returns a shallow copy of the discovery classloaders keyed on a hash calculated from plugin name and parent classloader.
 int getNumberOfDiscoveryClassLoaders()
          Returns the total number of discovery classloaders that have been created and managed.
 int getNumberOfPluginClassLoaders()
          Returns the total number of plugin classloaders that have been created and managed.
 int getNumberOfResourceClassLoaders()
          Returns the total number of resource classloaders that have been created and managed.
 Map<String,ClassLoader> getPluginClassLoaders()
          Returns a shallow copy of the plugin classloaders keyed on plugin name.
 PluginDependencyGraph getPluginDependencyGraph()
          Returns the graph of all the plugins and their dependencies.
 Map<CanonicalResourceKey,ClassLoader> getResourceClassLoaders()
          Returns a shallow copy of the resource classloaders keyed on a canonical keys.
 ClassLoader getRootClassLoader()
          Returns the classloader that should be the ancestor (i.e.
 boolean isCreateResourceClassLoaders()
          Returns true if this manager will create instances of classloaders for those individual Resources that require it, or false if this manager will never create individual classloaders for Resources (i.e.
 ClassLoader obtainDiscoveryClassLoader(String pluginName, ClassLoader parentClassLoader)
          Similar to obtainPluginClassLoader(String), however, the classloader to be returned will have the given parent classloader (as opposed to having parent classloaders that follow the plugin dependency graph hierarchy).
 ClassLoader obtainPluginClassLoader(String pluginName)
          Returns a plugin classloader (creating it if necessary) that contains the plugin jar and whose parent classloader is that of the the classloader for the required (<depends>) plugin.
 ClassLoader obtainResourceClassLoader(org.rhq.core.domain.resource.Resource resource, ResourceContainer parent, List<URL> additionalJars)
          Returns the classloader that the given resource should use when being invoked.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassLoaderManager

public ClassLoaderManager(Map<String,URL> pluginNamesUrls,
                          PluginDependencyGraph graph,
                          ClassLoader rootClassLoader,
                          File tmpDir,
                          boolean createResourceClassLoaders)
Creates the object that will manage all classloaders for the plugins deployed in the given plugin deployment graph.

Parameters:
pluginNamesUrls - maps a plugin name with the URL to that plugin's jar file
graph - the graph that provides plugin dependency information for all plugins that are deployed
rootClassLoader - the classloader at the top of the classloader hierarchy to be used as the parent classloader for those classloaders that are not children to other shared/resource classloaders.
tmpDir - where the classloaders can write out the jars that are embedded in the plugin jars
createResourceClassLoaders - if true, the classloader manager will create resource classloader instances when appropriate. If false, this classloader manager will never create classloaders on a per-resource instance basis. It will only ever create and return plugin classloaders. This will be false when the plugin container is running embedded inside a managed resource and that managed resource will provide the necessary client jars via the root classloader.
Method Detail

destroy

public void destroy()
Cleans up this object and all classloaders it has created.


toString

public String toString()
Overrides:
toString in class Object

getRootClassLoader

public ClassLoader getRootClassLoader()
Returns the classloader that should be the ancestor (i.e. top most parent) of all plugin classloaders.

Returns:
the root plugin classloader for all plugins

getPluginDependencyGraph

public PluginDependencyGraph getPluginDependencyGraph()
Returns the graph of all the plugins and their dependencies.

Returns:
plugin dependency graph

obtainPluginClassLoader

public ClassLoader obtainPluginClassLoader(String pluginName)
                                    throws PluginContainerException
Returns a plugin classloader (creating it if necessary) that contains the plugin jar and whose parent classloader is that of the the classloader for the required (<depends>) plugin. In other words, this follows the plugin dependency hierarchy as defined in the given dependency graph.

Parameters:
pluginName - the plugin whose classloader is to be created
Returns:
the plugin classloader
Throws:
PluginContainerException

obtainDiscoveryClassLoader

public ClassLoader obtainDiscoveryClassLoader(String pluginName,
                                              ClassLoader parentClassLoader)
                                       throws PluginContainerException
Similar to obtainPluginClassLoader(String), however, the classloader to be returned will have the given parent classloader (as opposed to having parent classloaders that follow the plugin dependency graph hierarchy). This is used to support loading discovery components where those discovery components need to use connections to the parent resource in order to perform their discovery.

Parameters:
pluginName - the name of the plugin where the discovery component can be found
parentClassLoader - the parent classloader of the new classloader being created
Returns:
the new plugin classloader
Throws:
PluginContainerException

obtainResourceClassLoader

public ClassLoader obtainResourceClassLoader(org.rhq.core.domain.resource.Resource resource,
                                             ResourceContainer parent,
                                             List<URL> additionalJars)
                                      throws PluginContainerException
Returns the classloader that the given resource should use when being invoked. Note that the parent container should never be null; if it is, the caller should just assume the resource is the top-level platform and just get that platform resource's plugin classloader.

Parameters:
resource - the resource whose classloader is to be obtained
parent - the container for the parent of the given resource (must never be null)
additionalJars - additional jars to put into the classloader
Returns:
the resource's classloader - this will be newly created if this is the first time we've been asked to obtain it
Throws:
PluginContainerException

getNumberOfPluginClassLoaders

public int getNumberOfPluginClassLoaders()
Returns the total number of plugin classloaders that have been created and managed. This method is here just to support the plugin container management MBean.

Returns:
number of plugin classloaders that are currently created and being used

getNumberOfDiscoveryClassLoaders

public int getNumberOfDiscoveryClassLoaders()
Returns the total number of discovery classloaders that have been created and managed. This method is here just to support the plugin container management MBean.

Returns:
number of discovery classloaders that are currently created and being used

getNumberOfResourceClassLoaders

public int getNumberOfResourceClassLoaders()
Returns the total number of resource classloaders that have been created and managed. This is the count of unique classloader instances that have been created - each resource classloader could potentially be assigned to multiple resources. This method is here just to support the plugin container management MBean.

Returns:
number of unique resource classloaders that are currently created and assigned to resources

getPluginClassLoaders

public Map<String,ClassLoader> getPluginClassLoaders()
Returns a shallow copy of the plugin classloaders keyed on plugin name. This method is here just to support the plugin container management MBean. Do not use this method to obtain a plugin's classloader, instead, you want to use obtainPluginClassLoader(String).

Returns:
all plugin classloaders currently assigned to plugins (will never be null)

getDiscoveryClassLoaders

public Map<String,ClassLoader> getDiscoveryClassLoaders()
Returns a shallow copy of the discovery classloaders keyed on a hash calculated from plugin name and parent classloader. This method is here just to support the plugin container management MBean. Do not use this method to obtain a discovery classloader, instead, you want to use obtainDiscoveryClassLoader(String, ClassLoader).

Returns:
all discovery classloaders currently created (will never be null)

getResourceClassLoaders

public Map<CanonicalResourceKey,ClassLoader> getResourceClassLoaders()
Returns a shallow copy of the resource classloaders keyed on a canonical keys. This method is here just to support the plugin container management MBean. Do not use this method to obtain a resource's classloader, instead, you want to use obtainResourceClassLoader(Resource, ResourceContainer, List).

Returns:
all resource classloaders currently assigned to resources (will never be null)

isCreateResourceClassLoaders

public boolean isCreateResourceClassLoaders()
Returns true if this manager will create instances of classloaders for those individual Resources that require it, or false if this manager will never create individual classloaders for Resources (i.e. obtainResourceClassLoader(Resource, ResourceContainer, List) will always just return plugin classloaders).

Returns:
true if this manager will create instances of classloaders for those individual Resources that require it, or false if this manager will never create individual classloaders for Resources (i.e. obtainResourceClassLoader(Resource, ResourceContainer, List) will always just return plugin classloaders)


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.