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  
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import javax.portlet.PortletContext;
31  import javax.portlet.PortletException;
32  import javax.portlet.UnavailableException;
33  import javax.portlet.filter.FilterConfig;
34  import javax.portlet.filter.PortletFilter;
35  
36  /**
37   * <a href="PortletFilterFactory.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PortletFilterFactory {
43  
44      public static PortletFilter create(
45              com.liferay.portal.model.PortletFilter portletFilterModel,
46              PortletContext ctx)
47          throws PortletException {
48  
49          return _instance._create(portletFilterModel, ctx);
50      }
51  
52      public static void destroy(
53          com.liferay.portal.model.PortletFilter portletFilterModel) {
54  
55          _instance._destroy(portletFilterModel);
56      }
57  
58      private PortletFilterFactory() {
59          _pool = new ConcurrentHashMap<String, Map<String, PortletFilter>>();
60      }
61  
62      private PortletFilter _create(
63              com.liferay.portal.model.PortletFilter portletFilterModel,
64              PortletContext ctx)
65          throws PortletException {
66  
67          PortletApp portletApp = portletFilterModel.getPortletApp();
68  
69          Map<String, PortletFilter> portletFilters = _pool.get(
70              portletApp.getServletContextName());
71  
72          if (portletFilters == null) {
73              portletFilters = new ConcurrentHashMap<String, PortletFilter>();
74  
75              _pool.put(portletApp.getServletContextName(), portletFilters);
76          }
77  
78          PortletFilter portletFilter = portletFilters.get(
79              portletFilterModel.getFilterName());
80  
81          if (portletFilter == null) {
82              FilterConfig filterConfig =
83                  FilterConfigFactory.create(portletFilterModel, ctx);
84  
85              if (portletApp.isWARFile()) {
86                  PortletContextBag portletContextBag = PortletContextBagPool.get(
87                      portletApp.getServletContextName());
88  
89                  portletFilter = portletContextBag.getPortletFilters().get(
90                      portletFilterModel.getFilterName());
91  
92                  portletFilter = _init(
93                      portletFilterModel, filterConfig, portletFilter);
94              }
95              else {
96                  portletFilter = _init(portletFilterModel, filterConfig);
97              }
98  
99              portletFilters.put(
100                 portletFilterModel.getFilterName(), portletFilter);
101         }
102 
103         return portletFilter;
104     }
105 
106     private void _destroy(
107         com.liferay.portal.model.PortletFilter portletFilterModel) {
108 
109         PortletApp portletApp = portletFilterModel.getPortletApp();
110 
111         Map<String, PortletFilter> portletFilters = _pool.get(
112             portletApp.getServletContextName());
113 
114         if (portletFilters == null) {
115             return;
116         }
117 
118         PortletFilter portletFilter = portletFilters.get(
119             portletFilterModel.getFilterName());
120 
121         if (portletFilter == null) {
122             return;
123         }
124 
125         portletFilter.destroy();
126 
127         portletFilters.remove(portletFilterModel.getFilterName());
128     }
129 
130     private PortletFilter _init(
131             com.liferay.portal.model.PortletFilter portletFilterModel,
132             FilterConfig filterConfig)
133         throws PortletException {
134 
135         return _init(portletFilterModel, filterConfig, null);
136     }
137 
138     private PortletFilter _init(
139             com.liferay.portal.model.PortletFilter portletFilterModel,
140             FilterConfig filterConfig, PortletFilter portletFilter)
141         throws PortletException {
142 
143         try {
144             if (portletFilter == null) {
145                 portletFilter = (PortletFilter)Class.forName(
146                     portletFilterModel.getFilterClass()).newInstance();
147             }
148 
149             portletFilter.init(filterConfig);
150         }
151         catch (ClassNotFoundException cnofe) {
152             throw new UnavailableException(cnofe.getMessage());
153         }
154         catch (InstantiationException ie) {
155             throw new UnavailableException(ie.getMessage());
156         }
157         catch (IllegalAccessException iae) {
158             throw new UnavailableException(iae.getMessage());
159         }
160 
161         return portletFilter;
162     }
163 
164     private static PortletFilterFactory _instance = new PortletFilterFactory();
165 
166     private Map<String, Map<String, PortletFilter>> _pool;
167 
168 }