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

📄 importsearchpolicy.java

📁 OSGI 的 源码实现,采用JAVA书写
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**     * Callback method for <tt>ModuleListener</tt>; this should not     * be called directly. This callback is used to re-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>. It then invalidates     * all modules that import from the reset module.    **/    public void moduleReset(ModuleEvent event)    {        // This will reset module meta-data.        moduleAdded(event);// TODO: Synchronization?        ModuleManager m_mgr = (ModuleManager) event.getSource();        List list = createImporterList(m_mgr, event.getModule());        for (int i = 0; (list != null) && (i < list.size()); i++)        {            Module module = (Module) list.get(i);            invalidate(                module, module.getAttributes(),                module.getResourceSources(), module.getLibrarySources());        }    }    /**     * Callback method for <tt>ModuleListener</tt>; this should not     * be called directly. Used to listen for module removal events     * in order to invalidate all the modules that import form the     * removed moduled.    **/    public void moduleRemoved(ModuleEvent event)    {// TODO: Synchronization?        ModuleManager m_mgr = (ModuleManager) event.getSource();        List list = createImporterList(m_mgr, event.getModule());        for (int i = 0; (list != null) && (i < list.size()); i++)        {            Module module = (Module) list.get(i);            invalidate(                module, module.getAttributes(),                module.getResourceSources(), module.getLibrarySources());        }    }    //    // Instance utility methods.    //    /**     * This utility method returns the module that exports the     * specified import identifier and version. This method uses the     * <tt>validate()</tt> method to find the exporting module and,     * as a result, relies on the compatibility and selection     * policies associated with this <tt>ImportSearchPolicy</tt>     * instance. If successful, the returned module is guaranteed     * to be validated. This method only needs to be used for more     * advanced purposes (i.e., check import availability dynamically,     * etc.) and need not be used under normal circumstances.     * @param identifier the identifier of the import to resolve.     * @param version the version of the import to resolve.     * @return the exporting module selected to resolve the specified     *         import target.    **/    public Module resolveImportTarget(Object identifier, Object version)    {        // Create a fake module that imports the specified target        // and then try to validate it so we can get the exporting        // module that is used to satisfy the import.        Object[] targetImport = { identifier, version, null };        Object[][] attrs = new Object[][] {            new Object[] { EXPORTS_ATTR, new Object[0][0] },            new Object[] { IMPORTS_ATTR, new Object[][] { targetImport } },            new Object[] { PROPAGATES_ATTR, new Object[0] },            new Object[] { VALID_ATTR, Boolean.FALSE}        };        Module fake = new Module(m_mgr, "resolve import", attrs, null, null);        try {            validate(fake);        } catch (ValidationException ex) {            // Ignore this.        }        return (Module) targetImport[RESOLVING_MODULE_IDX];    }    //    // Static utility methods.    //    private static final Object[][] m_emptyImports = new Object[0][0];    private static final Object[] m_emptyProp = new Object[0];    /**     * Utility method that returns the <tt>VALID_ATTR</tt> attribute for     * the specified module.     * @param module the module whose <tt>VALID_ATTR</tt> attribute is to     *        be retrieved.     * @return an instance of <tt>Boolean</tt>.    **/    public static Boolean getValidAttribute(Module module)    {        Object value = module.getAttribute(VALID_ATTR);        if (value != null)        {            return (Boolean) value;        }        return Boolean.FALSE;    }    /**     * Utility method that returns the <tt>IMPORTS_ATTR</tt> attribute for     * the specified module.     * @param module the module whose <tt>IMPORTS_ATTR</tt> attribute is to     *        be retrieved.     * @return an <tt>Object[][]</tt> value or <tt>null</tt>.    **/    public static Object[][] getImportsAttribute(Module module)    {        Object value = module.getAttribute(IMPORTS_ATTR);        if (value != null)        {            return (Object[][]) value;        }        return m_emptyImports;    }    /**     * Utility method that returns the <tt>EXPORTS_ATTR</tt> attribute for     * the specified module.     * @param module the module whose <tt>EXPORTS_ATTR</tt> attribute is to     *        be retrieved.     * @return an <tt>Object[][]</tt> value or <tt>null</tt>.    **/    public static Object[][] getExportsAttribute(Module module)    {        Object value = module.getAttribute(EXPORTS_ATTR);        if (value != null)        {            return (Object[][]) value;        }        return m_emptyImports;    }    /**     * Utility method that returns the <tt>IMPORTS_ATTR</tt> or the     * <tt>EXPORTS_ATTR</tt> attribute for the specified module.     * @param module the module whose <tt>IMPORTS_ATTR</tt> or     *        <tt>EXPORTS_ATTR</tt> attribute is to be retrieved.     * @param name either <tt>IMPORTS_ATTR</tt> or <tt>EXPORTS_ATTR</tt>     *        depending on which attribute should be retrieved.     * @return an <tt>Object[][]</tt> value or <tt>null</tt>.    **/    public static Object[][] getImportsOrExports(Module module, String name)    {        Object value = module.getAttribute(name);        if (value != null)        {            return (Object[][]) value;        }        return m_emptyImports;    }    /**     * Utility method that returns the <tt>PROPAGATES_ATTR</tt> attribute for     * the specified module.     * @param module the module whose <tt>PROPAGATES_ATTR</tt> attribute is to     *        be retrieved.     * @return an <tt>Object[]</tt> value or <tt>null</tt>.    **/    public static Object[] getPropagatesAttribute(Module module)    {        Object value = module.getAttribute(PROPAGATES_ATTR);        if (value != null)        {            return (Object[]) value;        }        return m_emptyProp;    }    /**     * Utility method to determine if the specified module imports a given     * import identifier, regardless of version. This method checks both     * imports and exports, since a module is assumed to import what it exports.     * @param module the module to check.     * @param identifier the import identifier to check.     * @return <tt>true</tt> if the module imports the specified     *         import identifier or <tt>false</tt> if it does not.    **/    public static boolean doesImport(Module module, Object identifier)    {        Object[][] imports = getImportsAttribute(module);        for (int i = 0; i < imports.length; i++)        {            if (imports[i][IDENTIFIER_IDX].equals(identifier))            {                return true;            }        }        imports = getExportsAttribute(module);        for (int i = 0; i < imports.length; i++)        {            if (imports[i][IDENTIFIER_IDX].equals(identifier))            {                return true;            }        }        return false;    }    /**     * Utility method to create a list of modules that import from     * the specified module.     * @param mgr the module manager that contains the module.     * @param module the module for which to create an importer list.     * @return a list of modules that import from the specified module     *         or <tt>null</tt>.    **/    public static List createImporterList(ModuleManager mgr, Module module)    {        List list = null;        Module[] modules = mgr.getModules();        for (int modIdx = 0; modIdx < modules.length; modIdx++)        {            Object[][] imports = getImportsAttribute(modules[modIdx]);            for (int impIdx = 0; impIdx < imports.length; impIdx++)            {                if (imports[impIdx][RESOLVING_MODULE_IDX] == module)                {                    if (list == null)                    {                        list = new ArrayList();                    }                    list.add(modules[modIdx]);                    break;                }            }        }        return list;    }    /**     * Utility method to get the import version number associated with a specific     * import identifier of the specified module.     * @param module the module to investigate.     * @param identifier the import identifier for which the version should     *        be retrieved.     * @return the version number object or <tt>null</tt>.    **/    public static Object getImportVersion(Module module, Object identifier)    {        Object[][] imports = getImportsAttribute(module);        for (int i = 0; i < imports.length; i++)        {            if (imports[i][IDENTIFIER_IDX].equals(identifier))            {                return imports[i][VERSION_IDX];            }        }        return null;    }    /**     * Utility method to get the export version number associated with a specific     * export identifier of the specified module.     * @param module the module to investigate.     * @param identifier the export identifier for which the version should     *        be retrieved.     * @return the version number object or <tt>null</tt>.    **/    public static Object getExportVersion(Module module, Object identifier)    {        Object[][] exports = getExportsAttribute(module);        for (int i = 0; i < exports.length; i++)        {            if (exports[i][IDENTIFIER_IDX].equals(identifier))            {                return exports[i][VERSION_IDX];            }        }        return null;    }    /**     * Utility method to get the resolving module of a specific import     * identifier for the specified module.     * @param module the module to investigate.     * @param identifier the import identifier for which the resolving     *        module should be retrieved.     * @return the resolving module or <tt>null</tt>.    **/    public static Module getImportResolvingModule(Module module, Object identifier)    {        Object[][] imports = getImportsAttribute(module);        for (int i = 0; i < imports.length; i++)        {            if (imports[i][IDENTIFIER_IDX].equals(identifier))            {                return (Module) imports[i][RESOLVING_MODULE_IDX];            }        }        return null;    }    /**     * Utility method to get the resolving module of a specific export     * identifier for the specified module.     * @param module the module to investigate.     * @param identifier the export identifier for which the resolving     *        module should be retrieved.     * @return the resolving module or <tt>null</tt>.    **/    public static Module getExportResolvingModule(Module module, Object identifier)    {        Object[][] exports = getExportsAttribute(module);        for (int i = 0; i < exports.length; i++)        {            if (exports[i][IDENTIFIER_IDX].equals(identifier))            {                return (Module) exports[i][RESOLVING_MODULE_IDX];            }        }        return null;    }}

⌨️ 快捷键说明

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