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