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.PortalLifecycle;
21  import com.liferay.portal.kernel.util.PortalLifecycleUtil;
22  
23  import java.io.IOException;
24  
25  import javax.servlet.ServletConfig;
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  /**
32   * <a href="PortalClassLoaderServlet.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class PortalClassLoaderServlet
37      extends HttpServlet implements PortalLifecycle {
38  
39      public void destroy() {
40          portalDestroy();
41      }
42  
43      public void init(ServletConfig servletConfig) {
44          _servletConfig = servletConfig;
45  
46          PortalLifecycleUtil.register(this);
47      }
48  
49      public void portalDestroy() {
50          if (!_calledPortalDestroy) {
51              PortalLifecycleUtil.removeDestroy(this);
52  
53              doPortalDestroy();
54  
55              _calledPortalDestroy = true;
56          }
57      }
58  
59      public void portalInit() {
60          Thread currentThread = Thread.currentThread();
61  
62          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
63  
64          ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
65  
66          try {
67              currentThread.setContextClassLoader(portalClassLoader);
68  
69              String servletClass = _servletConfig.getInitParameter(
70                  "servlet-class");
71  
72              _servlet = (HttpServlet)portalClassLoader.loadClass(
73                  servletClass).newInstance();
74  
75              _servlet.init(_servletConfig);
76          }
77          catch (Exception e) {
78              _log.error(e, e);
79          }
80          finally {
81              currentThread.setContextClassLoader(contextClassLoader);
82          }
83      }
84  
85      public void service(
86              HttpServletRequest request, HttpServletResponse response)
87          throws IOException, ServletException {
88  
89          Thread currentThread = Thread.currentThread();
90  
91          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
92  
93          try {
94              currentThread.setContextClassLoader(
95                  PortalClassLoaderUtil.getClassLoader());
96  
97              _servlet.service(request, response);
98          }
99          finally {
100             currentThread.setContextClassLoader(contextClassLoader);
101         }
102     }
103 
104     protected void doPortalDestroy() {
105         Thread currentThread = Thread.currentThread();
106 
107         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
108 
109         try {
110             currentThread.setContextClassLoader(
111                 PortalClassLoaderUtil.getClassLoader());
112 
113             _servlet.destroy();
114         }
115         finally {
116             currentThread.setContextClassLoader(contextClassLoader);
117         }
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(
121         PortalClassLoaderServlet.class);
122 
123     private boolean _calledPortalDestroy;
124     private HttpServlet _servlet;
125     private ServletConfig _servletConfig;
126 
127 }