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