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;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.registry.collections.ServiceTrackerCollections;
021    import com.liferay.registry.collections.ServiceTrackerMap;
022    
023    import javax.portlet.PortletRequest;
024    import javax.portlet.PortletURL;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Eudaldo Alonso
030     */
031    public class PortletProviderUtil {
032    
033            public static String getPortletId(
034                    String className, PortletProvider.Action action) {
035    
036                    PortletProvider portletProvider = getPortletProvider(className, action);
037    
038                    if (portletProvider != null) {
039                            return portletProvider.getPortletId();
040                    }
041    
042                    return StringPool.BLANK;
043            }
044    
045            public static PortletURL getPortletURL(
046                            HttpServletRequest request, String className,
047                            PortletProvider.Action action)
048                    throws PortalException {
049    
050                    PortletProvider portletProvider = getPortletProvider(className, action);
051    
052                    if (portletProvider != null) {
053                            return portletProvider.getPortletURL(request);
054                    }
055    
056                    return null;
057            }
058    
059            public static PortletURL getPortletURL(
060                            PortletRequest portletRequest, String className,
061                            PortletProvider.Action action)
062                    throws PortalException {
063    
064                    return getPortletURL(
065                            PortalUtil.getHttpServletRequest(portletRequest), className,
066                            action);
067            }
068    
069            protected static PortletProvider getPortletProvider(
070                    String className, PortletProvider.Action action) {
071    
072                    PortletProvider portletProvider = null;
073    
074                    if (action.equals(PortletProvider.Action.ADD)) {
075                            portletProvider = _addServiceTrackerMap.getService(className);
076    
077                            if (portletProvider == null) {
078                                    portletProvider = _addServiceTrackerMap.getService(
079                                            PortletProvider.CLASS_NAME_ANY);
080                            }
081                    }
082                    else if (action.equals(PortletProvider.Action.BROWSE)) {
083                            portletProvider = _browseServiceTrackerMap.getService(className);
084    
085                            if (portletProvider == null) {
086                                    portletProvider = _browseServiceTrackerMap.getService(
087                                            PortletProvider.CLASS_NAME_ANY);
088                            }
089                    }
090                    else if (action.equals(PortletProvider.Action.EDIT)) {
091                            portletProvider = _editServiceTrackerMap.getService(className);
092    
093                            if (portletProvider == null) {
094                                    portletProvider = _editServiceTrackerMap.getService(
095                                            PortletProvider.CLASS_NAME_ANY);
096                            }
097                    }
098                    else if (action.equals(PortletProvider.Action.VIEW)) {
099                            portletProvider = _viewServiceTrackerMap.getService(className);
100    
101                            if (portletProvider == null) {
102                                    portletProvider = _viewServiceTrackerMap.getService(
103                                            PortletProvider.CLASS_NAME_ANY);
104                            }
105                    }
106    
107                    return portletProvider;
108            }
109    
110            private static final ServiceTrackerMap<String, AddPortletProvider>
111                    _addServiceTrackerMap = ServiceTrackerCollections.singleValueMap(
112                            AddPortletProvider.class, "model.class.name");
113            private static final ServiceTrackerMap<String, BrowsePortletProvider>
114                    _browseServiceTrackerMap = ServiceTrackerCollections.singleValueMap(
115                            BrowsePortletProvider.class, "model.class.name");
116            private static final ServiceTrackerMap<String, EditPortletProvider>
117                    _editServiceTrackerMap = ServiceTrackerCollections.singleValueMap(
118                            EditPortletProvider.class, "model.class.name");
119            private static final ServiceTrackerMap<String, ViewPortletProvider>
120                    _viewServiceTrackerMap = ServiceTrackerCollections.singleValueMap(
121                            ViewPortletProvider.class, "model.class.name");
122    
123            static {
124                    _addServiceTrackerMap.open();
125                    _browseServiceTrackerMap.open();
126                    _editServiceTrackerMap.open();
127                    _viewServiceTrackerMap.open();
128            }
129    
130    }