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

📄 cmsstaticexport.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsStaticExport.java,v $
* Date   : $Date: 2002/04/10 08:22:11 $
* Version: $Revision: 1.25 $
*
* 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.*;
import com.opencms.launcher.*;
import com.opencms.util.Utils;
import java.util.*;
import java.io.*;
import java.net.*;
import org.apache.oro.text.perl.*;

/**
 * This class holds the functionaility to export resources from the cms
 * to the filesystem.
 *
 * @author Hanjo Riege
 * @version $Revision: 1.25 $ $Date: 2002/04/10 08:22:11 $
 */
public class CmsStaticExport implements I_CmsConstants{

    /**
     * The cms-object to do the operations.
     */
    private CmsObject m_cms;

    /**
     * The resources to export.
     */
    private Vector m_startpoints;

    /**
     * indicates that this is called after publish project
     */
    private boolean m_afterPublish = false;

    /**
     * In this vector we store the links that have changed after publish. It is
     * used by the search.
     * It contains compleate links (after linkreplace rules).
     */
    private Vector m_changedLinks = null;

    private static Perl5Util c_perlUtil = null;

    /**
     * The export path.
     */
    private String m_exportPath;

    /**
     *
     */
    private String m_webAppUrl="";
    private String m_servletUrl="";

    /**
     * Value for resources with property export = false
     */
    static final String C_EXPORT_FALSE = "export value was set to false";

    /**
     * Additional rules for the export generated dynamical.
     * Used to replace ugly long foldernames with short nice
     * ones set as a property to the folder.
     * one extra version for extern
     */
    public static Vector m_dynamicExportNameRules = null;
    public static Vector m_dynamicExportNameRulesExtern = null;

    /**
     * Additional rules for the export generated dynamical.
     * Used for linking back to OpenCms for dynamic content and
     * linking out to the static stuff. Used for online and for export.
     */
    public static Vector m_dynamicExportRulesOnline = null;

    /**
     * Additional rules for the export generated dynamical.
     * Used for linking back to OpenCms for dynamic content and
     * linking out to the static stuff. Used for extern. It only
     * returns "" so the staticExport dont write something to disk.
     */
    public static Vector m_dynamicExportRulesExtern = null;

    /**
     * This rules are only tho show if a resource is enabled with and without
     * https. they are generated by the export property (values https_enabled
     * for exportable resources(true) and dynamic_https_enabled for dynamic
     * resources (dynamic).
     */
    public static Vector m_rulesForHttpsEnabledResources = null;

