001    /**
002     * Copyright (c) 2000-2013 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.portlet;
016    
017    import com.liferay.portal.kernel.util.InstanceFactory;
018    import com.liferay.portal.model.PortletApp;
019    import com.liferay.portal.model.PortletURLListener;
020    
021    import java.util.Map;
022    import java.util.concurrent.ConcurrentHashMap;
023    
024    import javax.portlet.PortletException;
025    import javax.portlet.PortletURLGenerationListener;
026    import javax.portlet.UnavailableException;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class PortletURLListenerFactory {
032    
033            public static PortletURLGenerationListener create(
034                            PortletURLListener portletURLListener)
035                    throws PortletException {
036    
037                    return _instance._create(portletURLListener);
038            }
039    
040            public static void destroy(PortletURLListener portletURLListener) {
041                    _instance._destroy(portletURLListener);
042            }
043    
044            private PortletURLListenerFactory() {
045                    _pool = new ConcurrentHashMap
046                            <String, Map<String, PortletURLGenerationListener>>();
047            }
048    
049            private PortletURLGenerationListener _create(
050                            PortletURLListener portletURLListener)
051                    throws PortletException {
052    
053                    PortletApp portletApp = portletURLListener.getPortletApp();
054    
055                    Map<String, PortletURLGenerationListener>
056                            portletURLGenerationListeners = _pool.get(
057                                    portletApp.getServletContextName());
058    
059                    if (portletURLGenerationListeners == null) {
060                            portletURLGenerationListeners =
061                                    new ConcurrentHashMap<String, PortletURLGenerationListener>();
062    
063                            _pool.put(
064                                    portletApp.getServletContextName(),
065                                    portletURLGenerationListeners);
066                    }
067    
068                    PortletURLGenerationListener portletURLGenerationListener =
069                            portletURLGenerationListeners.get(
070                                    portletURLListener.getListenerClass());
071    
072                    if (portletURLGenerationListener != null) {
073                            return portletURLGenerationListener;
074                    }
075    
076                    if (portletApp.isWARFile()) {
077                            PortletContextBag portletContextBag = PortletContextBagPool.get(
078                                    portletApp.getServletContextName());
079    
080                            portletURLGenerationListener =
081                                    portletContextBag.getPortletURLListeners().get(
082                                            portletURLListener.getListenerClass());
083    
084                            portletURLGenerationListener = _init(
085                                    portletURLListener, portletURLGenerationListener);
086                    }
087                    else {
088                            portletURLGenerationListener = _init(portletURLListener);
089                    }
090    
091                    portletURLGenerationListeners.put(
092                            portletURLListener.getListenerClass(),
093                            portletURLGenerationListener);
094    
095                    return portletURLGenerationListener;
096            }
097    
098            private void _destroy(PortletURLListener portletURLListener) {
099                    PortletApp portletApp = portletURLListener.getPortletApp();
100    
101                    Map<String, PortletURLGenerationListener>
102                            portletURLGenerationListeners = _pool.get(
103                                    portletApp.getServletContextName());
104    
105                    if (portletURLGenerationListeners == null) {
106                            return;
107                    }
108    
109                    PortletURLGenerationListener portletURLGenerationListener =
110                            portletURLGenerationListeners.get(
111                                    portletURLListener.getListenerClass());
112    
113                    if (portletURLGenerationListener == null) {
114                            return;
115                    }
116    
117                    portletURLGenerationListeners.remove(
118                            portletURLListener.getListenerClass());
119            }
120    
121            private PortletURLGenerationListener _init(
122                            PortletURLListener portletURLListener)
123                    throws PortletException {
124    
125                    return _init(portletURLListener, null);
126            }
127    
128            private PortletURLGenerationListener _init(
129                            PortletURLListener portletURLListener,
130                            PortletURLGenerationListener portletURLGenerationListener)
131                    throws PortletException {
132    
133                    try {
134                            if (portletURLGenerationListener == null) {
135                                    portletURLGenerationListener =
136                                            (PortletURLGenerationListener)InstanceFactory.newInstance(
137                                                    portletURLListener.getListenerClass());
138                            }
139                    }
140                    catch (Exception e) {
141                            throw new UnavailableException(e.getMessage());
142                    }
143    
144                    return portletURLGenerationListener;
145            }
146    
147            private static PortletURLListenerFactory _instance =
148                    new PortletURLListenerFactory();
149    
150            private Map<String, Map<String, PortletURLGenerationListener>> _pool;
151    
152    }