cmspublishmanager.java

来自「找了很久才找到到源代码」· Java 代码 · 共 697 行 · 第 1/2 页

JAVA
697
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/publish/CmsPublishManager.java,v $
 * Date   : $Date: 2007-08-13 16:29:47 $
 * Version: $Revision: 1.5 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 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.publish;

import org.opencms.db.CmsPublishList;
import org.opencms.db.CmsSecurityManager;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.main.CmsException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.relations.CmsRelationFilter;
import org.opencms.report.CmsShellReport;
import org.opencms.report.I_CmsReport;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsSecurityException;
import org.opencms.util.CmsUUID;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * This manager provide access to the publish engine runtime information.<p>
 * 
 * @author Michael Moossen
 * 
 * @version $Revision: 1.5 $
 * 
 * @since 6.5.5
 */
public class CmsPublishManager {

    /** The default history size. */
    public static final int DEFAULT_HISTORY_SIZE = 100;

    /** The default persistance setting for the publish queue. */
    public static final boolean DEFAULT_QUEUE_PERSISTANCE = false;

    /** The default shutdown time for the running publish job. */
    public static final int DEFAULT_QUEUE_SHUTDOWNTIME = 1;

    /** Milliseconds in a second. */
    private static final int MS_ONE_SECOND = 1000;

    /** Indicates if the configuration can be modified. */
    private boolean m_frozen;

    /** The underlying publish engine. */
    private CmsPublishEngine m_publishEngine;

    /** The maximum size of the publish history. */
    private int m_publishHistorySize;

    /** Indicates if the publish queue is re-initialized on statup. */
    private boolean m_publishQueuePersistance;

    /** The amount of time to wait for a publish job during shutdown. */
    private int m_publishQueueShutdowntime;

    /** The security manager. */
    private CmsSecurityManager m_securityManager;

    /**
     * Default constructor used in digester initialization.<p>
     */
    public CmsPublishManager() {

        m_publishEngine = null;
        m_frozen = false;
    }

    /**
     * Constructor used to create a pre-initialized instance.<p>
     * 
     * @param historySize the size of the publish history
     * @param queuePersistance indicates if th equeue is re-initialzed on startup
     * @param queueShutdowntime the amount of time to wait for a publish job during shutdown
     */
    public CmsPublishManager(int historySize, boolean queuePersistance, int queueShutdowntime) {

        m_publishEngine = null;
        m_publishHistorySize = historySize;
        m_publishQueuePersistance = queuePersistance;
        m_publishQueueShutdowntime = queueShutdowntime;
        m_frozen = false;
    }

    /**
     * Aborts the given publish job.<p>
     * 
     * @param cms the cms context
     * @param publishJob the publish job to abort
     * @param removeJob indicates if the job will be removed or added to history
     * 
     * @throws CmsException if there is some problem during unlocking the resources
     * @throws CmsSecurityException if the current user has not enough permissions 
     * @throws CmsPublishException if the publish job can not been aborted 
     */
    public void abortPublishJob(CmsObject cms, CmsPublishJobEnqueued publishJob, boolean removeJob)
    throws CmsException, CmsSecurityException, CmsPublishException {

        if (!OpenCms.getRoleManager().hasRole(cms, CmsRole.PROJECT_MANAGER)
            && !cms.getRequestContext().currentUser().getId().equals(publishJob.getUserId())) {
            // Can only be executed by somebody with the role CmsRole#PROJECT_MANAGER or the owner of the job
            throw new CmsSecurityException(Messages.get().container(
                Messages.ERR_PUBLISH_ENGINE_ABORT_DENIED_1,
                cms.getRequestContext().currentUser().getName()));
        }
        m_publishEngine.abortPublishJob(cms.getRequestContext().currentUser().getId(), publishJob, removeJob);
    }

    /**
     * Adds a publish listener to listen on publish events.<p>
     * 
     * @param listener the publish listener to add
     */
    public void addPublishListener(I_CmsPublishEventListener listener) {

        m_publishEngine.addPublishListener(listener);
    }

    /**
     * Check if the thread for the current publish job is stil active or was interrupted
     * and so the next job in the queue can be started.<p>
     */
    public void checkCurrentPublishJobThread() {

        m_publishEngine.run();
    }

    /**
     * Disables the publishing of resources.<p>
     */
    public void disablePublishing() {

        m_publishEngine.disableEngine();
    }

    /**
     * Enables the enqeueing of resources for publishing.<p>
     */
    public void enablePublishing() {

        m_publishEngine.enableEngine();
    }

