1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.model.PortletApp;
23  import com.liferay.portal.model.PortletURLListener;
24  
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  import javax.portlet.PortletException;
29  import javax.portlet.PortletURLGenerationListener;
30  import javax.portlet.UnavailableException;
31  
32  /**
33   * <a href="PortletURLListenerFactory.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class PortletURLListenerFactory {
39  
40      public static PortletURLGenerationListener create(
41              PortletURLListener portletURLListener)
42          throws PortletException {
43  
44          return _instance._create(portletURLListener);
45      }
46  
47      public static void destroy(PortletURLListener portletURLListener) {
48          _instance._destroy(portletURLListener);
49      }
50  
51      private PortletURLListenerFactory() {
52          _pool = new ConcurrentHashMap
53              <String, Map<String, PortletURLGenerationListener>>();
54      }
55  
56      private PortletURLGenerationListener _create(
57              PortletURLListener portletURLListener)
58          throws PortletException {
59  
60          PortletApp portletApp = portletURLListener.getPortletApp();
61  
62          Map<String, PortletURLGenerationListener>
63              portletURLGenerationListeners = _pool.get(
64                  portletApp.getServletContextName());
65  
66          if (portletURLGenerationListeners == null) {
67              portletURLGenerationListeners =
68                  new ConcurrentHashMap<String, PortletURLGenerationListener>();
69  
70              _pool.put(
71                  portletApp.getServletContextName(),
72                  portletURLGenerationListeners);
73          }
74  
75          PortletURLGenerationListener portletURLGenerationListener =
76              portletURLGenerationListeners.get(
77                  portletURLListener.getListenerClass());
78  
79          if (portletURLGenerationListener == null) {
80              if (portletApp.isWARFile()) {
81                  PortletContextBag portletContextBag = PortletContextBagPool.get(
82                      portletApp.getServletContextName());
83  
84                  portletURLGenerationListener =
85                      portletContextBag.getPortletURLListeners().get(
86                          portletURLListener.getListenerClass());
87  
88                  portletURLGenerationListener = _init(
89                      portletURLListener, portletURLGenerationListener);
90              }
91              else {
92                  portletURLGenerationListener = _init(portletURLListener);
93              }
94  
95              portletURLGenerationListeners.put(
96                  portletURLListener.getListenerClass(),
97                  portletURLGenerationListener);
98          }
99  
100         return portletURLGenerationListener;
101     }
102 
103     private void _destroy(PortletURLListener portletURLListener) {
104         PortletApp portletApp = portletURLListener.getPortletApp();
105 
106         Map<String, PortletURLGenerationListener>
107             portletURLGenerationListeners = _pool.get(
108                 portletApp.getServletContextName());
109 
110         if (portletURLGenerationListeners == null) {
111             return;
112         }
113 
114         PortletURLGenerationListener portletURLGenerationListener =
115             portletURLGenerationListeners.get(
116                 portletURLListener.getListenerClass());
117 
118         if (portletURLGenerationListener == null) {
119             return;
120         }
121 
122         portletURLGenerationListeners.remove(
123             portletURLListener.getListenerClass());
124     }
125 
126     private PortletURLGenerationListener _init(
127             PortletURLListener portletURLListener)
128         throws PortletException {
129 
130         return _init(portletURLListener, null);
131     }
132 
133     private PortletURLGenerationListener _init(
134             PortletURLListener portletURLListener,
135             PortletURLGenerationListener portletURLGenerationListener)
136         throws PortletException {
137 
138         try {
139             if (portletURLGenerationListener == null) {
140                 portletURLGenerationListener =
141                     (PortletURLGenerationListener)Class.forName(
142                         portletURLListener.getListenerClass()).newInstance();
143             }
144         }
145         catch (ClassNotFoundException cnofe) {
146             throw new UnavailableException(cnofe.getMessage());
147         }
148         catch (InstantiationException ie) {
149             throw new UnavailableException(ie.getMessage());
150         }
151         catch (IllegalAccessException iae) {
152             throw new UnavailableException(iae.getMessage());
153         }
154 
155         return portletURLGenerationListener;
156     }
157 
158     private static PortletURLListenerFactory _instance =
159         new PortletURLListenerFactory();
160 
161     private Map<String, Map<String, PortletURLGenerationListener>> _pool;
162 
163 }