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.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        Iterator<I_CmsContextMenuItemProvider> providersIt = ServiceLoader.load(
064            I_CmsContextMenuItemProvider.class).iterator();
065        while (providersIt.hasNext()) {
066            try {
067                addProvider(providersIt.next());
068            } catch (Throwable t) {
069                LOG.error("Error loading context menu provider from classpath.", t);
070            }
071        }
072    }
073
074    /**
075     * Adds a new provider class.<p>
076     *
077     * @param providerClass the provider class
078     */
079    public void addProvider(Class<? extends I_CmsContextMenuItemProvider> providerClass) {
080
081        try {
082            m_providerMap.put(providerClass, providerClass.newInstance());
083        } catch (Exception e) {
084            LOG.error(e.getLocalizedMessage(), e);
085        }
086    }
087
088    /**
089     * Adds a provider.<p>
090     *
091     * Note that no two providers of the same exact class may be added.<p>
092     *
093     * @param instance the provider instance to add
094     */
095    public void addProvider(I_CmsContextMenuItemProvider instance) {
096
097        m_providerMap.put(instance.getClass(), instance);
098
099    }
100
101    /**
102     * @see org.opencms.ui.contextmenu.I_CmsContextMenuItemProvider#getMenuItems()
103     */
104    public List<I_CmsContextMenuItem> getMenuItems() {
105
106        return Collections.unmodifiableList(m_itemCache);
107    }
108
109    /**
110     * Initializes this instance.<p>
111     *
112     * This must be called after the provider classes have been added.
113     */
114    public synchronized void initialize() {
115
116        List<I_CmsContextMenuItem> result = Lists.newArrayList();
117        for (I_CmsContextMenuItemProvider provider : m_providerMap.values()) {
118            result.addAll(provider.getMenuItems());
119        }
120        m_itemCache = result;
121    }
122
123}