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