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

📄 cmsregistry.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsRegistry.java,v $
* Date   : $Date: 2004/01/07 09:17:15 $
* Version: $Revision: 1.74.2.1 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* 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 OpenCms, please see the
* OpenCms 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 com.opencms.file;

import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.report.CmsShellReport;
import com.opencms.report.I_CmsReport;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.workplace.I_CmsWpConstants;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Implements the registry for OpenCms.<p>
 * 
 * The OpenCms registry contains information about the installed modules in the system,
 * as well as the launcher mappings, the mail server settings for the task management,
 * the workplace views and some other items.<p>
 *
 * @author Andreas Schouten
 * @author Thomas Weckert (t.weckert@alkacon.com)
 * @author Alexander Kandzior (a.kandzior@alkacon.com)
 * 
 * @version $Revision: 1.74.2.1 $ $Date: 2004/01/07 09:17:15 $
 */
public class CmsRegistry extends A_CmsXmlContent implements I_CmsRegistry, I_CmsConstants, I_CmsWpConstants {

    /**
     *  The xml-document representing the registry.
     */
    private Document m_xmlReg;

    /**
     *  The filename for the registry.
     */
    private String m_regFileName;

    /**
     *  A hashtable with shortcuts into the dom-structure for each module.
     */
    private Hashtable m_modules = new Hashtable();

    /**
     *  A hashtable with all exportpoints and paths.
     */
    private Hashtable m_exportpoints = new Hashtable();

    /**
     *  The cms-object to get access to the system with the context of the current user.
     */
    private CmsObject m_cms = null;

    /**
     *  The date-format to use.
     */
    private SimpleDateFormat m_dateFormat = new java.text.SimpleDateFormat("MM.dd.yyyy");

    /**
     *  A message digest to check the resource-codes
     */
    private MessageDigest m_digest;

    // Module event-method names.
    private static final String C_UPLOAD_EVENT_METHOD_NAME = "moduleWasUploaded";
    private static final String C_UPDATE_PARAMETER_EVENT_METHOD_NAME = "moduleParameterWasUpdated";
    private static final String C_DELETE_EVENT_METHOD_NAME = "moduleWasDeleted";

    /**
     * Declaration of an empty module in the registry.
     */
    private static final String[] C_EMPTY_MODULE = { 
        "<module><type>", 
        "</type><name>", 
        "</name><nicename>", 
        "</nicename><version>", 
        "</version><description><![CDATA[ ", 
        "]]></description><author>", 
        "</author><email/><creationdate>", 
        "</creationdate>",
        "<view/><publishclass/><documentation/><dependencies/><maintenance_class/><parameters/><repository/></module>" 
    };

    /** XML to create an export point */
    private static final String[] C_EXPORTPOINT = { "<exportpoint><source>", "</source><destination>", "</destination></exportpoint>"};
    
    /** Debug flag, set to 9 for maximum vebosity */
    private static final int DEBUG = 0;

    /**
     * Creates a new CmsRegistry for a user. The cms-object represents 
     * the current state of the current user.<p>
     *
     * @param CmsObject the cms-object to get access to the system
     */
    public CmsRegistry(CmsRegistry reg, CmsObject cms) {
        super();
        // there is no need of a real copy for this parameters
        m_modules = reg.m_modules;
        m_exportpoints = reg.m_exportpoints;
        m_regFileName = reg.m_regFileName;
        m_xmlReg = reg.m_xmlReg;
        // store the cms-object for this instance.
        m_cms = cms;
        try {
            m_digest = MessageDigest.getInstance(CmsImport.C_IMPORT_DIGEST);
        } catch (NoSuchAlgorithmException e) {
            m_digest = null;
        }
    }
    
    /**
     * Creates a new CmsRegistry. The regFileName is the path to the registry-file in
     * the server filesystem.
     *
     *  @param String regFileName the path to the registry-file in the server fs.
     */
    public CmsRegistry(String regFileName) throws CmsException {
        super();
        try {
            // store the filename
            m_regFileName = regFileName;

            // get the file
            File xmlFile = new File(m_regFileName);

            // parse the registry-xmlfile and store it.
            InputStream content = new FileInputStream(xmlFile);
            m_xmlReg = parse(content);
            init(true);
        } catch (Exception exc) {
            throw new CmsException("couldn't init registry", CmsException.C_REGISTRY_ERROR, exc);
        }
    }

