001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.staticexport;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032import org.opencms.report.I_CmsReport;
033import org.opencms.util.CmsUUID;
034
035import org.apache.commons.logging.Log;
036
037/**
038 * Abstract implementation for the <code>{@link I_CmsStaticExportHandler}</code> interface.<p>
039 *
040 * This handler is most suitable for dynamic sites that use the static export
041 * as optimization for non-dynamic content.<p>
042 *
043 * @since 6.0.0
044 *
045 * @see I_CmsStaticExportHandler
046 */
047public abstract class A_CmsOnDemandStaticExportHandler extends A_CmsStaticExportHandler {
048
049    /** The log object for this class. */
050    private static final Log LOG = CmsLog.getLog(A_CmsOnDemandStaticExportHandler.class);
051
052    /**
053     * @see org.opencms.staticexport.I_CmsStaticExportHandler#performEventPublishProject(org.opencms.util.CmsUUID, org.opencms.report.I_CmsReport)
054     */
055    @Override
056    public void performEventPublishProject(CmsUUID publishHistoryId, I_CmsReport report) {
057
058        int count = 0;
059        // if the handler is still running, we must wait up to 30 secounds until it is finished
060        while ((count < CmsStaticExportManager.HANDLER_FINISH_TIME) && isBusy()) {
061            count++;
062            try {
063                if (LOG.isInfoEnabled()) {
064                    LOG.info(
065                        Messages.get().getBundle().key(
066                            Messages.LOG_WAITING_STATIC_EXPORT_3,
067                            getClass().getName(),
068                            Integer.valueOf(count),
069                            Integer.valueOf(CmsStaticExportManager.HANDLER_FINISH_TIME)));
070                }
071                Thread.sleep(1000);
072            } catch (InterruptedException e) {
073                // if interrupted we ignore the handler, this will produce some log messages but should be ok
074                count = CmsStaticExportManager.HANDLER_FINISH_TIME;
075            }
076        }
077
078        if (isBusy()) {
079            // if the handler is still busy write a warning to the log and exit
080            Object[] arguments = new Object[] {
081                publishHistoryId,
082                Integer.valueOf(CmsStaticExportManager.HANDLER_FINISH_TIME)};
083            LOG.error(Messages.get().getBundle().key(Messages.LOG_SCRUBBING_FOLDER_FAILED_2, arguments));
084
085            return;
086        }
087
088        final CmsUUID id = publishHistoryId;
089
090        if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_1_CORE_OBJECT) {
091            // only perform scrubbing if OpenCms is still running
092            m_busy = true;
093            Thread t = new Thread(new Runnable() {
094
095                public void run() {
096
097                    try {
098                        scrubExportFolders(id);
099                    } finally {
100                        m_busy = false;
101                    }
102                }
103            }, Messages.get().getBundle().key(Messages.GUI_THREAD_NAME_SCRUB_EXPORT_FOLDERS_1, String.valueOf(id)));
104            t.start();
105        }
106    }
107}