001/*
002 * This program is part of the Alkacon OpenCms Software library.
003 *
004 * This license applies to all programs, pages, Java classes, parts and
005 * modules of the Alkacon OpenCms Software library published by
006 * Alkacon Software GmbH & Co. KG, unless otherwise noted.
007 *
008 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
009 *
010 * This program is free software; you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as published by
012 * the Free Software Foundation; either version 2 of the License, or (at
013 * your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * General Public License for more details.
019 *
020 * You should have received a copy of the GNU General Public License
021 * along with this program; if not, write to the Free Software
022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023 *
024 * For further information about Alkacon Software GmbH & Co. KG, please see the
025 * companys website: http://www.alkacon.com.
026 *
027 * For further information about OpenCms, please see the OpenCms project
028 * website: http://www.opencms.org.
029 *
030 * The names "Alkacon", "Alkacon Software GmbH & Co. KG" and "OpenCms" must not be used
031 * to endorse or promote products derived from this software without prior
032 * written permission. For written permission, please contact info@alkacon.com.
033 *
034 * Products derived from this software may not be called "Alkacon",
035 * "Alkacon Software GmbH & Co. KG" or "OpenCms", nor may "Alkacon", "Alkacon Software GmbH & Co. KG"
036 * or "OpenCms" appear in their name, without prior written permission of
037 * Alkacon Software GmbH & Co. KG.
038 *
039 * This program is also available under a commercial non-GPL license. For
040 * pricing and ordering information, please inquire at sales@alkacon.com.
041 */
042
043package org.opencms.ui.apps.dbmanager;
044
045import org.opencms.file.CmsObject;
046import org.opencms.file.CmsResource;
047import org.opencms.file.CmsResourceFilter;
048import org.opencms.lock.CmsLockFilter;
049import org.opencms.lock.CmsLockType;
050import org.opencms.main.OpenCms;
051import org.opencms.report.A_CmsReportThread;
052import org.opencms.report.I_CmsReport;
053import org.opencms.ui.apps.Messages; //TODO move messages
054
055import java.util.ArrayList;
056import java.util.Iterator;
057import java.util.List;
058
059/**
060 * Remove the publish locks.<p>
061 *
062 * @since 7.0.2
063 */
064public class CmsRemovePubLocksThread extends A_CmsReportThread {
065
066    /** The last error occurred. */
067    private Throwable m_error;
068
069    /** The list of resource names. */
070    private List<String> m_resources;
071
072    /**
073     * Creates an Thread to remove the publish locks.<p>
074     *
075     * @param cms the current OpenCms context object
076     * @param resources a list of resource names
077     */
078    public CmsRemovePubLocksThread(CmsObject cms, List<String> resources) {
079
080        super(cms, Messages.get().getBundle().key(Messages.GUI_DB_PUBLOCKS_THREAD_NAME_0));
081        m_resources = new ArrayList<String>(resources);
082        initHtmlReport(cms.getRequestContext().getLocale());
083    }
084
085    /**
086     * Returns the last error.<p>
087     *
088     * @see org.opencms.report.A_CmsReportThread#getError()
089     */
090    @Override
091    public Throwable getError() {
092
093        return m_error;
094    }
095
096    /**
097     * Updates the report.<p>
098     *
099     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
100     */
101    @Override
102    public String getReportUpdate() {
103
104        return getReport().getReportUpdate();
105    }
106
107    /**
108     * Starts the report thread.<p>
109     *
110     * @see java.lang.Runnable#run()
111     */
112    @Override
113    public void run() {
114
115        try {
116            getReport().println(
117                Messages.get().container(Messages.RPT_DB_PUBLOCKS_BEGIN_0),
118                I_CmsReport.FORMAT_HEADLINE);
119            CmsObject cms = getCms();
120            CmsLockFilter filter = CmsLockFilter.FILTER_ALL;
121            filter = filter.filterType(CmsLockType.PUBLISH);
122
123            Iterator<String> it = m_resources.iterator();
124            while (it.hasNext()) {
125                String paramResName = it.next();
126                getReport().println(
127                    Messages.get().container(Messages.RPT_DB_PUBLOCKS_READLOCKS_1, paramResName),
128                    I_CmsReport.FORMAT_NOTE);
129                Iterator<String> itResources = cms.getLockedResources(paramResName, filter).iterator();
130                while (itResources.hasNext()) {
131                    String resName = itResources.next();
132                    if (!cms.existsResource(resName, CmsResourceFilter.ALL)) {
133                        getReport().println(
134                            Messages.get().container(Messages.RPT_DB_PUBLOCKS_UNLOCKING_1, resName),
135                            I_CmsReport.FORMAT_DEFAULT);
136                        OpenCms.getMemoryMonitor().uncacheLock(cms.getRequestContext().addSiteRoot(resName));
137                        continue;
138                    }
139                    Iterator<CmsResource> itSiblings = cms.readSiblings(resName, CmsResourceFilter.ALL).iterator();
140                    while (itSiblings.hasNext()) {
141                        CmsResource res = itSiblings.next();
142                        getReport().println(
143                            Messages.get().container(Messages.RPT_DB_PUBLOCKS_UNLOCKING_1, cms.getSitePath(res)),
144                            I_CmsReport.FORMAT_DEFAULT);
145                        OpenCms.getMemoryMonitor().uncacheLock(res.getRootPath());
146                    }
147                }
148            }
149            getReport().println(Messages.get().container(Messages.RPT_DB_PUBLOCKS_END_0), I_CmsReport.FORMAT_HEADLINE);
150        } catch (Throwable exc) {
151            getReport().println(
152                Messages.get().container(Messages.RPT_DB_PUBLOCKS_FAILED_0),
153                I_CmsReport.FORMAT_WARNING);
154            getReport().println(exc);
155            m_error = exc;
156        }
157    }
158}