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.toolbar;
016    
017    import com.liferay.portal.kernel.portlet.toolbar.contributor.PortletToolbarContributor;
018    import com.liferay.portal.kernel.portlet.toolbar.contributor.locator.PortletToolbarContributorLocator;
019    import com.liferay.portal.kernel.servlet.taglib.ui.Menu;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.registry.Registry;
023    import com.liferay.registry.RegistryUtil;
024    import com.liferay.registry.ServiceReference;
025    import com.liferay.registry.ServiceTracker;
026    import com.liferay.registry.ServiceTrackerCustomizer;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.List;
031    import java.util.concurrent.CopyOnWriteArrayList;
032    
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletResponse;
035    
036    /**
037     * Provides elements to be rendered in the portlet toolbar. To obtain those
038     * elements, it delegates the task to the {@link
039     * PortletToolbarContributorLocator} instances registered in OSGI.
040     *
041     * @author Sergio González
042     */
043    public class PortletToolbar {
044    
045            public PortletToolbar() {
046                    Registry registry = RegistryUtil.getRegistry();
047    
048                    _serviceTracker = registry.trackServices(
049                            PortletToolbarContributorLocator.class,
050                            new PortletToolbarServiceTrackerCustomizer());
051    
052                    _serviceTracker.open();
053            }
054    
055            public List<Menu> getPortletTitleMenus(
056                    String portletId, PortletRequest portletRequest,
057                    PortletResponse portletResponse) {
058    
059                    if ((portletRequest == null) || (portletResponse == null) ||
060                            Validator.isNull(portletId)) {
061    
062                            return Collections.emptyList();
063                    }
064    
065                    List<Menu> portletTitleMenus = new ArrayList<>();
066    
067                    for (PortletToolbarContributorLocator
068                                    portletToolbarContributorLocator :
069                                            _portletToolbarContributorLocators) {
070    
071                            List<PortletToolbarContributor> portletToolbarContributors =
072                                    portletToolbarContributorLocator.getPortletToolbarContributors(
073                                            portletId, portletRequest);
074    
075                            if (portletToolbarContributors == null) {
076                                    continue;
077                            }
078    
079                            for (PortletToolbarContributor portletToolbarContributor :
080                                            portletToolbarContributors) {
081    
082                                    List<Menu> curPortletTitleMenus =
083                                            portletToolbarContributor.getPortletTitleMenus(
084                                                    portletRequest, portletResponse);
085    
086                                    if (ListUtil.isEmpty(curPortletTitleMenus)) {
087                                            continue;
088                                    }
089    
090                                    portletTitleMenus.addAll(curPortletTitleMenus);
091                            }
092                    }
093    
094                    return portletTitleMenus;
095            }
096    
097            private static final List<PortletToolbarContributorLocator>
098                    _portletToolbarContributorLocators = new CopyOnWriteArrayList<>();
099    
100            private final ServiceTracker
101                    <PortletToolbarContributorLocator, PortletToolbarContributorLocator>
102                            _serviceTracker;
103    
104            private static class PortletToolbarServiceTrackerCustomizer
105                    implements ServiceTrackerCustomizer
106                            <PortletToolbarContributorLocator,
107                                    PortletToolbarContributorLocator> {
108    
109                    @Override
110                    public PortletToolbarContributorLocator addingService(
111                            ServiceReference<PortletToolbarContributorLocator>
112                                    serviceReference) {
113    
114                            Registry registry = RegistryUtil.getRegistry();
115    
116                            PortletToolbarContributorLocator portletToolbarContributorLocator =
117                                    registry.getService(serviceReference);
118    
119                            _portletToolbarContributorLocators.add(
120                                    portletToolbarContributorLocator);
121    
122                            return portletToolbarContributorLocator;
123                    }
124    
125                    @Override
126                    public void modifiedService(
127                            ServiceReference<PortletToolbarContributorLocator> serviceReference,
128                            PortletToolbarContributorLocator portletToolbarContributorLocator) {
129                    }
130    
131                    @Override
132                    public void removedService(
133                            ServiceReference<PortletToolbarContributorLocator> serviceReference,
134                            PortletToolbarContributorLocator portletToolbarContributorLocator) {
135    
136                            Registry registry = RegistryUtil.getRegistry();
137    
138                            registry.ungetService(serviceReference);
139    
140                            _portletToolbarContributorLocators.remove(
141                                    portletToolbarContributorLocator);
142                    }
143    
144            }
145    
146    }