001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.portlet.configuration.icon;
016    
017    import com.liferay.portal.kernel.portlet.configuration.icon.locator.PortletConfigurationIconLocator;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.theme.PortletDisplay;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.registry.collections.ServiceTrackerCollections;
025    import com.liferay.registry.collections.ServiceTrackerList;
026    import com.liferay.registry.collections.ServiceTrackerMap;
027    
028    import java.util.ArrayList;
029    import java.util.HashSet;
030    import java.util.List;
031    import java.util.Set;
032    
033    import javax.portlet.PortletRequest;
034    
035    /**
036     * @author Eudaldo Alonso
037     */
038    public class PortletConfigurationIconTracker {
039    
040            public static List<PortletConfigurationIconFactory>
041                    getPortletConfigurationIcons(PortletRequest portletRequest) {
042    
043                    List<PortletConfigurationIconFactory>
044                            portletConfigurationIconFactories = new ArrayList<>();
045    
046                    String portletId = getPortletId(portletRequest);
047    
048                    for (String path : getPaths(portletRequest)) {
049                            List<PortletConfigurationIconFactory>
050                                    portletPortletConfigurationIconFactories =
051                                            _serviceTrackerMap.getService(
052                                                    getKey(StringPool.STAR, path));
053    
054                            if (portletPortletConfigurationIconFactories != null) {
055                                    portletConfigurationIconFactories.addAll(
056                                            portletPortletConfigurationIconFactories);
057                            }
058    
059                            portletPortletConfigurationIconFactories =
060                                    _serviceTrackerMap.getService(getKey(portletId, path));
061    
062                            if (portletPortletConfigurationIconFactories != null) {
063                                    portletConfigurationIconFactories.addAll(
064                                            portletPortletConfigurationIconFactories);
065                            }
066                    }
067    
068                    return portletConfigurationIconFactories;
069            }
070    
071            protected static String getKey(String portletId, String path) {
072                    return portletId + StringPool.COLON + path;
073            }
074    
075            protected static Set<String> getPaths(PortletRequest portletRequest) {
076                    Set<String> paths = new HashSet<>();
077    
078                    String portletId = getPortletId(portletRequest);
079    
080                    for (PortletConfigurationIconLocator portletConfigurationIconLocator :
081                                    _serviceTrackerList) {
082    
083                            String path = portletConfigurationIconLocator.getPath(
084                                    portletRequest);
085    
086                            List<String> defaultViews =
087                                    portletConfigurationIconLocator.getDefaultViews(portletId);
088    
089                            String[] defaultViewsArray = ArrayUtil.toStringArray(defaultViews);
090    
091                            if (Validator.isNotNull(path) &&
092                                    !ArrayUtil.contains(defaultViewsArray, path)) {
093    
094                                    paths.add(path);
095    
096                                    continue;
097                            }
098    
099                            paths.addAll(defaultViews);
100                    }
101    
102                    return paths;
103            }
104    
105            protected static String getPortletId(PortletRequest portletRequest) {
106                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
107                            WebKeys.THEME_DISPLAY);
108    
109                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
110    
111                    return portletDisplay.getRootPortletId();
112            }
113    
114            private PortletConfigurationIconTracker() {
115                    _serviceTrackerMap.open();
116            }
117    
118            private static final ServiceTrackerList<PortletConfigurationIconLocator>
119                    _serviceTrackerList = ServiceTrackerCollections.list(
120                            PortletConfigurationIconLocator.class);
121            private static final ServiceTrackerMap
122                    <String, List<PortletConfigurationIconFactory>>
123                            _serviceTrackerMap = ServiceTrackerCollections.multiValueMap(
124                                    PortletConfigurationIconFactory.class, null,
125                                    new PortletConfigurationIconServiceReferenceMapper());
126    
127    }