packagemanager.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 578 行 · 第 1/2 页

JAVA
578
字号
        }        return RETURN_MODULE_UNKNOWN;    }    /**     * A utility method that collects all failed modules. Such an module caused     * an error while being loaded, and is now cached in case it is referenced     * elsewhere.     *     * @param state the failed module.     */    private void dropFailedModule(final PackageState state) {        if (this.modules.contains(state) == false) {            this.modules.add(state);        }    }    /**     * Tries to load a given module and all dependent modules. If the dependency check     * fails for that module (or for one of the dependent modules), the loaded modules     * are discarded and no action is taken.     *     * @param moduleInfo        the module info of the module that should be loaded.     * @param incompleteModules a list of incompletly loaded modules. This are module     *                          specifications which depend on the current module and wait for the module to     *                          be completly loaded.     * @param modules           the list of previously loaded modules for this module.     * @param fatal             a flag that states, whether the failure of loading a module should     *                          be considered an error. Root-modules load errors are never fatal, as we try     *                          to load all known modules, regardless whether they are active or not.     * @return true, if the module was loaded successfully, false otherwise.     */    private boolean loadModule(final ModuleInfo moduleInfo, final ArrayList incompleteModules,                               final ArrayList modules, final boolean fatal) {        try {            final Class c = ObjectUtilities.getClassLoader(getClass()).loadClass(moduleInfo.getModuleClass());            final Module module = (Module) c.newInstance();            if (acceptVersion(moduleInfo, module) == false) {                // module conflict!                Log.warn("Module " + module.getName() + ": required version: " + moduleInfo +                    ", but found Version: \n" + module);                final PackageState state = new PackageState(module, PackageState.STATE_ERROR);                dropFailedModule(state);                return false;            }            final int moduleContained = containsModule(modules, module);            if (moduleContained == RETURN_MODULE_ERROR) {                // the module caused harm before ...                Log.debug("Indicated failure for module: " + module.getModuleClass());                final PackageState state = new PackageState(module, PackageState.STATE_ERROR);                dropFailedModule(state);                return false;            }            else if (moduleContained == RETURN_MODULE_UNKNOWN) {                if (incompleteModules.contains(module)) {                    // we assume that loading will continue ...                    Log.error(new Log.SimpleMessage                        ("Circular module reference: This module definition is invalid: ",                            module.getClass()));                    final PackageState state = new PackageState(module, PackageState.STATE_ERROR);                    dropFailedModule(state);                    return false;                }                incompleteModules.add(module);                final ModuleInfo[] required = module.getRequiredModules();                for (int i = 0; i < required.length; i++) {                    if (loadModule(required[i], incompleteModules, modules, true) == false) {                        Log.debug("Indicated failure for module: " + module.getModuleClass());                        final PackageState state = new PackageState(module, PackageState.STATE_ERROR);                        dropFailedModule(state);                        return false;                    }                }                final ModuleInfo[] optional = module.getOptionalModules();                for (int i = 0; i < optional.length; i++) {                    if (loadModule(optional[i], incompleteModules, modules, true) == false) {                        Log.debug(new Log.SimpleMessage("Optional module: ",                            optional[i].getModuleClass(), " was not loaded."));                    }                }                // maybe a dependent module defined the same base module ...                if (containsModule(modules, module) == RETURN_MODULE_UNKNOWN) {                    modules.add(module);                }                incompleteModules.remove(module);            }            return true;        }        catch (ClassNotFoundException cnfe) {            if (fatal) {                Log.warn(new Log.SimpleMessage                    ("Unresolved dependency for package: ", moduleInfo.getModuleClass()));            }            Log.debug(new Log.SimpleMessage("ClassNotFound: ", cnfe.getMessage()));            return false;        }        catch (Exception e) {            Log.warn(new Log.SimpleMessage("Exception while loading module: ", moduleInfo), e);            return false;        }    }    /**     * Checks, whether the given module meets the requirements defined in the module     * information.     *     * @param moduleRequirement the required module specification.     * @param module            the module that should be checked against the specification.     * @return true, if the module meets the given specifications, false otherwise.     */    private boolean acceptVersion(final ModuleInfo moduleRequirement, final Module module) {        if (moduleRequirement.getMajorVersion() == null) {            return true;        }        if (module.getMajorVersion() == null) {            Log.warn("Module " + module.getName() + " does not define a major version.");        }        else {            final int compare = acceptVersion(moduleRequirement.getMajorVersion(),                module.getMajorVersion());            if (compare > 0) {                return false;            }            else if (compare < 0) {                return true;            }        }        if (moduleRequirement.getMinorVersion() == null) {            return true;        }        if (module.getMinorVersion() == null) {            Log.warn("Module " + module.getName() + " does not define a minor version.");        }        else {            final int compare = acceptVersion(moduleRequirement.getMinorVersion(),                module.getMinorVersion());            if (compare > 0) {                return false;            }            else if (compare < 0) {                return true;            }        }        if (moduleRequirement.getPatchLevel() == null) {            return true;        }        if (module.getPatchLevel() == null) {            Log.debug("Module " + module.getName() + " does not define a patch level.");        }        else {            if (acceptVersion(moduleRequirement.getPatchLevel(),                module.getPatchLevel()) > 0) {                Log.debug("Did not accept patchlevel: " + moduleRequirement.getPatchLevel() + " - " +                    module.getPatchLevel());                return false;            }        }        return true;    }    /**     * Compare the version strings. If the strings have a different length,     * the shorter string is padded with spaces to make them compareable.     *     * @param modVer    the version string of the module     * @param depModVer the version string of the dependent or optional module     * @return 0, if the dependent module version is equal tothe module's required     *         version, a negative number if the dependent module is newer or a positive     *         number if the dependent module is older and does not fit.     */    private int acceptVersion(final String modVer, final String depModVer) {        final int mLength = Math.max(modVer.length(), depModVer.length());        final char[] modVerArray;        final char[] depVerArray;        if (modVer.length() > depModVer.length()) {            modVerArray = modVer.toCharArray();            depVerArray = new char[mLength];            final int delta = modVer.length() - depModVer.length();            Arrays.fill(depVerArray, 0, delta, ' ');            System.arraycopy(depVerArray, delta, depModVer.toCharArray(), 0, depModVer.length());        }        else if (modVer.length() < depModVer.length()) {            depVerArray = depModVer.toCharArray();            modVerArray = new char[mLength];            final char[] b1 = new char[mLength];            final int delta = depModVer.length() - modVer.length();            Arrays.fill(b1, 0, delta, ' ');            System.arraycopy(b1, delta, modVer.toCharArray(), 0, modVer.length());        }        else {            depVerArray = depModVer.toCharArray();            modVerArray = modVer.toCharArray();        }        return new String(modVerArray).compareTo(new String(depVerArray));    }    /**     * Returns the default package configuration. Private report configuration     * instances may be inserted here. These inserted configuration can never override     * the settings from this package configuration.     *     * @return the package configuration.     */    public PackageConfiguration getPackageConfiguration() {        return this.packageConfiguration;    }    /**     * Returns an array of the currently active modules. The module definition     * returned contain all known modules, including buggy and unconfigured     * instances.     *     * @return the modules.     */    public Module[] getAllModules() {        final Module[] mods = new Module[this.modules.size()];        for (int i = 0; i < this.modules.size(); i++) {            final PackageState state = (PackageState) this.modules.get(i);            mods[i] = state.getModule();        }        return mods;    }    /**     * Returns all active modules. This array does only contain modules     * which were successfully configured and initialized.     *     * @return the list of all active modules.     */    public Module[] getActiveModules() {        final ArrayList mods = new ArrayList();        for (int i = 0; i < this.modules.size(); i++) {            final PackageState state = (PackageState) this.modules.get(i);            if (state.getState() == PackageState.STATE_INITIALIZED) {                mods.add(state.getModule());            }        }        return (Module[]) mods.toArray(new Module[mods.size()]);    }    /**     * Prints the modules that are used.     *     * @param p the print stream.     */    public void printUsedModules(final PrintStream p) {        final Module[] allMods = getAllModules();        final ArrayList activeModules = new ArrayList();        final ArrayList failedModules = new ArrayList();        for (int i = 0; i < allMods.length; i++) {            if (isModuleAvailable(allMods[i])) {                activeModules.add(allMods[i]);            }            else {                failedModules.add(allMods[i]);            }        }        p.print("Active modules: ");        p.println(activeModules.size());        p.println("----------------------------------------------------------");        for (int i = 0; i < activeModules.size(); i++) {            final Module mod = (Module) activeModules.get(i);            p.print(new PadMessage(mod.getModuleClass(), 70));            p.print(" [");            p.print(mod.getSubSystem());            p.println("]");            p.print("  Version: ");            p.print(mod.getMajorVersion());            p.print("-");            p.print(mod.getMinorVersion());            p.print("-");            p.print(mod.getPatchLevel());            p.print(" Producer: ");            p.println(mod.getProducer());            p.print("  Description: ");            p.println(mod.getDescription());        }    }}

⌨️ 快捷键说明

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