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