1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletURLListenerFactory {
41  
42      public static PortletURLGenerationListener create(
43              PortletURLListener portletURLListener)
44          throws PortletException {
45  
46          return _instance._create(portletURLListener);
47      }
48  
49      public static void destroy(PortletURLListener portletURLListener) {
50          _instance._destroy(portletURLListener);
51      }
52  
53      private PortletURLListenerFactory() {
54          _pool = new ConcurrentHashMap
55              <String, Map<String, PortletURLGenerationListener>>();
56      }
57  
58      private PortletURLGenerationListener _create(
59              PortletURLListener portletURLListener)
60          throws PortletException {
61  
62          PortletApp portletApp = portletURLListener.getPortletApp();
63  
64          Map<String, PortletURLGenerationListener>
65              portletURLGenerationListeners = _pool.get(
66                  portletApp.getServletContextName());
67  
68          if (portletURLGenerationListeners == null) {
69              portletURLGenerationListeners =
70                  new ConcurrentHashMap<String, PortletURLGenerationListener>();
71  
72              _pool.put(
73                  portletApp.getServletContextName(),
74                  portletURLGenerationListeners);
75          }
76  
77          PortletURLGenerationListener portletURLGenerationListener =
78              portletURLGenerationListeners.get(
79                  portletURLListener.getListenerClass());
80  
81          if (portletURLGenerationListener == null) {
82              if (portletApp.isWARFile()) {
83                  PortletContextBag portletContextBag = PortletContextBagPool.get(
84                      portletApp.getServletContextName());
85  
86                  portletURLGenerationListener =
87                      portletContextBag.getPortletURLListeners().get(
88                          portletURLListener.getListenerClass());
89  
90                  portletURLGenerationListener = _init(
91                      portletURLListener, portletURLGenerationListener);
92              }
93              else {
94                  portletURLGenerationListener = _init(portletURLListener);
95              }
96  
97              portletURLGenerationListeners.put(
98                  portletURLListener.getListenerClass(),
99                  portletURLGenerationListener);
100         }
101 
102         return portletURLGenerationListener;
103     }
104 
105     private void _destroy(PortletURLListener portletURLListener) {
106         PortletApp portletApp = portletURLListener.getPortletApp();
107 
108         Map<String, PortletURLGenerationListener>
109             portletURLGenerationListeners = _pool.get(
110                 portletApp.getServletContextName());
111 
112         if (portletURLGenerationListeners == null) {
113             return;
114         }
115 
116         PortletURLGenerationListener portletURLGenerationListener =
117             portletURLGenerationListeners.get(
118                 portletURLListener.getListenerClass());
119 
120         if (portletURLGenerationListener == null) {
121             return;
122         }
123 
124         portletURLGenerationListeners.remove(
125             portletURLListener.getListenerClass());
126     }
127 
128     private PortletURLGenerationListener _init(
129             PortletURLListener portletURLListener)
130         throws PortletException {
131 
132         return _init(portletURLListener, null);
133     }
134 
135     private PortletURLGenerationListener _init(
136             PortletURLListener portletURLListener,
137             PortletURLGenerationListener portletURLGenerationListener)
138         throws PortletException {
139 
140         try {
141             if (portletURLGenerationListener == null) {
142                 portletURLGenerationListener =
143                     (PortletURLGenerationListener)Class.forName(
144                         portletURLListener.getListenerClass()).newInstance();
145             }
146         }
147         catch (ClassNotFoundException cnofe) {
148             throw new UnavailableException(cnofe.getMessage());
149         }
150         catch (InstantiationException ie) {
151             throw new UnavailableException(ie.getMessage());
152         }
153         catch (IllegalAccessException iae) {
154             throw new UnavailableException(iae.getMessage());
155         }
156 
157         return portletURLGenerationListener;
158     }
159 
160     private static PortletURLListenerFactory _instance =
161         new PortletURLListenerFactory();
162 
163     private Map<String, Map<String, PortletURLGenerationListener>> _pool;
164 
165 }