001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.ui.contextmenu;
029
030import org.opencms.main.CmsLog;
031
032import java.util.Collections;
033import java.util.Iterator;
034import java.util.List;
035import java.util.ServiceLoader;
036
037import org.apache.commons.logging.Log;
038
039import com.google.common.collect.ClassToInstanceMap;
040import com.google.common.collect.Lists;
041import com.google.common.collect.MutableClassToInstanceMap;
042
043/**
044 * Class used to manage multiple context menu item providers through a single instance.<p>
045 *
046 * Also keeps track of available menu entries by their global id.<p>
047 */
048public class CmsContextMenuItemProviderGroup implements I_CmsContextMenuItemProvider {
049
050    /** Logger instance for this class. */
051    private static final Log LOG = CmsLog.getLog(CmsContextMenuItemProviderGroup.class);
052
053    /** Map of provider classes. */
054    ClassToInstanceMap<I_CmsContextMenuItemProvider> m_providerMap = MutableClassToInstanceMap.create();
055
056    /** Item cache. */
057    private List<I_CmsContextMenuItem> m_itemCache = Lists.newArrayList();
058
059    /**
060     * Creates a new instance.<p>
061     */
062    public CmsContextMenuItemProviderGroup() {
063
064        Iterator<I_CmsContextMenuItemProvider> providersIt = ServiceLoader.load(
065            I_CmsContextMenuItemProvider.class).iterator();
066        while (providersIt.hasNext()) {
067            try {
068                addProvider(providersIt.next());
069            } catch (Throwable t) {
070                LOG.error("Error loading context menu provider from classpath.", t);
071            }
072        }
073    }
074
075    /**
076     * Adds a new provider class.<p>
077     *
078     * @param providerClass the provider class
079     */
080    public void addProvider(Class<? extends I_CmsContextMenuItemProvider> providerClass) {
081
082        try {
083            m_providerMap.put(providerClass, providerClass.newInstance());
084        } catch (Exception e) {
085            LOG.error(e.getLocalizedMessage(), e);
086        }
087    }
088
089    /**
090     * Adds a provider.<p>
091     *
092     * Note that no two providers of the same exact class may be added.<p>
093     *
094     * @param instance the provider instance to add
095     */
096    public void addProvider(I_CmsContextMenuItemProvider instance) {
097
098        m_providerMap.put(instance.getClass(), instance);
099
100    }
101
102    /**
103     * @see org.opencms.ui.contextmenu.I_CmsContextMenuItemProvider#getMenuItems()
104     */
105    public List<I_CmsContextMenuItem> getMenuItems() {
106
107        return Collections.unmodifiableList(m_itemCache);
108    }
109
110    /**
111     * Initializes this instance.<p>
112     *
113     * This must be called after the provider classes have been added.
114     */
115    public synchronized void initialize() {
116
117        List<I_CmsContextMenuItem> result = Lists.newArrayList();
118        for (I_CmsContextMenuItemProvider provider : m_providerMap.values()) {
119            result.addAll(provider.getMenuItems());
120        }
121        m_itemCache = result;
122    }
123
124}