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

📄 cmsmodulemanager.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/module/CmsModuleManager.java,v $
 * Date   : $Date: 2007-09-06 13:53:28 $
 * Version: $Revision: 1.39 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.module;

import org.opencms.configuration.CmsConfigurationException;
import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.configuration.CmsModuleConfiguration;
import org.opencms.db.CmsExportPoint;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.importexport.CmsImportExportManager;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.CmsIllegalStateException;
import org.opencms.main.CmsLog;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsRoleViolationException;
import org.opencms.security.CmsSecurityException;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;

/**
 * Manages the modules of an OpenCms installation.<p>
 * 
 * @author Alexander Kandzior 
 * @author Michael Moossen
 * 
 * @version $Revision: 1.39 $ 
 * 
 * @since 6.0.0 
 */
public class CmsModuleManager {

    /** Indicates dependency check for module deletion. */
    public static final int DEPENDENCY_MODE_DELETE = 0;

    /** Indicates dependency check for module import. */
    public static final int DEPENDENCY_MODE_IMPORT = 1;

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsModuleManager.class);

    /** The list of module export points. */
    private Set m_moduleExportPoints;

    /** The map of configured modules. */
    private Map m_modules;

    /**
     * Basic constructor.<p>
     * 
     * @param configuredModules the list of configured modules 
     */
    public CmsModuleManager(List configuredModules) {

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_MOD_MANAGER_CREATED_0));
        }

        m_modules = new Hashtable();
        for (int i = 0; i < configuredModules.size(); i++) {
            CmsModule module = (CmsModule)configuredModules.get(i);
            m_modules.put(module.getName(), module);
            if (CmsLog.INIT.isInfoEnabled()) {
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_MOD_CONFIGURED_1, module.getName()));
            }
        }

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(
                Messages.INIT_NUM_MODS_CONFIGURED_1,
                new Integer(m_modules.size())));
        }
        m_moduleExportPoints = Collections.EMPTY_SET;
    }

    /**
     * Returns a map of dependencies.<p>
     * 
     * The module dependencies are get from the installed modules or
     * from the module manifest.xml files found in the given FRS path.<p>
     * 
     * Two types of dependency lists can be generated:<br>
     * <ul>
     *   <li>Forward dependency lists: a list of modules that depends on a module</li>
     *   <li>Backward dependency lists: a list of modules that a module depends on</li>
     * </ul>
     * 
     * @param rfsAbsPath a RFS absolute path to search for modules, or <code>null</code> to use the installed modules
     * @param mode if <code>true</code> a list of forward dependency is build, is not a list of backward dependency
     * 
     * @return a Map of module names as keys and a list of dependency names as values
     * 
     * @throws CmsConfigurationException if something goes wrong
     */
    public static Map buildDepsForAllModules(String rfsAbsPath, boolean mode) throws CmsConfigurationException {

        Map ret = new HashMap();
        List modules;
        if (rfsAbsPath == null) {
            modules = OpenCms.getModuleManager().getAllInstalledModules();
        } else {
            modules = new ArrayList(getAllModulesFromPath(rfsAbsPath).keySet());
        }
        Iterator itMods = modules.iterator();
        while (itMods.hasNext()) {
            CmsModule module = (CmsModule)itMods.next();

            // if module a depends on module b, and module c depends also on module b:
            // build a map with a list containing "a" and "c" keyed by "b" to get a 
            // list of modules depending on module "b"...
            Iterator itDeps = module.getDependencies().iterator();
            while (itDeps.hasNext()) {
                CmsModuleDependency dependency = (CmsModuleDependency)itDeps.next();
                // module dependency package name
                String moduleDependencyName = dependency.getName();

                if (mode) {
                    // get the list of dependend modules
                    List moduleDependencies = (List)ret.get(moduleDependencyName);
                    if (moduleDependencies == null) {
                        // build a new list if "b" has no dependend modules yet
                        moduleDependencies = new ArrayList();
                        ret.put(moduleDependencyName, moduleDependencies);
                    }
                    // add "a" as a module depending on "b"
                    moduleDependencies.add(module.getName());
                } else {
                    List moduleDependencies = (List)ret.get(module.getName());
                    if (moduleDependencies == null) {
                        moduleDependencies = new ArrayList();
                        ret.put(module.getName(), moduleDependencies);
                    }
                    moduleDependencies.add(dependency.getName());
                }
            }
        }
        itMods = modules.iterator();
        while (itMods.hasNext()) {
            CmsModule module = (CmsModule)itMods.next();
            if (ret.get(module.getName()) == null) {
                ret.put(module.getName(), new ArrayList());
            }
        }
        return ret;
    }

    /**
     * Returns a map of dependencies between the given modules.<p>
     * 
     * The module dependencies are get from the installed modules or
     * from the module manifest.xml files found in the given FRS path.<p>
     * 
     * Two types of dependency lists can be generated:<br>
     * <ul>
     *   <li>Forward dependency lists: a list of modules that depends on a module</li>
     *   <li>Backward dependency lists: a list of modules that a module depends on</li>
     * </ul>
     * 
     * @param moduleNames a list of module names
     * @param rfsAbsPath a RFS absolute path to search for modules, or <code>null</code> to use the installed modules
     * @param mode if <code>true</code> a list of forward dependency is build, is not a list of backward dependency
     * 
     * @return a Map of module names as keys and a list of dependency names as values
     * 
     * @throws CmsConfigurationException if something goes wrong
     */
    public static Map buildDepsForModulelist(List moduleNames, String rfsAbsPath, boolean mode)
    throws CmsConfigurationException {

        Map ret = buildDepsForAllModules(rfsAbsPath, mode);
        Iterator itMods;
        if (rfsAbsPath == null) {
            itMods = OpenCms.getModuleManager().getAllInstalledModules().iterator();
        } else {
            itMods = getAllModulesFromPath(rfsAbsPath).keySet().iterator();
        }
        while (itMods.hasNext()) {
            CmsModule module = (CmsModule)itMods.next();
            if (!moduleNames.contains(module.getName())) {
                Iterator itDeps = ret.values().iterator();
                while (itDeps.hasNext()) {
                    List dependencies = (List)itDeps.next();
                    dependencies.remove(module.getName());
                }
                ret.remove(module.getName());
            }
        }
        return ret;
    }

    /**
     * Returns a map of modules found in the given RFS absolute path.<p>
     * 
     * @param rfsAbsPath the path to look for module distributions
     * 
     * @return a map of <code>{@link CmsModule}</code> objects for keys and filename for values
     * 
     * @throws CmsConfigurationException if something goes wrong
     */
    public static Map getAllModulesFromPath(String rfsAbsPath) throws CmsConfigurationException {

        Map modules = new HashMap();
        if (rfsAbsPath == null) {
            return modules;
        }
        File folder = new File(rfsAbsPath);
        if (folder.exists()) {
            // list all child resources in the given folder
            File[] folderFiles = folder.listFiles();
            if (folderFiles != null) {
                for (int i = 0; i < folderFiles.length; i++) {
                    File moduleFile = folderFiles[i];
                    if (moduleFile.isFile() && !(moduleFile.getAbsolutePath().toLowerCase().endsWith(".zip"))) {
                        // skip non-ZIP files
                        continue;
                    }
                    if (moduleFile.isDirectory()) {
                        File manifest = new File(moduleFile, CmsImportExportManager.EXPORT_MANIFEST);
                        if (!manifest.exists() || !manifest.canRead()) {
                            // skip unused directories
                            continue;
                        }
                    }
                    modules.put(
                        CmsModuleImportExportHandler.readModuleFromImport(moduleFile.getAbsolutePath()),
                        moduleFile.getName());
                }
            }
        }
        return modules;
    }

    /**
     * Sorts a given list of module names by dependencies,
     * so that the resulting list can be imported in that given order,
     * that means modules without dependencies first.<p>
     * 
     * The module dependencies are get from the installed modules or
     * from the module manifest.xml files found in the given FRS path.<p>
     * 
     * @param moduleNames a list of module names
     * @param rfsAbsPath a RFS absolute path to search for modules, or <code>null</code> to use the installed modules
     * 
     * @return a sorted list of module names
     * 
     * @throws CmsConfigurationException if something goes wrong
     */
    public static List topologicalSort(List moduleNames, String rfsAbsPath) throws CmsConfigurationException {

        List modules = new ArrayList(moduleNames);
        List retList = new ArrayList();
        Map moduleDependencies = buildDepsForModulelist(moduleNames, rfsAbsPath, true);
        boolean finished = false;
        while (!finished) {
            finished = true;
            Iterator itMods = modules.iterator();
            while (itMods.hasNext()) {
                String moduleName = (String)itMods.next();

⌨️ 快捷键说明

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