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

📄 cmsconfigurationmanager.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (clazz.equals(configuration.getClass())) {
                return configuration;
            }
        }
        return null;
    }

    /**
     * Returns the list of all initialized configurations.<p>
     * 
     * @return the list of all initialized configurations
     */
    public List getConfigurations() {

        return m_configurations;
    }

    /**
     * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdFilename()
     */
    public String getDtdFilename() {

        return DTD_FILE_NAME;
    }

    /**
     * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdSystemLocation()
     */
    public String getDtdSystemLocation() {

        return DEFAULT_DTD_LOCATION;
    }

    /**
     * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdUrlPrefix()
     */
    public String getDtdUrlPrefix() {

        return DEFAULT_DTD_PREFIX;
    }

    /**
     * @see org.opencms.configuration.I_CmsXmlConfiguration#getXmlFileName()
     */
    public String getXmlFileName() {

        return DEFAULT_XML_FILE_NAME;
    }

    /**
     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
     */
    public void initConfiguration() {

        // does not need to be initialized
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_INIT_CONFIGURATION_1, this));
        }
    }

    /**
     * Loads the OpenCms configuration from the given XML file.<p>
     * 
     * @throws SAXException in case of XML parse errors
     * @throws IOException in case of file IO errors
     */
    public void loadXmlConfiguration() throws SAXException, IOException {

        URL baseUrl = m_baseFolder.toURL();
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_BASE_URL_1, baseUrl));
        }

        // first load the base configuration
        loadXmlConfiguration(baseUrl, this);

        // now iterate all sub-configrations
        Iterator i = m_configurations.iterator();
        while (i.hasNext()) {
            I_CmsXmlConfiguration config = (I_CmsXmlConfiguration)i.next();
            loadXmlConfiguration(baseUrl, config);
        }

        // remove the old backups
        removeOldBackups(MAX_BACKUP_DAYS);
    }

    /**
     * Sets the configuration read from the legacy "opencms.properties".<p>
     * 
     * @param legacyConfiguration the configuration read from the legacy "opencms.properties"
     */
    public void setConfiguration(Map legacyConfiguration) {

        m_legacyConfiguration = legacyConfiguration;
    }

    /**
     * Writes the XML configuration for the provided configuration instance.<p>
     * 
     * @param clazz the configuration class to write the XML for
     * @throws IOException in case of I/O errors while writing
     * @throws CmsConfigurationException if the given class is not a valid configuration class
     */
    public void writeConfiguration(Class clazz) throws IOException, CmsConfigurationException {

        I_CmsXmlConfiguration configuration = getConfiguration(clazz);
        if (configuration == null) {
            throw new CmsConfigurationException(Messages.get().container(
                Messages.ERR_CONFIG_WITH_UNKNOWN_CLASS_1,
                clazz.getName()));
        }

        // generate the file URL for the XML input
        File file = new File(m_baseFolder, configuration.getXmlFileName());
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITE_CONFIG_XMLFILE_1, file.getAbsolutePath()));
        }

        // generate the XML document 
        Document config = generateXml(configuration);

        // output the document
        XMLWriter writer = null;
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setIndentSize(4);
        format.setTrimText(false);
        format.setEncoding(CmsEncoder.ENCODING_UTF_8);

        try {
            OutputStream out = new FileOutputStream(file);
            writer = new XMLWriter(out, format);
            writer.write(config);
            writer.flush();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }

        if (LOG.isInfoEnabled()) {
            LOG.info(Messages.get().getBundle().key(
                Messages.LOG_WRITE_CONFIG_SUCCESS_2,
                file.getAbsolutePath(),
                configuration.getClass().getName()));
        }
    }

    /**
     * Creates a backup of the given XML configurations input file.<p>
     * 
     * @param configuration the configuration for which the input file should be backed up
     */
    private void backupXmlConfiguration(I_CmsXmlConfiguration configuration) {

        String fromName = m_baseFolder.getAbsolutePath() + File.separatorChar + configuration.getXmlFileName();
        String toDatePrefix = BACKUP_DATE_FORMAT.format(new Date());
        String toName = m_backupFolder.getAbsolutePath()
            + File.separatorChar
            + toDatePrefix
            + configuration.getXmlFileName();

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATE_CONFIG_BKP_2, fromName, toName));
        }

        try {
            CmsFileUtil.copy(fromName, toName);
        } catch (IOException e) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_CREATE_CONFIG_BKP_FAILURE_1, toName), e);
        }
    }

    /**
     * Adds a new DTD system id prefix mapping for internal resolution of external URLs.<p>
     * 
     * @param configuration the configuration to add the mapping from
     */
    private void cacheDtdSystemId(I_CmsXmlConfiguration configuration) {

        if (configuration.getDtdSystemLocation() != null) {
            try {
                String file = CmsFileUtil.readFile(configuration.getDtdSystemLocation()
                    + configuration.getDtdFilename(), CmsEncoder.ENCODING_UTF_8);
                CmsXmlEntityResolver.cacheSystemId(
                    configuration.getDtdUrlPrefix() + configuration.getDtdFilename(),
                    file.getBytes(CmsEncoder.ENCODING_UTF_8));
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(
                        Messages.LOG_CACHE_DTD_SYSTEM_ID_1,
                        configuration.getDtdUrlPrefix()
                            + configuration.getDtdFilename()
                            + " --> "
                            + configuration.getDtdSystemLocation()
                            + configuration.getDtdFilename()));
                }
            } catch (IOException e) {
                LOG.error(Messages.get().getBundle().key(
                    Messages.LOG_CACHE_DTD_SYSTEM_ID_FAILURE_1,
                    configuration.getDtdSystemLocation() + configuration.getDtdFilename()), e);
            }
        }
    }

    /**
     * Loads the OpenCms configuration from the given XML URL.<p>
     * 
     * @param url the base URL of the XML configuration to load
     * @param configuration the configuration to load
     * @throws SAXException in case of XML parse errors
     * @throws IOException in case of file IO errors
     */
    private void loadXmlConfiguration(URL url, I_CmsXmlConfiguration configuration) throws SAXException, IOException {

        // generate the file URL for the XML input
        URL fileUrl = new URL(url, configuration.getXmlFileName());
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOAD_CONFIG_XMLFILE_1, fileUrl));
        }

        // create a backup of the configuration
        backupXmlConfiguration(configuration);

        // instantiate Digester and enable XML validation
        m_digester = new Digester();
        m_digester.setUseContextClassLoader(true);
        m_digester.setValidating(true);
        m_digester.setEntityResolver(new CmsXmlEntityResolver(null));
        m_digester.setRuleNamespaceURI(null);
        m_digester.setErrorHandler(new CmsXmlErrorHandler());

        // add this class to the Digester
        m_digester.push(configuration);

        configuration.addXmlDigesterRules(m_digester);

        // start the parsing process        
        m_digester.parse(fileUrl.openStream());
    }

    /**
     * Removes all backups that are older then the given number of days.<p>
     * 
     * @param daysToKeep the days to keep the backups for
     */
    private void removeOldBackups(long daysToKeep) {

        long maxAge = (System.currentTimeMillis() - (daysToKeep * 24 * 60 * 60 * 1000));
        File[] files = m_backupFolder.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            long lastMod = file.lastModified();
            if ((lastMod < maxAge) & (!file.getAbsolutePath().endsWith(CmsConfigurationManager.POSTFIX_ORI))) {
                file.delete();
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_REMOVE_CONFIG_FILE_1, file.getAbsolutePath()));
                }
            }
        }
    }
}

⌨️ 快捷键说明

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