📄 modulemanager.java
字号:
* Resets a given module. In resetting a module, the module's associated * class loader is thrown away; it is the application's responsibility to * determine when and how that application code stops using classes (and * subsequent instances) from the class loader of the reset module. * This method allows the associated elements of the module (i.e., * attributes, resource sources, and library sources) to be changed also; * if these elements have not changed then they simply need to be passed * back in from the existing module. This method is useful in situations * where the underlying module needs to be changed at run time, such as * might be necessary if a module was updated. * </p> * <p> * The same effect could be achieved by first removing and then re-adding * a module, but with one subtle different. By removing and then re-adding * a module, a new module is created and, thus, all existing references * become invalid. By explicitly having this method, the <tt>ModuleManager</tt> * maintains the integrity of the module reference, which is more intuitive * in the case where an updated module is intended to be the same module, * only updated. * </p> * @param module the module reset. * @param attributes an array of key-value attribute pairs to * associate with the module. * @param resSources an array of <tt>ResourceSource</tt>s to associate * with the module. * @param libSources an array of <tt>LibrarySource</tt>s to associate * with the module. * @see org.ungoverned.moduleloader.Module * @see org.ungoverned.moduleloader.ResourceSource * @see org.ungoverned.moduleloader.LibrarySource **/ public void resetModule( Module module, Object[][] attributes, ResourceSource[] resSources, LibrarySource[] libSources) { // Use a synchronized block instead of synchronizing the // method, so we can fire our event outside of the block. synchronized (this) { module = (Module) m_moduleMap.get(module.getId()); if (module != null) { module.reset(attributes, resSources, libSources); } else { // Don't fire event. return; } } // Fire event here instead of inside synchronized block. fireModuleReset(module); } /** * <p> * Removes the specified module from the <tt>ModuleManager</tt>. Removing * a module only removed the module from the <tt>ModuleManager</tt>. It is * the application's responsibility to determine when and how application code * stop using classes (and subsequent instances) that were loaded from * the class loader of the removed module. * </p> * @param module the module to remove. **/ public void removeModule(Module module) { // Use a synchronized block instead of synchronizing the // method, so we can fire our event outside of the block. synchronized (this) { if (m_moduleMap.get(module.getId()) != null) { // Remove from data structures. m_moduleList.remove(module); m_moduleMap.remove(module.getId()); // Dispose of the module. module.dispose(); } else { // Don't fire event. return; } } // Fire event here instead of inside synchronized block. fireModuleRemoved(module); } /** * <p> * Adds a listener to the <tt>ModuleManager</tt> to listen for * module added, reset, and removed events. * </p> * @param l the <tt>ModuleListener</tt> to add. **/ public void addModuleListener(ModuleListener l) { // Verify listener. if (l == null) { throw new IllegalArgumentException("Listener is null"); } // Use the m_noListeners object as a lock. synchronized (m_noListeners) { // If we have no listeners, then just add the new listener. if (m_listeners == m_noListeners) { m_listeners = new ModuleListener[] { l }; } // Otherwise, we need to do some array copying. // Notice, the old array is always valid, so if // the dispatch thread is in the middle of a dispatch, // then it has a reference to the old listener array // and is not affected by the new value. else { ModuleListener[] newList = new ModuleListener[m_listeners.length + 1]; System.arraycopy(m_listeners, 0, newList, 0, m_listeners.length); newList[m_listeners.length] = l; m_listeners = newList; } } } /** * <p> * Removes a listener from the <tt>ModuleManager</tt>. * </p> * @param l the <tt>ModuleListener</tt> to remove. **/ public void removeModuleListener(ModuleListener l) { // Verify listener. if (l == null) { throw new IllegalArgumentException("Listener is null"); } // Use the m_noListeners object as a lock. synchronized (m_noListeners) { // Try to find the instance in our list. int idx = -1; for (int i = 0; i < m_listeners.length; i++) { if (m_listeners[i].equals(l)) { idx = i; break; } } // If we have the instance, then remove it. if (idx >= 0) { // If this is the last listener, then point to empty list. if (m_listeners.length == 1) { m_listeners = m_noListeners; } // Otherwise, we need to do some array copying. // Notice, the old array is always valid, so if // the dispatch thread is in the middle of a dispatch, // then it has a reference to the old listener array // and is not affected by the new value. else { ModuleListener[] newList = new ModuleListener[m_listeners.length - 1]; System.arraycopy(m_listeners, 0, newList, 0, idx); if (idx < newList.length) { System.arraycopy(m_listeners, idx + 1, newList, idx, newList.length - idx); } m_listeners = newList; } } } } /** * <p> * Fires an event indicating that the specified module was added * to the <tt>ModuleManager</tt>. * </p> * @param module the module that was added. **/ protected void fireModuleAdded(Module module) { // Event holder. ModuleEvent event = null; // Get a copy of the listener array, which is guaranteed // to not be null. ModuleListener[] listeners = m_listeners; // Loop through listeners and fire events. for (int i = 0; i < listeners.length; i++) { // Lazily create event. if (event == null) { event = new ModuleEvent(this, module); } listeners[i].moduleAdded(event); } } /** * <p> * Fires an event indicating that the specified module was reset. * </p> * @param module the module that was reset. **/ protected void fireModuleReset(Module module) { // Event holder. ModuleEvent event = null; // Get a copy of the listener array, which is guaranteed // to not be null. ModuleListener[] listeners = m_listeners; // Loop through listeners and fire events. for (int i = 0; i < listeners.length; i++) { // Lazily create event. if (event == null) { event = new ModuleEvent(this, module); } listeners[i].moduleReset(event); } } /** * <p> * Fires an event indicating that the specified module was removed * from the <tt>ModuleManager</tt>. * </p> * @param module the module that was removed. **/ protected void fireModuleRemoved(Module module) { // Event holder. ModuleEvent event = null; // Get a copy of the listener array, which is guaranteed // to not be null. ModuleListener[] listeners = m_listeners; // Loop through listeners and fire events. for (int i = 0; i < listeners.length; i++) { // Lazily create event. if (event == null) { event = new ModuleEvent(this, module); } listeners[i].moduleRemoved(event); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -