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