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.SetUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.registry.collections.ServiceTrackerCollections;
023    import com.liferay.registry.collections.ServiceTrackerList;
024    import com.liferay.registry.collections.ServiceTrackerMap;
025    
026    import java.util.ArrayList;
027    import java.util.HashSet;
028    import java.util.List;
029    import java.util.Set;
030    
031    import javax.portlet.PortletRequest;
032    
033    /**
034     * @author Eudaldo Alonso
035     */
036    public class PortletConfigurationIconTracker {
037    
038            public static List<PortletConfigurationIconFactory>
039                    getPortletConfigurationIcons(
040                            String portletId, PortletRequest portletRequest) {
041    
042                    List<PortletConfigurationIconFactory>
043                            portletConfigurationIconFactories = new ArrayList<>();
044    
045                    for (String path : getPaths(portletId, portletRequest)) {
046                            List<PortletConfigurationIconFactory>
047                                    portletPortletConfigurationIconFactories =
048                                            _serviceTrackerMap.getService(
049                                                    getKey(StringPool.STAR, path));
050    
051                            if (portletPortletConfigurationIconFactories != null) {
052                                    portletConfigurationIconFactories.addAll(
053                                            portletPortletConfigurationIconFactories);
054                            }
055    
056                            portletPortletConfigurationIconFactories =
057                                    _serviceTrackerMap.getService(getKey(portletId, path));
058    
059                            if (portletPortletConfigurationIconFactories != null) {
060                                    portletConfigurationIconFactories.addAll(
061                                            portletPortletConfigurationIconFactories);
062                            }
063                    }
064    
065                    return portletConfigurationIconFactories;
066            }
067    
068            protected static String getKey(String portletId, String path) {
069                    return portletId + StringPool.COLON + path;
070            }
071    
072            protected static Set<String> getPaths(
073                    String portletId, PortletRequest portletRequest) {
074    
075                    Set<String> paths = new HashSet<>();
076    
077                    for (PortletConfigurationIconLocator portletConfigurationIconLocator :
078                                    _serviceTrackerList) {
079    
080                            String path = portletConfigurationIconLocator.getPath(
081                                    portletRequest);
082    
083                            List<String> defaultViews =
084                                    portletConfigurationIconLocator.getDefaultViews(portletId);
085    
086                            String[] defaultViewsArray = ArrayUtil.toStringArray(defaultViews);
087    
088                            if (Validator.isNotNull(path)) {
089                                    paths.add(path);
090    
091                                    if (ArrayUtil.isNotEmpty(defaultViewsArray) &&
092                                            ArrayUtil.contains(defaultViewsArray, path)) {
093    
094                                            paths.add(StringPool.DASH);
095                                    }
096                            }
097                    }
098    
099                    if (SetUtil.isEmpty(paths)) {
100                            paths.add(StringPool.DASH);
101                    }
102    
103                    return paths;
104            }
105    
106            private static final ServiceTrackerList<PortletConfigurationIconLocator>
107                    _serviceTrackerList = ServiceTrackerCollections.openList(
108                            PortletConfigurationIconLocator.class);
109            private static final ServiceTrackerMap
110                    <String, List<PortletConfigurationIconFactory>>
111                            _serviceTrackerMap = ServiceTrackerCollections.openMultiValueMap(
112                                    PortletConfigurationIconFactory.class, null,
113                                    new PortletConfigurationIconServiceReferenceMapper());
114    
115    }