Class ModuleImpl

  • All Implemented Interfaces:
    Module

    public final class ModuleImpl
    extends java.lang.Object
    implements Module
    A module represents a set of resources accessible to third party modules. Each module has a module definition which defines its name, its list of exported resources and its dependencies to other modules. A module instance stores references to the class loader instances giving access to the module's implementation. A module instance belongs to a ModuleRegistry which can be used to get the list of available modules and/or get particular module implementation. Modules can only satisfy their dependencies within the ModuleRegistry instance they are registered in.
    • Field Detail

      • serviceClasses

        private final java.util.Map<java.lang.String,​java.util.List<java.lang.Class>> serviceClasses
        Lazily loaded provider Classes from ModuleMetadata. The key is the service class name. We can't use Class because that would cause leaks.
      • dependencies

        private final java.util.List<ModuleImpl> dependencies
      • lastModifieds

        private final java.util.HashMap<java.lang.String,​java.lang.Long> lastModifieds
      • shared

        private boolean shared
      • sticky

        private boolean sticky
    • Method Detail

      • getClassLoader

        public ClassLoaderFacade getClassLoader()
        Return the ClassLoader instance associated with this module. Only designated public interfaces will be loaded and returned by this classloader.
        Specified by:
        getClassLoader in interface Module
        Returns:
        the public ClassLoader
      • getPrivateClassLoader

        ModuleClassLoader getPrivateClassLoader()
        Return the private class loader for this module. This class loader will be loading all the classes which are not explicitely exported in the module definition
        Returns:
        the private ClassLoader instance
      • getModuleDefinition

        public ModuleDefinition getModuleDefinition()
        Returns the module definition for this module instance
        Specified by:
        getModuleDefinition in interface Module
        Returns:
        the module definition
      • getRegistry

        public ModulesRegistryImpl getRegistry()
        Returns the registry owning this module
        Specified by:
        getRegistry in interface Module
        Returns:
        the registry owning the module
      • detach

        public void detach()
        Detach this module from its registry. This does not free any of the loaded resources. Only proper release of all references to the public class loader will ensure module being garbage collected. Detached modules are orphan and will be garbage collected if resources are properly disposed.
        Specified by:
        detach in interface Module
      • toString

        public java.lang.String toString()
        Return a String representation
        Overrides:
        toString in class java.lang.Object
        Returns:
        a descriptive String about myself
      • addListener

        public void addListener​(ModuleChangeListener listener)
        Add a new module change listener
        Specified by:
        addListener in interface Module
        Parameters:
        listener - the listener
      • removeListener

        public void removeListener​(ModuleChangeListener listener)
        Unregister a module change listener
        Specified by:
        removeListener in interface Module
        Parameters:
        listener - the listener to unregister
      • fireChangeEvent

        protected void fireChangeEvent()
        fires a ModuleChange event to all listeners
      • refresh

        public void refresh()
        Trigger manual refresh mechanism, the module will check all its URLs and generate change events if any of them has changed. This will allow the owning registry to force a module upgrade at next module request.
        Specified by:
        refresh in interface Module
      • parseInhabitants

        java.util.List<ActiveDescriptor> parseInhabitants​(java.lang.String name,
                                                          java.util.List<PopulatorPostProcessor> postProcessors)
                                                   throws java.io.IOException
        Parses all the inhabitants descriptors of the given name in this module.
        Returns:
        Throws:
        java.io.IOException
      • resolve

        public void resolve()
                     throws ResolveError
        Ensure that this module is resolved.

        If the module is already resolved, this method does nothing. Otherwise, iterate over all declared ModuleDependency instances and use the associated ModuleRegistry to resolve it. After successful completion of this method, the module state is ModuleState.RESOLVED.

        Specified by:
        resolve in interface Module
        Throws:
        ResolveError - if any of the declared dependency of this module cannot be satisfied
      • start

        public void start()
                   throws ResolveError
        Forces module startup. In most cases, the runtime will take care of starting modules when they are first used. There could be cases where code need to manually start a sub module. Invoking this method will move the module to the ModuleState.READY, the Lifecycle.start method will be invoked.
        Specified by:
        start in interface Module
        Throws:
        ResolveError
      • stop

        public boolean stop()
        Forces module stop. In most cases, the runtime will take care of stopping modules when the last module user released its interest. However, in certain cases, it may be interesting to manually stop the module. Stopping the module means that the module is removed from the registry, the class loader references are released (note : the class loaders will only be released if all instances of any class loaded by them are gc'ed). If a LifecyclePolicy for this module is defined, the Lifecycle.stop(Module) method will be called and finally the module state will be returned to ModuleState.NEW.
        Specified by:
        stop in interface Module
        Returns:
        true if unloading was successful
      • getImports

        public java.util.List<Module> getImports()
        Returns the list of imported modules
        Specified by:
        getImports in interface Module
        Returns:
        the list of imported modules
      • addImport

        public Module addImport​(ModuleDependency dependency)
        Create and add a new module to this module's list of imports.
        Specified by:
        addImport in interface Module
        Parameters:
        dependency - new module's definition
      • getState

        public ModuleState getState()
        Returns the module's state
        Specified by:
        getState in interface Module
        Returns:
        the module's state
      • removeImport

        public void removeImport​(ModuleImpl module)
      • getName

        public java.lang.String getName()
        Short-cut for getModuleDefinition().getName().
        Specified by:
        getName in interface Module
      • isShared

        public boolean isShared()
        Returns true if this module is sharable. A sharable module means that onlu one instance of the module classloader will be used by all users.
        Specified by:
        isShared in interface Module
        Returns:
        true if this module is sharable.
      • setShared

        void setShared​(boolean sharable)
        Sets the sharable flag. Setting the flag to false means that the moodule class loader should not be shared among module owners.
        Parameters:
        sharable - set to true to share the module
      • isSticky

        public boolean isSticky()
        Returns true if the module is sticky. A sticky module cannot be stopped or unloaded. Once a sticky module is loaded or started, it will stay in the JVM until it exists.
        Specified by:
        isSticky in interface Module
        Returns:
        true is the module is sticky
      • setSticky

        public void setSticky​(boolean sticky)
        Sets the sticky flag.
        Specified by:
        setSticky in interface Module
        Parameters:
        sticky - true if the module should stick around
      • getProvidersClass

        public <T> java.lang.Iterable<java.lang.Class<? extends T>> getProvidersClass​(java.lang.Class<T> serviceClass)
        Specified by:
        getProvidersClass in interface Module
      • getProvidersClass

        public java.lang.Iterable<java.lang.Class> getProvidersClass​(java.lang.String name)
        Specified by:
        getProvidersClass in interface Module
      • hasProvider

        public boolean hasProvider​(java.lang.Class serviceClass)
        Returns true if this module has any provider for the given service class.
        Specified by:
        hasProvider in interface Module
      • dumpState

        public void dumpState​(java.io.PrintStream writer)
        Specified by:
        dumpState in interface Module
      • uninstall

        public void uninstall()
        Description copied from interface: Module
        Removes the module from the registry backing store, the module will need be reinstalled to be loaded.
        Specified by:
        uninstall in interface Module