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

📄 cmsconfigurationmanager.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/configuration/CmsConfigurationManager.java,v $
 * Date   : $Date: 2006/03/27 14:52:46 $
 * Version: $Revision: 1.30 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 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.configuration;

import org.opencms.i18n.CmsEncoder;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsFileUtil;
import org.opencms.xml.CmsXmlEntityResolver;
import org.opencms.xml.CmsXmlErrorHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocumentType;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

/**
 * Configuration manager for digesting the OpenCms XML configuration.<p>
 * 
 * Reads the individual configuration class nodes first and creaes new 
 * instances of the "base" configuration classes.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.30 $
 * 
 * @since 6.0.0
 */
public class CmsConfigurationManager implements I_CmsXmlConfiguration {

    /** The location of the OpenCms configuration DTD if the default prefix is the system ID. */
    public static final String DEFAULT_DTD_LOCATION = "org/opencms/configuration/";

    /** The default prefix for the OpenCms configuration DTD. */
    public static final String DEFAULT_DTD_PREFIX = "http://www.opencms.org/dtd/6.0/";

    /** The name of the default XML file for this configuration. */
    public static final String DEFAULT_XML_FILE_NAME = "opencms.xml";

    /** The name of the DTD file for this configuration. */
    public static final String DTD_FILE_NAME = "opencms-configuration.dtd";

    /** The "opencms" root node of the XML configuration. */
    public static final String N_ROOT = "opencms";

    /** Postfix for original configuration files. */
    public static final String POSTFIX_ORI = ".ori";

    /** The config node. */
    protected static final String N_CONFIG = "config";

    /** The configurations node. */
    protected static final String N_CONFIGURATION = "configuration";

    /** Date format for the backup file time prefix. */
    private static final SimpleDateFormat BACKUP_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_");

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

    /** The number of days to keep old backups for. */
    private static final long MAX_BACKUP_DAYS = 15;

    /** The folder where to store the backup files of the configuration. */
    private File m_backupFolder;

    /** The base folder where the configuration files are located. */
    private File m_baseFolder;

    /** The initialized configuration classes. */
    private List m_configurations;

    /** The digester for reading the XML configuration. */
    private Digester m_digester;

    /** The legacy configuration based on "opencms.properties". */
    private Map m_legacyConfiguration;

    /**
     * Creates a new OpenCms configuration manager.<p>
     * 
     * @param baseFolder base folder where XML configurations to load are located
     */
    public CmsConfigurationManager(String baseFolder) {

        m_baseFolder = new File(baseFolder);
        if (!m_baseFolder.exists()) {
            if (LOG.isErrorEnabled()) {
                LOG.error(Messages.get().getBundle().key(
                    Messages.LOG_INVALID_CONFIG_BASE_FOLDER_1,
                    m_baseFolder.getAbsolutePath()));
            }
        }
        m_backupFolder = new File(m_baseFolder.getAbsolutePath() + File.separatorChar + "backup");
        if (!m_backupFolder.exists()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_CREATE_CONFIG_BKP_FOLDER_1,
                    m_backupFolder.getAbsolutePath()));
            }
            m_backupFolder.mkdirs();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_CONFIG_BASE_FOLDER_1, m_baseFolder.getAbsolutePath()));
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_CONFIG_BKP_FOLDER_1, m_backupFolder.getAbsolutePath()));
        }
        cacheDtdSystemId(this);
        m_configurations = new ArrayList();
    }

    /**
     * Adds a configuration object to the configuration manager.<p>
     * 
     * @param configuration the configuration to add
     */
    public void addConfiguration(I_CmsXmlConfiguration configuration) {

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_CONFIG_1, configuration));
        }
        m_configurations.add(configuration);
        cacheDtdSystemId(configuration);
    }

    /**
     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
     */
    public void addConfigurationParameter(String paramName, String paramValue) {

        // noop, this configuration has no additional parameters
    }

    /**
     * @see org.opencms.configuration.I_CmsXmlConfiguration#addXmlDigesterRules(org.apache.commons.digester.Digester)
     */
    public void addXmlDigesterRules(Digester digester) {

        // add rule for <configuration> node        
        digester.addObjectCreate(
            "*/" + N_CONFIGURATION + "/" + N_CONFIG,
            I_CmsXmlConfiguration.A_CLASS,
            CmsConfigurationException.class);
        digester.addSetNext("*/" + N_CONFIGURATION + "/" + N_CONFIG, "addConfiguration");
    }

    /**
     * @see org.opencms.configuration.I_CmsXmlConfiguration#generateXml(org.dom4j.Element)
     */
    public Element generateXml(Element parent) {

        // add the <configuration> node
        Element configurationElement = parent.addElement(N_CONFIGURATION);
        for (int i = 0; i < m_configurations.size(); i++) {
            // append the individual configuration 
            I_CmsXmlConfiguration configuration = (I_CmsXmlConfiguration)m_configurations.get(i);
            configurationElement.addElement(N_CONFIG).addAttribute(
                I_CmsXmlConfiguration.A_CLASS,
                configuration.getClass().getName());
        }
        return parent;
    }

    /**
     * Creates the XML document build from the provided configuration.<p>
     * 
     * @param configuration the configuration to build the XML for
     * @return the XML document build from the provided configuration
     */
    public Document generateXml(I_CmsXmlConfiguration configuration) {

        // create a new document
        Document result = DocumentHelper.createDocument();

        // set the document type        
        DOMDocumentType docType = new DOMDocumentType();
        docType.setElementName(N_ROOT);
        docType.setSystemID(configuration.getDtdUrlPrefix() + configuration.getDtdFilename());
        result.setDocType(docType);

        Element root = result.addElement(N_ROOT);
        // start the XML generation
        configuration.generateXml(root);

        // return the resulting document
        return result;
    }

    /**
     * Returns the backup folder.<p>
     *
     * @return the backup folder
     */
    public File getBackupFolder() {

        return m_backupFolder;
    }

    /**
     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
     */
    public Map getConfiguration() {

        return m_legacyConfiguration;
    }

    /**
     * Returns a specific configuration from the list of initialized configurations.<p>
     * 
     * @param clazz the configuration class that should be returned
     * @return the initialized configuration class instance, or <code>null</code> if this is not found
     */
    public I_CmsXmlConfiguration getConfiguration(Class clazz) {

        for (int i = 0; i < m_configurations.size(); i++) {
            I_CmsXmlConfiguration configuration = (I_CmsXmlConfiguration)m_configurations.get(i);

⌨️ 快捷键说明

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