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