001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            if (portletApp.isWARFile()) {
074                                    PortletContextBag portletContextBag = PortletContextBagPool.get(
075                                            portletApp.getServletContextName());
076    
077                                    portletURLGenerationListener =
078                                            portletContextBag.getPortletURLListeners().get(
079                                                    portletURLListener.getListenerClass());
080    
081                                    portletURLGenerationListener = _init(
082                                            portletURLListener, portletURLGenerationListener);
083                            }
084                            else {
085                                    portletURLGenerationListener = _init(portletURLListener);
086                            }
087    
088                            portletURLGenerationListeners.put(
089                                    portletURLListener.getListenerClass(),
090                                    portletURLGenerationListener);
091                    }
092    
093                    return portletURLGenerationListener;
094            }
095    
096            private void _destroy(PortletURLListener portletURLListener) {
097                    PortletApp portletApp = portletURLListener.getPortletApp();
098    
099                    Map<String, PortletURLGenerationListener>
100                            portletURLGenerationListeners = _pool.get(
101                                    portletApp.getServletContextName());
102    
103                    if (portletURLGenerationListeners == null) {
104                            return;
105                    }
106    
107                    PortletURLGenerationListener portletURLGenerationListener =
108                            portletURLGenerationListeners.get(
109                                    portletURLListener.getListenerClass());
110    
111                    if (portletURLGenerationListener == null) {
112                            return;
113                    }
114    
115                    portletURLGenerationListeners.remove(
116                            portletURLListener.getListenerClass());
117            }
118    
119            private PortletURLGenerationListener _init(
120                            PortletURLListener portletURLListener)
121                    throws PortletException {
122    
123                    return _init(portletURLListener, null);
124            }
125    
126            private PortletURLGenerationListener _init(
127                            PortletURLListener portletURLListener,
128                            PortletURLGenerationListener portletURLGenerationListener)
129                    throws PortletException {
130    
131                    try {
132                            if (portletURLGenerationListener == null) {
133                                    portletURLGenerationListener =
134                                            (PortletURLGenerationListener)InstanceFactory.newInstance(
135                                                    portletURLListener.getListenerClass());
136                            }
137                    }
138                    catch (Exception e) {
139                            throw new UnavailableException(e.getMessage());
140                    }
141    
142                    return portletURLGenerationListener;
143            }
144    
145            private static PortletURLListenerFactory _instance =
146                    new PortletURLListenerFactory();
147    
148            private Map<String, Map<String, PortletURLGenerationListener>> _pool;
149    
150    }