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

📄 a_cmslauncher.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        return cmsTemplate;
    }

    /**
     * Utility method to handle any occurence of an execption.
     * <P>
     * If the Exception is NO CmsException (i.e. it was not detected previously)
     * it will be written to the logfile.
     * <P>
     * If the current user is the anonymous user, no further execption will
     * be thrown, but a server error will be sent
     * (we want to prevent the user from seeing any exeptions).
     * Otherwise a new Exception will be thrown.
     *
     * @param cms CmsObject Object for accessing system resources.
     * @param e Exception that should be handled.
     * @param errorText Error message that should be shown.
     * @throws CmsException
     */
    public void handleException(CmsObject cms, Exception e, String errorText) throws CmsException {

        // Print out some error messages
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
            A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + errorText);
            A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + "--> Exception: " + com.opencms.util.Utils.getStackTrace(e));
            A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + "--> Cannot create output for this file. Must send error. Sorry.");
        }

        // If the user is "Guest", we send an servlet error.

        // Otherwise we try to throw an exception.
        CmsRequestContext reqContext = cms.getRequestContext();
        if((!C_DEBUG) && cms.anonymousUser().equals(reqContext.currentUser())) {
            throw new CmsException(errorText, CmsException.C_SERVICE_UNAVAILABLE, e);
        }
        else {
            if(e instanceof CmsException) {
                throw (CmsException)e;
            }
            else {
                throw new CmsException(errorText, CmsException.C_LAUNCH_ERROR, e);
            }
        }
    }

    /**
     * Start method called by the OpenCms system to show a resource.
     * <P>
     * In this method initial values valid for all launchers can be set
     * and the _clearcache parameter is checked.
     * After this the abstract method launch(...) is called to
     * invoke the customized part of the launcher.
     *
     * @param cms CmsObject Object for accessing system resources.
     * @param file CmsFile Object with the selected resource to be shown.
     * @param startTemplateClass Name of the template class to start with.
     * @param openCms a instance of A_OpenCms for redirect-needs
     * @throws CmsException
     */
    public void initlaunch(CmsObject cms, CmsFile file, String startTemplateClass, A_OpenCms openCms) throws CmsException {

        // First some debugging output.
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() && C_DEBUG ) {
            A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + "Launcher started for " + file.getName());
        }

        // Check all values to be valid
        String errorMessage = null;
        if(file == null) {
            errorMessage = "Got \"null\" CmsFile object. :-(";
        }
        if(cms == null) {
            errorMessage = "Actual cms object missing";
        }
        if(errorMessage != null) {
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + errorMessage);
            }
            throw new CmsException(errorMessage, CmsException.C_LAUNCH_ERROR);
        }

        // Check the clearcache parameter
        String clearcache = cms.getRequestContext().getRequest().getParameter("_clearcache");
        
        // Clear launcher caches if this is required
        clearLauncherCache(cms, 
            ((clearcache != null) && ("all".equals(clearcache) || "file".equals(clearcache))),
            ((clearcache != null) && ("all".equals(clearcache) || "template".equals(clearcache))));
        
        launch(cms, file, startTemplateClass, openCms);
    }
    
    /**
     * Compatibility method to ensure the legacy cache command line parameters
     * are still supported.<p>
     * 
     * @param cms an initialized CmsObject
     * @param clearClasses if true, CmsTemplateClassManager is cleared
     * @param clearFiles if true, A_CmsXmlContent cache is cleared
     * @param clearTemplates if true, internal template cache is cleared.
     */
    private static void clearLauncherCache(CmsObject cms, boolean clearFiles, boolean clearTemplates) {
        long currentFsCounter = cms.getFileSystemChanges();
        if(clearFiles || (currentFsCounter > m_lastFsCounterFile)) {
            A_CmsXmlContent.clearFileCache();
            m_lastFsCounterFile = currentFsCounter;
        }
        if(clearTemplates || (currentFsCounter > m_lastFsCounterTemplate)) {
            m_templateCache.clearCache();
            m_lastFsCounterTemplate = currentFsCounter;
        }        
    }

    /**
     * Clear the XML template cache that is maintained in the launcher.
     * To use this method, call it on one of the classes that extend 
     * A_CmsLauncher (e.g. com.opencms.launcher.CmsXmlLauncher.clearLauncherCache()).
     * @param cms an initialized CmsObject
     */  
    public static void clearLauncherCache(CmsObject cms) {
        clearLauncherCache(cms, true, true);
    }
    

    /**
     * Unitary method to start generating the output.
     * Every launcher has to implement this method.
     * In it possibly the selected file will be analyzed, and the
     * Canonical Root will be called with the appropriate
     * template class, template file and parameters. At least the
     * canonical root's output must be written to the HttpServletResponse.
     *
     * @param cms CmsObject Object for accessing system resources
     * @param file CmsFile Object with the selected resource to be shown
     * @param startTemplateClass Name of the template class to start with.
     * @param openCms a instance of A_OpenCms for redirect-needs
     * @throws CmsException
     */
    protected abstract void launch(CmsObject cms, CmsFile file, String startTemplateClass, A_OpenCms openCms) throws CmsException;

    /**
     * Writes a given byte array to the HttpServletRespose output stream.
     * @param result byte array that should be written.
     * @param mimeType MIME type that should be set for the output.
     * @throws CmsException
     */
    protected void writeBytesToResponse(CmsObject cms, byte[] result) throws CmsException {
        try {
            I_CmsResponse resp = cms.getRequestContext().getResponse();
            if((!cms.getRequestContext().isStreaming()) && result != null && !resp.isRedirected()) {
                // Only write any output to the response output stream if
                // the current request is neither redirected nor streamed.
                OutputStream out = resp.getOutputStream();

                resp.setContentLength(result.length);
                resp.setHeader("Connection", "keep-alive");
                out.write(result);
                out.close();
            }
        }
        catch(IOException ioe) {
            if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_DEBUG) ) {
                A_OpenCms.log(C_OPENCMS_DEBUG, getClassName() + "IO error while writing to response stream for " + cms.getRequestContext().getFileUri());
                A_OpenCms.log(C_OPENCMS_DEBUG, getClassName() + ioe);
            }
        }
        catch(Exception e) {
            String errorMessage = "Cannot write output to HTTP response stream";
            handleException(cms, e, errorMessage);
        }
    }

    /**
     * Sets the currently running OpenCms instance.
     */
    public void setOpenCms(A_OpenCms openCms) {
        // normally we don't need the instance - ignoring
        // if a launcher uses this, it should overload this method
    }
}

⌨️ 快捷键说明

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