1   /**
2    * Copyright (c) 2000-2008 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  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   */
47  public class PortalClassLoaderFilter implements Filter, PortalInitable {
48  
49      public void destroy() {
50          ClassLoader contextClassLoader =
51              Thread.currentThread().getContextClassLoader();
52  
53          try {
54              Thread.currentThread().setContextClassLoader(
55                  PortalClassLoaderUtil.getClassLoader());
56  
57              _filter.destroy();
58          }
59          finally {
60              Thread.currentThread().setContextClassLoader(contextClassLoader);
61          }
62      }
63  
64      public void doFilter(
65              ServletRequest servletRequest, ServletResponse servletResponse,
66              FilterChain filterChain)
67          throws IOException, ServletException {
68  
69          ClassLoader contextClassLoader =
70              Thread.currentThread().getContextClassLoader();
71  
72          try {
73              Thread.currentThread().setContextClassLoader(
74                  PortalClassLoaderUtil.getClassLoader());
75  
76              _filter.doFilter(servletRequest, servletResponse, filterChain);
77          }
78          finally {
79              Thread.currentThread().setContextClassLoader(contextClassLoader);
80          }
81      }
82  
83      public void init(FilterConfig filterConfig) {
84          _filterConfig = filterConfig;
85  
86          PortalInitableUtil.init(this);
87      }
88  
89      public void portalInit() {
90          try {
91              ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
92  
93              String filterClass = _filterConfig.getInitParameter("filter-class");
94  
95              if (filterClass.startsWith("com.liferay.filters.")) {
96                  filterClass = StringUtil.replace(
97                      filterClass, "com.liferay.filters.",
98                      "com.liferay.portal.servlet.filters.");
99              }
100 
101             _filter = (Filter)classLoader.loadClass(filterClass).newInstance();
102 
103             _filter.init(_filterConfig);
104         }
105         catch (Exception e) {
106             _log.error(e, e);
107         }
108     }
109 
110     private static Log _log =
111         LogFactoryUtil.getLog(PortalClassLoaderFilter.class);
112 
113     private Filter _filter;
114     private FilterConfig _filterConfig;
115 
116 }