    /**
     * This constructs a new CmsStaticExport-object which generates static html pages in the filesystem.
     *
     * @param cms the cms-object to work with.
     * @param startpoints. The resources to export (Vector of Strings)
     * @param doTheExport. must be set to true to export something, otherwise only the linkrules are generated.
     * @param changedResources. Contains the changed resources belonging to the project, if started after publishProject.
     *
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    public CmsStaticExport(CmsObject cms, Vector startpoints, boolean doTheExport,
                     Vector changedLinks, CmsPublishedResources changedResources)
                     throws CmsException{
        m_cms = cms;
        m_startpoints = startpoints;
        m_changedLinks = changedLinks;
        m_exportPath = cms.getStaticExportProperties().getExportPath();
        c_perlUtil = new Perl5Util();
        if(changedResources != null){
            m_afterPublish = true;
        }

        if(!doTheExport){
            // this is just to generate the dynamic rulesets
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                "[CmsStaticExport] Generating the dynamic rulesets.");
            }
            createDynamicRules();
        }else{
            try{
                m_servletUrl = cms.getRequestContext().getRequest().getServletUrl();
                m_webAppUrl = cms.getRequestContext().getRequest().getWebAppUrl();
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                        "[CmsStaticExport] Starting the static export.");
                }
                createDynamicRules();
                checkExportPath();
                Vector exportLinks = null;
                if(m_afterPublish){
                    exportLinks = getChangedLinks(changedResources);
                }else{
                    exportLinks = getStartLinks();
                }
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                            "[CmsStaticExport] got "+exportLinks.size()+" links to start with.");
                }
                for(int i=0; i < exportLinks.size(); i++){
                    String aktLink = (String)exportLinks.elementAt(i);
                    exportLink(aktLink, exportLinks);
                }
                setChangedLinkVector(exportLinks);
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                            "[CmsStaticExport] all done.");
                }
            }catch(NullPointerException e){
                // no original request
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT,
                                        "[CmsStaticExport] Nothing found to export.");
                }
            }
        }
    }

    /**
     * This method checks if this link has to be exported.
     *
     * @param link The link to be checked.
     * @return true if the link should be exported.
     */
    private boolean linkHasChanged(String link){
        CmsExportLink linkObject = null;
        try{
            // first look if this link was exported before (then it is saved in the database)
            linkObject = m_cms.readExportLink(link);
            if(linkObject == null){
                return true;
            }else{
                // lets check the dates
                Vector deps = linkObject.getDependencies();
                try{
                    for(int i=0; i<deps.size(); i++){
                        CmsResource res = m_cms.readFileHeader((String)deps.elementAt(i));
                        if(linkObject.getLastExportDate() < res.getDateLastModified()){
                            // ok one of the deps has changed since the last export
                            m_cms.deleteExportLink(linkObject);
                            return true;
                        }
                    }
                }catch(CmsException e){
                    // one of the files was deleted, we have to export it again
                    m_cms.deleteExportLink(linkObject);
                    return true;
                }
            }
        }catch(CmsException e){
            // this should not happen. Better we delete the link in the database and export it.
            try{
                m_cms.deleteExportLink(link);
            }catch(CmsException exc){
            }
            return true;
        }
        //set the processed flag to true
        if(linkObject != null){
            linkObject.setProcessedState(true);
            // write the linkObject to the database. Only the state has changed so
            // we dont need to write the whole object.
            try{
                m_cms.writeExportLinkProcessedState(linkObject);
            }catch(CmsException exp){
                linkObject.setProcessedState(false);
                return true;
            }
        }
        return false;
    }

    /**
     * Checks if the export path exists. If not it is created.
     */
    private void checkExportPath()throws CmsException{

        if(m_exportPath == null){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] no export folder given (null).");
            }
            throw new CmsException("[" + this.getClass().getName() + "] " + "no export folder given (null)", CmsException.C_BAD_NAME);
        }
        // we don't need the last slash
        if(m_exportPath.endsWith("/")){
            m_exportPath = m_exportPath.substring(0, m_exportPath.length()-1);
        }
        File discFolder = new File(m_exportPath + "/");
        if (!discFolder.exists()){
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_STATICEXPORT, "[CmsStaticExport] the export folder does not exist.");
            }
            throw new CmsException("[" + this.getClass().getName() + "] " + "the export folder does not exist", CmsException.C_BAD_NAME);
        }
    }

    /**
     * Generates the dynamic rules for the export. It looks for all folders that
     * have the property exportname. For each folder found it generates a rule that
     * replaces the folder name (incl. path) with the exportname.
     * In addition it looks for the property dynamic. and creates rules for linking
     * between static and dynamic pages.
     * These rules are used for export and for extern links.
     */
    private void createDynamicRules(){
        // first the rules for namereplacing
        try{
            // get the resources with the property exportname
            Vector resWithProp = null;
            if(!"false_ssl".equalsIgnoreCase(m_cms.getStaticExportProperties().getStaticExportEnabledValue())){
                resWithProp = m_cms.getResourcesWithProperty(C_PROPERTY_EXPORTNAME);
                // generate the dynamic rules for the nice exportnames
                if(resWithProp != null && resWithProp.size() != 0){
                    m_dynamicExportNameRules = new Vector();
                    m_dynamicExportNameRulesExtern = new Vector();

⌨️ 快捷键说明

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