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