    /**
     * Checks if the dependencies are fullfilled.<p>
     * 
     * @param module the dom-element describing the new module
     * @param replaceMode if <code>true</code> this is for module replacement, 
     * if <code>false</code> it is form module deletion
     * @return a Vector of conflict description Strings, if this is an empty vector, 
     * there are no conficts (i.e. the dependencies are fullfilled)
     */
    private Vector checkDependencies(Element module, boolean replaceMode) throws CmsException {
        
        float newVersion = -1;
        String versionString = module.getElementsByTagName("version").item(0).getFirstChild().getNodeValue();
        try {
            newVersion = Float.parseFloat(versionString);
        } catch (NumberFormatException e) {}

        Vector retValue = new Vector();
        
        if (replaceMode) { 
            // replace mode, just ensure new version number is larger then the old number
            // TODO: check dependencies of all other installed modules for "maxversion"
            String name = module.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
            // get the version of the module to replace
            float currentVersion = getModuleVersion(name);  
            if (currentVersion > newVersion) {
                retValue.addElement("For module replacement, the new version (" + newVersion + ") must be higher or equal to the current version " + currentVersion);
            }                     
        } else {
            // not replace mode, check if the listed dependencies are o.k.
            try {
                Element dependencies = (Element) (module.getElementsByTagName("dependencies").item(0));
                NodeList deps = dependencies.getElementsByTagName("dependency");
                for (int i = 0; i < deps.getLength(); i++) {
                    String name = ((Element)deps.item(i)).getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
                    float minVersion =
                        Float.parseFloat(
                            ((Element)deps.item(i)).getElementsByTagName("minversion").item(0).getFirstChild().getNodeValue());
                    float maxVersion =
                        Float.parseFloat(
                            ((Element)deps.item(i)).getElementsByTagName("maxversion").item(0).getFirstChild().getNodeValue());
        
                    // get the version of the needed repository
                    float currentVersion = getModuleVersion(name);
        
                    if (currentVersion == -1) {
                        retValue.addElement("The required module " + name + " doesn't exist");
                    } else if (currentVersion < minVersion) {
                        retValue.addElement("Module " + name + " version " + minVersion + " is not high enough");
                    } else if ((maxVersion != C_ANY_VERSION) && (currentVersion > maxVersion)) {
                        retValue.addElement("Module " + name + " version " + maxVersion + " is to high");
                    }
                }
            } catch (Exception exc) {
                throw new CmsException("Could not check the dependencies", CmsException.C_REGISTRY_ERROR, exc);
            }
        }

        return retValue;
    }
    
    /**
     *  Checks if the type of the value is correct.
     *  @param type the type that the value should have..
     *  @param value the value to check.
     */
    private boolean checkType(String type, String value) {
        type = type.toLowerCase();
        try {
            if("string".equals(type) ) {
                if( value != null) {
                    return true;
                } else {
                    return false;
                }
            } else if("int".equals(type) || "integer".equals(type)) {
                Integer.parseInt(value);
                return true;
            } else if("float".equals(type)) {
                Float.valueOf(value);
                return true;
            } else if("boolean".equals(type)) {
                Boolean.valueOf(value);
                return true;
            } else if("long".equals(type)) {
                Long.valueOf(value);
                return true;
            } else if("double".equals(type)) {
                Double.valueOf(value);
                return true;
            } else if("byte".equals(type)) {
                Byte.valueOf(value);
                return true;
            } else {
                // the type dosen't exist
                return false;
            }
        } catch(Exception exc) {
            // the type of the value was wrong
            return false;
        }
    }
    
    /**
     * Clones the registry.<p>
     *
     * @param CmsObject the current cms-object to get access to the system
     * @return the cloned registry
     */
    public I_CmsRegistry clone(CmsObject cms) {
        return new CmsRegistry(this, cms);
    }

    /**
     * This method creates a new module in the repository.
     *
     * @param modulename the name of the module
     * @param niceModulename another name of the module
     * @param description the description of the module
     * @param author the name of the author
     * @param type the type of the module
     * @param exportPoints a map of all export points of the module 
     * @param createDate the creation date of the module
     * @param version the version number of the module
     * @throws CmsException if the user has no right to create a new module
     */
    public void createModule(String modulename, String niceModulename, String description, String author, String type, Map exportPoints, long createDate, float version) throws CmsException {
            createModule(modulename, niceModulename, description, author, type, exportPoints, m_dateFormat.format(new Date(createDate)), version);

⌨️ 快捷键说明

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