1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.util.InstanceFactory;
18  import com.liferay.portal.model.PortletApp;
19  import com.liferay.portal.model.PortletURLListener;
20  
21  import java.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import javax.portlet.PortletException;
25  import javax.portlet.PortletURLGenerationListener;
26  import javax.portlet.UnavailableException;
27  
28  /**
29   * <a href="PortletURLListenerFactory.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class PortletURLListenerFactory {
34  
35      public static PortletURLGenerationListener create(
36              PortletURLListener portletURLListener)
37          throws PortletException {
38  
39          return _instance._create(portletURLListener);
40      }
41  
42      public static void destroy(PortletURLListener portletURLListener) {
43          _instance._destroy(portletURLListener);
44      }
45  
46      private PortletURLListenerFactory() {
47          _pool = new ConcurrentHashMap
48              <String, Map<String, PortletURLGenerationListener>>();
49      }
50  
51      private PortletURLGenerationListener _create(
52              PortletURLListener portletURLListener)
53          throws PortletException {
54  
55          PortletApp portletApp = portletURLListener.getPortletApp();
56  
57          Map<String, PortletURLGenerationListener>
58              portletURLGenerationListeners = _pool.get(
59                  portletApp.getServletContextName());
60  
61          if (portletURLGenerationListeners == null) {
62              portletURLGenerationListeners =
63                  new ConcurrentHashMap<String, PortletURLGenerationListener>();
64  
65              _pool.put(
66                  portletApp.getServletContextName(),
67                  portletURLGenerationListeners);
68          }
69  
70          PortletURLGenerationListener portletURLGenerationListener =
71              portletURLGenerationListeners.get(
72                  portletURLListener.getListenerClass());
73  
74          if (portletURLGenerationListener == null) {
75              if (portletApp.isWARFile()) {
76                  PortletContextBag portletContextBag = PortletContextBagPool.get(
77                      portletApp.getServletContextName());
78  
79                  portletURLGenerationListener =
80                      portletContextBag.getPortletURLListeners().get(
81                          portletURLListener.getListenerClass());
82  
83                  portletURLGenerationListener = _init(
84                      portletURLListener, portletURLGenerationListener);
85              }
86              else {
87                  portletURLGenerationListener = _init(portletURLListener);
88              }
89  
90              portletURLGenerationListeners.put(
91                  portletURLListener.getListenerClass(),
92                  portletURLGenerationListener);
93          }
94  
95          return portletURLGenerationListener;
96      }
97  
98      private void _destroy(PortletURLListener portletURLListener) {
99          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 }