Package org.jboss.modules
Class ModuleLoader
- java.lang.Object
-
- org.jboss.modules.ModuleLoader
-
- Direct Known Subclasses:
ClassifyingModuleLoader,ClassPathModuleLoader,JarModuleLoader,LocalModuleLoader
public class ModuleLoader extends java.lang.ObjectA repository for modules, from which a module may be loaded by identifier. Module loaders may additionally delegate to one or more other module loaders.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classModuleLoader.FutureModuleprivate static interfaceModuleLoader.MBeanReg(package private) static classModuleLoader.MXBeanImplprivate static classModuleLoader.RealMBeanRegprivate static classModuleLoader.TempMBeanReg
-
Field Summary
Fields Modifier and Type Field Description private booleancanRedefineprivate java.util.concurrent.atomic.AtomicIntegerclassCountprivate java.util.concurrent.atomic.AtomicLongclassLoadTimeprivate ModuleFinder[]findersprivate java.util.concurrent.atomic.AtomicLonglinkTimeprivate java.util.concurrent.atomic.AtomicLongloadTimeprivate static java.lang.RuntimePermissionML_PERMprivate static java.lang.RuntimePermissionMODULE_ITERATE_PERMprivate static java.lang.RuntimePermissionMODULE_REDEFINE_ANY_PERMprivate static java.lang.RuntimePermissionMODULE_REDEFINE_PERMprivate static java.lang.RuntimePermissionMODULE_UNLOAD_ANY_PERMprivate java.util.concurrent.ConcurrentMap<ModuleIdentifier,ModuleLoader.FutureModule>moduleMapprivate ModuleLoaderMXBeanmxBeanstatic ModuleFinder[]NO_FINDERSA constant representing zero module finders.private java.util.concurrent.atomic.AtomicIntegerraceCountprivate static Reaper<ModuleLoader,javax.management.ObjectName>reaperprivate static ModuleLoader.MBeanRegREG_REFprivate java.util.concurrent.atomic.AtomicIntegerscanCountprivate static java.util.concurrent.atomic.AtomicIntegerSEQ
-
Constructor Summary
Constructors Modifier Constructor Description protectedModuleLoader()Construct a new instance.(package private)ModuleLoader(boolean canRedefine, boolean skipRegister)(package private)ModuleLoader(boolean canRedefine, boolean skipRegister, ModuleFinder[] finders)ModuleLoader(ModuleFinder[] finders)Construct a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddClassLoadTime(long time)(package private) voidaddLinkTime(long amount)private static booleancheckPermissions()private ModuledefineModule(ConcreteModuleSpec moduleSpec, ModuleLoader.FutureModule futureModule)Defines a Module based on a specification.protected ModulefindLoadedModuleLocal(ModuleIdentifier identifier)Find an already-loaded module, returningnullif the module isn't currently loaded.protected ModuleSpecfindModule(ModuleIdentifier moduleIdentifier)Find a Module's specification in this ModuleLoader by its identifier.static ModuleLoaderforClass(java.lang.Class<?> clazz)Get the module loader for a class.static ModuleLoaderforClassLoader(java.lang.ClassLoader classLoader)Get the module loader for a class loader.protected DependencySpec[]getDependencies(Module module)Get the current dependency list for a module which was defined by this module loader, without any access checks.protected ModuleFinder[]getFinders()Get the module finders configured for this module loader.(package private) voidincClassCount()(package private) voidincRaceCount()(package private) voidincScanCount()(package private) static voidinstallMBeanServer()java.util.Iterator<ModuleIdentifier>iterateModules(ModuleIdentifier baseIdentifier, boolean recursive)Iterate the modules which can be located via this module loader.ModuleloadModule(ModuleIdentifier identifier)Load a module based on an identifier.protected ModuleloadModuleLocal(ModuleIdentifier identifier)Try to load a module from this module loader.protected ModulepreloadExportedModule(ModuleIdentifier identifier)Preload an "exported" module based on an identifier.protected ModulepreloadModule(ModuleIdentifier identifier)Preload a module based on an identifier.protected static ModulepreloadModule(ModuleIdentifier identifier, ModuleLoader moduleLoader)Utility method to delegate to another module loader, accessible from subclasses.protected voidrefreshResourceLoaders(Module module)Refreshes the paths provided by resource loaders associated with the specified Module.protected voidrelink(Module module)Relinks the dependencies associated with the specified Module.private static ModuleFinder[]safeClone(ModuleFinder[] finders)protected voidsetAndRefreshResourceLoaders(Module module, java.util.Collection<ResourceLoaderSpec> loaders)Replaces the resources loaders for the specified module and refreshes the internal path list that is derived from the loaders.protected voidsetAndRelinkDependencies(Module module, java.util.List<DependencySpec> dependencies)Replaces the dependencies for the specified module and relinks against the new modules This is an advanced method that should be used carefully, since it alters a live module.java.lang.StringtoString()Get the string representation of this module loader.protected voidunloadModuleLocal(Module module)Unload a module from this module loader.
-
-
-
Field Detail
-
ML_PERM
private static final java.lang.RuntimePermission ML_PERM
-
MODULE_REDEFINE_PERM
private static final java.lang.RuntimePermission MODULE_REDEFINE_PERM
-
MODULE_REDEFINE_ANY_PERM
private static final java.lang.RuntimePermission MODULE_REDEFINE_ANY_PERM
-
MODULE_UNLOAD_ANY_PERM
private static final java.lang.RuntimePermission MODULE_UNLOAD_ANY_PERM
-
MODULE_ITERATE_PERM
private static final java.lang.RuntimePermission MODULE_ITERATE_PERM
-
SEQ
private static final java.util.concurrent.atomic.AtomicInteger SEQ
-
REG_REF
private static volatile ModuleLoader.MBeanReg REG_REF
-
NO_FINDERS
public static final ModuleFinder[] NO_FINDERS
A constant representing zero module finders.
-
moduleMap
private final java.util.concurrent.ConcurrentMap<ModuleIdentifier,ModuleLoader.FutureModule> moduleMap
-
finders
private final ModuleFinder[] finders
-
canRedefine
private final boolean canRedefine
-
mxBean
private final ModuleLoaderMXBean mxBean
-
linkTime
private final java.util.concurrent.atomic.AtomicLong linkTime
-
loadTime
private final java.util.concurrent.atomic.AtomicLong loadTime
-
classLoadTime
private final java.util.concurrent.atomic.AtomicLong classLoadTime
-
scanCount
private final java.util.concurrent.atomic.AtomicInteger scanCount
-
raceCount
private final java.util.concurrent.atomic.AtomicInteger raceCount
-
classCount
private final java.util.concurrent.atomic.AtomicInteger classCount
-
reaper
private static final Reaper<ModuleLoader,javax.management.ObjectName> reaper
-
-
Constructor Detail
-
ModuleLoader
ModuleLoader(boolean canRedefine, boolean skipRegister)
-
ModuleLoader
ModuleLoader(boolean canRedefine, boolean skipRegister, ModuleFinder[] finders)
-
ModuleLoader
protected ModuleLoader()
Construct a new instance.
-
ModuleLoader
public ModuleLoader(ModuleFinder[] finders)
Construct a new instance.- Parameters:
finders- the module finders to search, in order
-
-
Method Detail
-
safeClone
private static ModuleFinder[] safeClone(ModuleFinder[] finders)
-
checkPermissions
private static boolean checkPermissions()
-
forClass
public static ModuleLoader forClass(java.lang.Class<?> clazz)
Get the module loader for a class.- Parameters:
clazz- the class- Returns:
- the module loader or
nullif the class's class loader does not belong to a module loader.
-
forClassLoader
public static ModuleLoader forClassLoader(java.lang.ClassLoader classLoader)
Get the module loader for a class loader.- Parameters:
classLoader- the class loader- Returns:
- the module loader or
nullif the class loader does not belong to a module loader.
-
toString
public java.lang.String toString()
Get the string representation of this module loader.- Overrides:
toStringin classjava.lang.Object- Returns:
- the string representation
-
installMBeanServer
static void installMBeanServer()
-
loadModule
public final Module loadModule(ModuleIdentifier identifier) throws ModuleLoadException
Load a module based on an identifier. This method delegates topreloadModule(ModuleIdentifier)and then links the returned module if necessary.- Parameters:
identifier- The module identifier- Returns:
- The loaded Module
- Throws:
ModuleLoadException- if the Module can not be loaded
-
iterateModules
public final java.util.Iterator<ModuleIdentifier> iterateModules(ModuleIdentifier baseIdentifier, boolean recursive)
Iterate the modules which can be located via this module loader.- Parameters:
baseIdentifier- the identifier to start with, ornullto iterate all modulesrecursive-trueto find recursively nested modules,falseto only find immediately nested modules- Returns:
- an iterator for the modules in this module finder
- Throws:
java.lang.SecurityException- if the caller does not have permission to iterate module loaders
-
preloadModule
protected Module preloadModule(ModuleIdentifier identifier) throws ModuleLoadException
Preload a module based on an identifier. By default, no delegation is done and this method simply invokesloadModuleLocal(ModuleIdentifier). A delegating module loader may delegate to the appropriate module loader based on loader-specific criteria (via thepreloadModule(ModuleIdentifier, ModuleLoader)method).- Parameters:
identifier- the module identifier- Returns:
- the load result, or
nullif the module is not found - Throws:
ModuleLoadException- if an error occurs
-
preloadExportedModule
protected Module preloadExportedModule(ModuleIdentifier identifier) throws ModuleLoadException
Preload an "exported" module based on an identifier. By default this simply delegates topreloadModule(ModuleIdentifier).- Parameters:
identifier- the module identifier- Returns:
- the load result, or
nullif the module is not found - Throws:
ModuleLoadException- if an error occurs
-
preloadModule
protected static Module preloadModule(ModuleIdentifier identifier, ModuleLoader moduleLoader) throws ModuleLoadException
Utility method to delegate to another module loader, accessible from subclasses. The delegate module loader will be queried for exported modules only.- Parameters:
identifier- the module identifiermoduleLoader- the module loader to delegate to- Returns:
- the delegation result
- Throws:
ModuleLoadException- if an error occurs
-
loadModuleLocal
protected final Module loadModuleLocal(ModuleIdentifier identifier) throws ModuleLoadException
Try to load a module from this module loader. Returnsnullif the module is not found. The returned module may not yet be resolved. The returned module may have a different name than the given identifier if the identifier is an alias for another module.- Parameters:
identifier- the module identifier- Returns:
- the module
- Throws:
ModuleLoadException- if an error occurs while loading the module
-
findLoadedModuleLocal
protected final Module findLoadedModuleLocal(ModuleIdentifier identifier)
Find an already-loaded module, returningnullif the module isn't currently loaded. May block while the loaded state of the module is in question (if the module is being concurrently loaded from another thread, for example).- Parameters:
identifier- the module identifier- Returns:
- the module, or
nullif it wasn't found
-
unloadModuleLocal
protected final void unloadModuleLocal(Module module) throws java.lang.SecurityException
Unload a module from this module loader. Note that this has no effect on existing modules which refer to the module being unloaded. Also, only modules from the current module loader can be unloaded. Unloading the same module more than once has no additional effect. This method only removes the mapping for the module; any running threads which are currently accessing or linked to the module will continue to function, however attempts to load this module will fail until a new module is loaded with the same name. Once this happens, if all references to the previous module are not cleared, the same module may be loaded more than once, causing possible class duplication and class cast exceptions if proper care is not taken.- Parameters:
module- the module to unload- Throws:
java.lang.SecurityException- if an attempt is made to unload a module which does not belong to this module loaderjava.lang.SecurityException- if the module was not defined by this module loader
-
findModule
protected ModuleSpec findModule(ModuleIdentifier moduleIdentifier) throws ModuleLoadException
Find a Module's specification in this ModuleLoader by its identifier. This can be overriden by sub-classes to implement the Module loading strategy for this loader. The default implementation iterates the module finders provided during construction. If no module is found in this module loader with the given identifier, then this method should returnnull. If the module is found but some problem occurred (for example, a transitive dependency failed to load) then this method should throw aModuleLoadExceptionof the relevant type.- Parameters:
moduleIdentifier- the module identifier- Returns:
- the module specification, or
nullif no module is found with the given identifier - Throws:
ModuleLoadException- if any problems occur finding the module
-
getFinders
protected final ModuleFinder[] getFinders()
Get the module finders configured for this module loader.- Returns:
- the module finders
-
defineModule
private Module defineModule(ConcreteModuleSpec moduleSpec, ModuleLoader.FutureModule futureModule) throws ModuleLoadException
Defines a Module based on a specification. May only be called fromloadModuleLocal(ModuleIdentifier).- Parameters:
moduleSpec- The module specification to create the Module fromfutureModule- the future module to populate- Returns:
- The defined Module
- Throws:
ModuleLoadException- If any dependent modules can not be loaded
-
refreshResourceLoaders
protected void refreshResourceLoaders(Module module)
Refreshes the paths provided by resource loaders associated with the specified Module. This is an advanced method that is intended to be called on modules that have a resource loader implementation that has changed and is returning different paths.- Parameters:
module- the module to refresh- Throws:
java.lang.SecurityException- if the module was not defined by this module loader, or if the module loader does not have the required permissions associated with it
-
setAndRefreshResourceLoaders
protected void setAndRefreshResourceLoaders(Module module, java.util.Collection<ResourceLoaderSpec> loaders)
Replaces the resources loaders for the specified module and refreshes the internal path list that is derived from the loaders. This is an advanced method that should be used carefully, since it alters a live module. Modules that import resources from the specified module will not automatically be updated to reflect the change. For this to occurrelink(Module)must be called on all of them.- Parameters:
module- the module to update and refreshloaders- the new collection of loaders the module should use- Throws:
java.lang.SecurityException- if the module was not defined by this module loader, or if the module loader does not have the required permissions associated with it
-
relink
protected void relink(Module module) throws ModuleLoadException
Relinks the dependencies associated with the specified Module. This is an advanced method that is intended to be called on all modules that directly or indirectly import dependencies that are re-exported by a module that has recently been updated and relinked viasetAndRelinkDependencies(Module, java.util.List).- Parameters:
module- the module to relink- Throws:
ModuleLoadException- if relinking failedjava.lang.SecurityException- if the module was not defined by this module loader, or if the module loader does not have the required permissions associated with it
-
setAndRelinkDependencies
protected void setAndRelinkDependencies(Module module, java.util.List<DependencySpec> dependencies) throws ModuleLoadException
Replaces the dependencies for the specified module and relinks against the new modules This is an advanced method that should be used carefully, since it alters a live module. Modules that import dependencies that are re-exported from the specified module will not automatically be updated to reflect the change. For this to occurrelink(Module)must be called on all of them.- Parameters:
module- the module to update and relinkdependencies- the new dependency list- Throws:
ModuleLoadException- if relinking failedjava.lang.SecurityException- if the module was not defined by this module loader, or if the module loader does not have the required permissions associated with it
-
getDependencies
protected DependencySpec[] getDependencies(Module module)
Get the current dependency list for a module which was defined by this module loader, without any access checks.- Returns:
- the current dependency list for the module
- Throws:
java.lang.SecurityException- if the module was not defined by this module loader
-
addLinkTime
void addLinkTime(long amount)
-
addClassLoadTime
void addClassLoadTime(long time)
-
incScanCount
void incScanCount()
-
incRaceCount
void incRaceCount()
-
incClassCount
void incClassCount()
-
-