⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 importsearchpolicy.java

📁 OSGI 的 源码实现,采用JAVA书写
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                            // see if it is propagating the import target from the same                            // module as previously determined for this module. If not,                            // then this is a propagation conflict.                            else if (sourceModule !=                                getImportResolvingModule(                                    exportingModule, propagates[propIdx]))                            {                                isValid = false;                                invalidException =                                    new ValidationException(                                        "Unable to validate module",                                        exportingModule,                                        propagates[propIdx],                                        null,                                        true);                                break;                            }                        }                    }                    // Set the chosen exporting module for the module                    // being validated.                    imports[impIdx][RESOLVING_MODULE_IDX] = exportingModule;                }            }            // Since this method is recursive, check to see it we are            // back at the root module that started the request, which            // would indicate that the request is finished.            if (m_rootModule == module)            {                // If the result is valid, then we have validated successfully.                if (isValid)                {                    // Loop through all modules in validate map                    // and mark them as valid.                    Iterator iter = m_validateMap.keySet().iterator();                    while (iter.hasNext())                    {                        Module m = (Module) iter.next();                        if (!getValidAttribute(m).booleanValue())                        {                            m.setAttribute(VALID_ATTR, Boolean.TRUE);                            if (fireValidatedList == null)                            {                                fireValidatedList = new ArrayList();                            }                            fireValidatedList.add(m);                        }                    }                }                // If we are here, then the validate failed, so we                // need to reset any partially validated modules.                else                {                    Iterator iter = m_validateMap.keySet().iterator();                    while (iter.hasNext())                    {                        Module m = (Module) iter.next();                        invalidate(                            m,                            m.getAttributes(),                            m.getResourceSources(),                            m.getLibrarySources());                    }                }                // Clear the root module and validation map                // before leaving the synchronized block.                m_rootModule = null;                m_validateMap.clear();            }        }        // (Re)throw the exception if invalid, otherwise        // fire validation events if the validated event        // list is not null.        if (!isValid)        {            throw invalidException;        }        else if (fireValidatedList != null)        {            for (int i = 0; i < fireValidatedList.size(); i++)            {                fireModuleValidated((Module) fireValidatedList.get(i));            }        }    }    /**     * This method returns a list of modules that have an export     * that is compatible with the given import identifier and version.     * @param identifier the import identifier.     * @param version the version of the import identifier.     * @return an array of modules that have compatible exports or <tt>null</tt>     *         if none are found.    **/    protected Module[] getCompatibleModules(Object identifier, Object version)    {        List list = null;        Module[] modules = m_mgr.getModules();        for (int modIdx = 0; modIdx < modules.length; modIdx++)        {            Object[][] exports = getExportsAttribute(modules[modIdx]);            for (int expIdx = 0; expIdx < exports.length; expIdx++)            {                // If the identifiers are comparable and compatible,                // then add the export identifier to the list.                if (m_compatPolicy.isCompatible(                        exports[expIdx][IDENTIFIER_IDX], exports[expIdx][VERSION_IDX],                        identifier, version))                {                    if (list == null)                    {                        list = new ArrayList();                    }                    list.add(modules[modIdx]);                }            }        }        if (list == null)        {            return null;        }        Module[] result = new Module[list.size()];        return (Module[]) list.toArray(result);    }    /**     * Invalidates a module by flushing its class loader and     * re-initializing its meta-data values.     * @param module the module to be invalidated.     * @param attributes the attributes associated with the module, since they     *        might have changed.     * @param resSources the resource sources associated wih the module, since they     *        might have changed.     * @param libSources the library sources associated wih the module, since they     *        might have changed.    **/    public void invalidate(        Module module, Object[][] attributes,        ResourceSource[] resSources, LibrarySource[] libSources)    {        // Synchronize on the module manager, because we don't want        // anything to change while we are in the middle of this        // operation.        synchronized (m_mgr)        {            m_mgr.resetModule(module, attributes, resSources, libSources);        }        // Fire invalidation event if necessary.        fireModuleInvalidated(m_mgr.getModule(module.getId()));    }    //    // Event handling methods for validation events.    //    /**     * Adds a validation listener to this import search policy. Validation     * listeners are notified when a module is validated and/or invalidated     * by the search policy.     * @param l the validation listener to add.    **/    public void addValidationListener(ValidationListener 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 ValidationListener[] { 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            {                ValidationListener[] newList = new ValidationListener[m_listeners.length + 1];                System.arraycopy(m_listeners, 0, newList, 0, m_listeners.length);                newList[m_listeners.length] = l;                m_listeners = newList;            }        }    }    /**     * Removes a validation listener to this import search policy.     * @param l the validation listener to remove.    **/    public void removeValidationListener(ValidationListener 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                {                    ValidationListener[] newList = new ValidationListener[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;                }            }        }    }    /**     * Fires a validation event for the specified module.     * @param module the module that was validated.    **/    protected void fireModuleValidated(Module module)    {        // Event holder.        ModuleEvent event = null;        // Get a copy of the listener array, which is guaranteed        // to not be null.        ValidationListener[] 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(m_mgr, module);            }            listeners[i].moduleValidated(event);        }    }    /**     * Fires an invalidation event for the specified module.     * @param module the module that was invalidated.    **/    protected void fireModuleInvalidated(Module module)    {        // Event holder.        ModuleEvent event = null;        // Get a copy of the listener array, which is guaranteed        // to not be null.        ValidationListener[] 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(m_mgr, module);            }            listeners[i].moduleInvalidated(event);        }    }    //    // ModuleListener methods.    //    /**     * Callback method for <tt>ModuleListener</tt>; this should not     * be called directly. This callback is used to initialize module     * meta-data attributes; it adds the <tt>VALID_ATTR</tt> attribute     * and initializes the resolving module entries in <tt>EXPORTS_ATTR</tt>     * and <tt>IMPORTS_ATTR</tt> to <tt>null</tt>.    **/    public void moduleAdded(ModuleEvent event)    {        synchronized (event.getModule())        {            // Add valid attribute to all modules.            event.getModule().setAttribute(VALID_ATTR, Boolean.FALSE);            for (int attrIdx = 0; attrIdx < m_searchAttrs.length; attrIdx++)            {                Object[][] imports =                    getImportsOrExports(event.getModule(), m_searchAttrs[attrIdx]);                for (int i = 0; i < imports.length; i++)                {                    imports[i][RESOLVING_MODULE_IDX] = null;                }            }        }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -