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