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.workplace.explorer;
029
030import org.opencms.main.CmsEvent;
031import org.opencms.main.I_CmsEventListener;
032import org.opencms.main.OpenCms;
033
034import java.lang.ref.WeakReference;
035import java.util.ArrayList;
036import java.util.Iterator;
037import java.util.List;
038
039/**
040 * Class which listens for cache flush events to uncache cached access control settings.<p>
041 */
042public class CmsExplorerTypeAccessFlushListener implements I_CmsEventListener {
043
044    /** List of weak references to the access settings. */
045    private List<WeakReference<CmsExplorerTypeAccess>> m_contents = new ArrayList<WeakReference<CmsExplorerTypeAccess>>();
046
047    /**
048     * Adds a new access settings object to this listener.<p>
049     *
050     * @param access the access settings
051     */
052    public synchronized void add(CmsExplorerTypeAccess access) {
053
054        m_contents.add(new WeakReference<CmsExplorerTypeAccess>(access));
055    }
056
057    /**
058     * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent)
059     */
060    public void cmsEvent(CmsEvent event) {
061
062        switch (event.getType()) {
063            case I_CmsEventListener.EVENT_CLEAR_CACHES:
064            case I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES:
065            case I_CmsEventListener.EVENT_CLEAR_ONLINE_CACHES:
066                doFlush();
067                break;
068            default: // ignore;
069        }
070    }
071
072    /**
073     * Installs this instance as an event listener.<p>
074     */
075    public void install() {
076
077        OpenCms.getEventManager().addCmsEventListener(
078            this,
079            new int[] {
080                I_CmsEventListener.EVENT_CLEAR_CACHES,
081                I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES,
082                I_CmsEventListener.EVENT_CLEAR_ONLINE_CACHES});
083    }
084
085    /**
086     * Flushes the cache for all registered access setting objects.<p>
087     */
088    protected synchronized void doFlush() {
089
090        Iterator<WeakReference<CmsExplorerTypeAccess>> iter = m_contents.iterator();
091        while (iter.hasNext()) {
092            WeakReference<CmsExplorerTypeAccess> ref = iter.next();
093            CmsExplorerTypeAccess access = ref.get();
094            if (access == null) {
095                iter.remove();
096            } else {
097                access.flushCache();
098            }
099        }
100    }
101
102}