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.editor.configuration;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.registry.ServiceReference;
022    import com.liferay.registry.collections.ServiceReferenceMapper;
023    import com.liferay.registry.collections.ServiceTrackerMap;
024    import com.liferay.registry.util.StringPlus;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Sergio Gonz??lez
031     */
032    public abstract class BaseEditorConfigurationProvider<T> {
033    
034            protected static String getKey(
035                    String portletName, String editorConfigKey, String editorName) {
036    
037                    if (Validator.isNull(portletName)) {
038                            portletName = "null";
039                    }
040    
041                    if (Validator.isNull(editorConfigKey)) {
042                            editorConfigKey = "null";
043                    }
044    
045                    if (Validator.isNull(editorName)) {
046                            editorName = "null";
047                    }
048    
049                    StringBundler sb = new StringBundler(5);
050    
051                    sb.append(portletName);
052                    sb.append(StringPool.PERIOD);
053                    sb.append(editorConfigKey);
054                    sb.append(StringPool.PERIOD);
055                    sb.append(editorName);
056    
057                    return sb.toString();
058            }
059    
060            protected List<T> getContributors(
061                    String portletName, String editorConfigKey, String editorName) {
062    
063                    List<T> contributors = new ArrayList<>();
064    
065                    populateContributor(
066                            contributors, getKey(portletName, editorConfigKey, editorName));
067    
068                    populateContributor(
069                            contributors, getKey(portletName, editorConfigKey, null));
070    
071                    populateContributor(
072                            contributors, getKey(null, editorConfigKey, editorName));
073    
074                    populateContributor(
075                            contributors, getKey(portletName, null, editorName));
076    
077                    populateContributor(contributors, getKey(null, editorConfigKey, null));
078    
079                    populateContributor(contributors, getKey(portletName, null, null));
080    
081                    populateContributor(contributors, getKey(null, null, editorName));
082    
083                    return contributors;
084            }
085    
086            protected abstract ServiceTrackerMap<String, List<T>>
087                    getServiceTrackerMap();
088    
089            protected void populateContributor(List<T> contributors, String key) {
090                    ServiceTrackerMap<String, List<T>> serviceTrackerMap =
091                            getServiceTrackerMap();
092    
093                    List<T> curContributors = serviceTrackerMap.getService(key);
094    
095                    if (ListUtil.isNotEmpty(curContributors)) {
096                            contributors.addAll(curContributors);
097                    }
098            }
099    
100            protected static class EditorServiceReferenceMapper<T>
101                    implements ServiceReferenceMapper<String, T> {
102    
103                    @Override
104                    public void map(
105                            ServiceReference<T> serviceReference, Emitter<String> emitter) {
106    
107                            List<String> portletNames = StringPlus.asList(
108                                    serviceReference.getProperty("javax.portlet.name"));
109    
110                            if (portletNames.isEmpty()) {
111                                    portletNames.add(StringPool.BLANK);
112                            }
113    
114                            List<String> editorConfigKeys = StringPlus.asList(
115                                    serviceReference.getProperty("editor.config.key"));
116    
117                            if (editorConfigKeys.isEmpty()) {
118                                    editorConfigKeys.add(StringPool.BLANK);
119                            }
120    
121                            List<String> editorNames = StringPlus.asList(
122                                    serviceReference.getProperty("editor.name"));
123    
124                            if (editorNames.isEmpty()) {
125                                    editorNames.add(StringPool.BLANK);
126                            }
127    
128                            for (String portletName : portletNames) {
129                                    for (String editorConfigKey : editorConfigKeys) {
130                                            for (String editorName : editorNames) {
131                                                    emitter.emit(
132                                                            getKey(portletName, editorConfigKey, editorName));
133                                            }
134                                    }
135                            }
136                    }
137    
138            }
139    
140    }