001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.ade.sitemap;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsUser;
032
033import java.util.concurrent.TimeUnit;
034
035import com.google.common.cache.Cache;
036import com.google.common.cache.CacheBuilder;
037
038/**
039 * A class used to keep track of which user is editing the alias table from which sites.<p>
040 *
041 * It consists of a table from site roots to users. The entries of the table will be removed
042 * if they haven't been updated in a given interval of time, but can also be removed manually.<p>
043 *
044 */
045public class CmsAliasEditorLockTable {
046
047    /**
048     * The interval after which a lock which has not been updated will be removed.<p>
049     */
050    public static final long TIMEOUT_INTERVAL = 60 * 1000;
051
052    /**
053     * Internal map from site roots to users.
054     */
055    private Cache<String, CmsUser> m_map;
056
057    /**
058     * Creates a new lock table instance.<p>
059     */
060    public CmsAliasEditorLockTable() {
061
062        CacheBuilder<Object, Object> mm = CacheBuilder.newBuilder().expireAfterWrite(
063            TIMEOUT_INTERVAL,
064            TimeUnit.MILLISECONDS);
065        m_map = mm.build();
066    }
067
068    /**
069     * Clears the entry for a given site root, but only if the user from the given CMS context matches the user in the entry.<p>
070     *
071     * @param cms the current CMS context
072     * @param siteRoot the site root for which the entry should be cleared
073     */
074    public void clear(CmsObject cms, String siteRoot) {
075
076        CmsUser originalUser = m_map.getIfPresent(siteRoot);
077        if ((originalUser == null) || !originalUser.equals(cms.getRequestContext().getCurrentUser())) {
078            return;
079        }
080        m_map.invalidate(siteRoot);
081    }
082
083    /**
084     * Tries to update or create an entry for the given user/site root combination.<p>
085     *
086     * If this method succeeds, it will return null, but if another user has created an entry for the site root,
087     * it will return that user.<p>
088     *
089     * @param cms the current CMS context
090     * @param siteRoot the site root
091     *
092     * @return null of the user who has already created an entry
093     */
094    public CmsUser update(CmsObject cms, String siteRoot) {
095
096        CmsUser originalUser = m_map.getIfPresent(siteRoot);
097        if ((originalUser == null) || originalUser.equals(cms.getRequestContext().getCurrentUser())) {
098            m_map.put(siteRoot, cms.getRequestContext().getCurrentUser());
099            return null;
100        }
101        return originalUser;
102    }
103}