    /**
     * Returns the current running publish job.<p>
     * 
     * @return the current running publish job
     */
    public CmsPublishJobRunning getCurrentPublishJob() {

        if (m_publishEngine.getCurrentPublishJob() == null) {
            return null;
        }
        return new CmsPublishJobRunning(m_publishEngine.getCurrentPublishJob().getPublishJob());
    }

    /**
     * Returns a publish job based on its publish history id.<p>
     * 
     * The returned publish job may be an enqueued, running or finished publish job.<p>
     * 
     * @param publishHistoryId the publish hostory id to search for
     * 
     * @return the publish job with the given publish history id, or <code>null</code>
     */
    public CmsPublishJobBase getJobByPublishHistoryId(CmsUUID publishHistoryId) {

        return m_publishEngine.getJobByPublishHistoryId(publishHistoryId);
    }

    /**
     * Returns the publish history list with already publish jobs.<p>
     * 
     * @return a list of {@link CmsPublishJobFinished} objects
     */
    public List getPublishHistory() {

        return m_publishEngine.getPublishHistory().asList();
    }

    /**
     * Returns the publish history list with already publish jobs, filtered by the given user.<p>
     * 
     * @param user the user to filter the jobs with
     * 
     * @return a list of {@link CmsPublishJobFinished} objects
     */
    public List getPublishHistory(CmsUser user) {

        List result = new ArrayList();
        Iterator it = getPublishHistory().iterator();
        while (it.hasNext()) {
            CmsPublishJobFinished publishJob = (CmsPublishJobFinished)it.next();
            if (publishJob.getUserId().equals(user.getId())) {
                result.add(publishJob);
            }
        }
        return result;
    }

    /**
     * Returns the publish History Size.<p>
     *
     * @return the publish History Size
     */
    public int getPublishHistorySize() {

        return m_publishHistorySize;
    }

    /**
     * Returns a publish list with all new/changed/deleted resources of the current (offline)
     * project that actually get published.<p>
     * 
     * @param cms the cms request context
     * 
     * @return a publish list
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsPublishList getPublishList(CmsObject cms) throws CmsException {

        return m_securityManager.fillPublishList(cms.getRequestContext(), new CmsPublishList(
            cms.getRequestContext().currentProject()));
    }

    /**
     * Returns a publish list with all new/changed/deleted resources of the current (offline)
     * project that actually get published for a direct publish of a single resource.<p>
     * 
     * @param cms the cms request context
     * @param directPublishResource the resource which will be directly published
     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct 
     *                      published resource should also get published.
     * 
     * @return a publish list
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsPublishList getPublishList(CmsObject cms, CmsResource directPublishResource, boolean directPublishSiblings)
    throws CmsException {

        return m_securityManager.fillPublishList(cms.getRequestContext(), new CmsPublishList(
            directPublishResource,
            directPublishSiblings));
    }

    /**
     * Returns a publish list with all new/changed/deleted resources of the current (offline)
     * project that actually get published for a direct publish of a List of resources.<p>
     * 
     * @param cms the cms request context
     * @param directPublishResources the resources which will be directly published
     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct 
     *                      published resources should also get published.
     * 
     * @return a publish list
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsPublishList getPublishList(CmsObject cms, List directPublishResources, boolean directPublishSiblings)
    throws CmsException {

        return getPublishList(cms, directPublishResources, directPublishSiblings, true);
    }

    /**
     * Returns a publish list with all new/changed/deleted resources of the current (offline)
     * project that actually get published for a direct publish of a List of resources.<p>
     * 
     * @param cms the cms request context
     * @param directPublishResources the {@link CmsResource} objects which will be directly published
     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct 
     *                      published resources should also get published.
     * @param publishSubResources indicates if sub-resources in folders should be published (for direct publish only)
     * 
     * @return a publish list
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsPublishList getPublishList(
        CmsObject cms,
        List directPublishResources,
        boolean directPublishSiblings,
        boolean publishSubResources) throws CmsException {

        return m_securityManager.fillPublishList(cms.getRequestContext(), new CmsPublishList(
            directPublishResources,
            directPublishSiblings,
            publishSubResources));
    }

    /**
     * Returns the queue with still waiting publish jobs.<p>
     * 
     * @return a list of {@link CmsPublishJobEnqueued} objects
     */
    public List getPublishQueue() {

        return m_publishEngine.getPublishQueue().asList();
    }

    /**
     * Returns the amount of time in seconds the system will wait during shutdown for a running publish job.<p>
     * 
     * @return the shutdown time for a running publish job
     */
    public int getPublishQueueShutdowntime() {

        return m_publishQueueShutdowntime;
    }

    /**

⌨️ 快捷键说明

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