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, 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.publish;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.util.CmsUUID;
035
036import java.util.concurrent.ConcurrentHashMap;
037
038/**
039 * Performs some additional checks on publish lists to prevent inconsistent VFS states.<p>
040 */
041public class CmsPublishListVerifier {
042
043    /**
044     * Entry for parent folders in which it is not allowed to publish resources.<p>
045     *
046     * Also contains a field for the reason why publishing is not allowed.
047     */
048    private class ForbiddenFolderEntry {
049
050        /** Reason why publishing is not allowed. */
051        String m_reason;
052
053        /** Root path of the forbidden parent folder. */
054        String m_rootPath;
055
056        /**
057         * Creates a new entry.<p>
058         *
059         * @param rootPath the root path of the forbidden entry
060         * @param reason the reason why publishing is not allowed
061         */
062        public ForbiddenFolderEntry(String rootPath, String reason) {
063            m_rootPath = rootPath;
064            m_reason = reason;
065        }
066
067        /**
068         * Returns the reason.<p>
069         *
070         * @return the reason
071         */
072        public String getReason() {
073
074            return m_reason;
075        }
076
077        /**
078         * Returns the rootPath.<p>
079         *
080         * @return the rootPath
081         */
082        public String getRootPath() {
083
084            return m_rootPath;
085        }
086    }
087
088    /** Forbidden parent folders, with UUIDs as keys. */
089    private ConcurrentHashMap<CmsUUID, ForbiddenFolderEntry> m_forbiddenParentFolders = new ConcurrentHashMap<CmsUUID, ForbiddenFolderEntry>();
090
091    /**
092     * Creates a new instance.<p>
093     */
094    public CmsPublishListVerifier() {
095        // do nothing
096    }
097
098    /**
099     * Adds a forbidden parent folder.<p>
100     *
101     * @param parentFolder the forbidden parent folder
102     * @param reason the reason why publishing is not allowed
103     * @return an ID which can be used later to remove the forbidden parent folder
104     */
105    public CmsUUID addForbiddenParentFolder(String parentFolder, String reason) {
106
107        CmsUUID id = new CmsUUID();
108        m_forbiddenParentFolders.put(id, new ForbiddenFolderEntry(parentFolder, reason));
109        return id;
110    }
111
112    /**
113     * Checks whether the given publish job is OK, and throws an exception otherwise.<p>
114     *
115     * @param publishList the publish list
116     * @throws CmsException if there's something wrong with the publish job
117     */
118    public void checkPublishList(CmsPublishList publishList) throws CmsException {
119
120        for (CmsResource resource : publishList.getAllResources()) {
121            for (ForbiddenFolderEntry entry : m_forbiddenParentFolders.values()) {
122                if (CmsStringUtil.isPrefixPath(entry.getRootPath(), resource.getRootPath())) {
123                    throw new CmsPublishException(
124                        Messages.get().container(
125                            Messages.ERR_PUBLISH_FORBIDDEN_PARENT_FOLDER_3,
126                            resource.getRootPath(),
127                            entry.getRootPath(),
128                            entry.getReason()));
129                }
130            }
131
132        }
133    }
134
135    /**
136     * Removes the forbidden parent folder using the id obtained while it was added.<p>
137     *
138     * @param id the id to remove the parent folder
139     */
140    public void removeForbiddenParentFolder(CmsUUID id) {
141
142        m_forbiddenParentFolders.remove(id);
143    }
144
145}