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.portal.kernel.servlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
25  import com.liferay.portal.kernel.util.PortalInitable;
26  import com.liferay.portal.kernel.util.PortalInitableUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.IOException;
30  
31  import javax.servlet.Filter;
32  import javax.servlet.FilterChain;
33  import javax.servlet.FilterConfig;
34  import javax.servlet.ServletException;
35  import javax.servlet.ServletRequest;
36  import javax.servlet.ServletResponse;
37  
38  /**
39   * <a href="PortalClassLoaderFilter.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class PortalClassLoaderFilter implements Filter, PortalInitable {
45  
46      public void destroy() {
47          Thread currentThread = Thread.currentThread();
48  
49          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
50  
51          try {
52              currentThread.setContextClassLoader(
53                  PortalClassLoaderUtil.getClassLoader());
54  
55              _filter.destroy();
56          }
57          finally {
58              currentThread.setContextClassLoader(contextClassLoader);
59          }
60      }
61  
62      public void doFilter(
63              ServletRequest servletRequest, ServletResponse servletResponse,
64              FilterChain filterChain)
65          throws IOException, ServletException {
66  
67          Thread currentThread = Thread.currentThread();
68  
69          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
70  
71          try {
72              currentThread.setContextClassLoader(
73                  PortalClassLoaderUtil.getClassLoader());
74  
75              _filter.doFilter(servletRequest, servletResponse, filterChain);
76          }
77          finally {
78              currentThread.setContextClassLoader(contextClassLoader);
79          }
80      }
81  
82      public void init(FilterConfig filterConfig) {
83          _filterConfig = filterConfig;
84  
85          PortalInitableUtil.init(this);
86      }
87  
88      public void portalInit() {
89          try {
90              ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
91  
92              String filterClass = _filterConfig.getInitParameter("filter-class");
93  
94              if (filterClass.startsWith("com.liferay.filters.")) {
95                  filterClass = StringUtil.replace(
96                      filterClass, "com.liferay.filters.",
97                      "com.liferay.portal.servlet.filters.");
98              }
99  
100             _filter = (Filter)classLoader.loadClass(filterClass).newInstance();
101 
102             _filter.init(_filterConfig);
103         }
104         catch (Exception e) {
105             _log.error(e, e);
106         }
107     }
108 
109     private static Log _log =
110         LogFactoryUtil.getLog(PortalClassLoaderFilter.class);
111 
112     private Filter _filter;
113     private FilterConfig _filterConfig;
114 
115 }