1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.servlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
25  import com.liferay.portal.kernel.util.PortalInitable;
26  import com.liferay.portal.kernel.util.PortalInitableUtil;
27  
28  import java.io.IOException;
29  
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /**
37   * <a href="PortalClassLoaderServlet.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PortalClassLoaderServlet
43      extends HttpServlet implements PortalInitable {
44  
45      public void destroy() {
46          Thread currentThread = Thread.currentThread();
47  
48          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
49  
50          try {
51              currentThread.setContextClassLoader(
52                  PortalClassLoaderUtil.getClassLoader());
53  
54              _servlet.destroy();
55          }
56          finally {
57              currentThread.setContextClassLoader(contextClassLoader);
58          }
59      }
60  
61      public void init(ServletConfig servletConfig) {
62          _servletConfig = servletConfig;
63  
64          PortalInitableUtil.init(this);
65      }
66  
67      public void portalInit() {
68          Thread currentThread = Thread.currentThread();
69  
70          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
71  
72          ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
73  
74          try {
75              currentThread.setContextClassLoader(portalClassLoader);
76  
77              String servletClass = _servletConfig.getInitParameter(
78                  "servlet-class");
79  
80              _servlet = (HttpServlet)portalClassLoader.loadClass(
81                  servletClass).newInstance();
82  
83              _servlet.init(_servletConfig);
84          }
85          catch (Exception e) {
86              _log.error(e, e);
87          }
88          finally {
89              currentThread.setContextClassLoader(contextClassLoader);
90          }
91      }
92  
93      public void service(
94              HttpServletRequest request, HttpServletResponse response)
95          throws IOException, ServletException {
96  
97          Thread currentThread = Thread.currentThread();
98  
99          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
100 
101         try {
102             currentThread.setContextClassLoader(
103                 PortalClassLoaderUtil.getClassLoader());
104 
105             _servlet.service(request, response);
106         }
107         finally {
108             currentThread.setContextClassLoader(contextClassLoader);
109         }
110     }
111 
112     private static Log _log =
113         LogFactoryUtil.getLog(PortalClassLoaderServlet.class);
114 
115     private HttpServlet _servlet;
116     private ServletConfig _servletConfig;
117 
118 }