1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  
31  import java.io.IOException;
32  
33  import javax.servlet.ServletConfig;
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServlet;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  /**
40   * <a href="PortalClassLoaderServlet.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class PortalClassLoaderServlet
46      extends HttpServlet implements PortalInitable {
47  
48      public void destroy() {
49          Thread currentThread = Thread.currentThread();
50  
51          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
52  
53          try {
54              currentThread.setContextClassLoader(
55                  PortalClassLoaderUtil.getClassLoader());
56  
57              _servlet.destroy();
58          }
59          finally {
60              currentThread.setContextClassLoader(contextClassLoader);
61          }
62      }
63  
64      public void init(ServletConfig servletConfig) {
65          _servletConfig = servletConfig;
66  
67          PortalInitableUtil.init(this);
68      }
69  
70      public void portalInit() {
71          Thread currentThread = Thread.currentThread();
72  
73          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
74  
75          ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
76  
77          try {
78              currentThread.setContextClassLoader(portalClassLoader);
79  
80              String servletClass = _servletConfig.getInitParameter(
81                  "servlet-class");
82  
83              _servlet = (HttpServlet)portalClassLoader.loadClass(
84                  servletClass).newInstance();
85  
86              _servlet.init(_servletConfig);
87          }
88          catch (Exception e) {
89              _log.error(e, e);
90          }
91          finally {
92              currentThread.setContextClassLoader(contextClassLoader);
93          }
94      }
95  
96      public void service(
97              HttpServletRequest request, HttpServletResponse response)
98          throws IOException, ServletException {
99  
100         Thread currentThread = Thread.currentThread();
101 
102         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
103 
104         try {
105             currentThread.setContextClassLoader(
106                 PortalClassLoaderUtil.getClassLoader());
107 
108             _servlet.service(request, response);
109         }
110         finally {
111             currentThread.setContextClassLoader(contextClassLoader);
112         }
113     }
114 
115     private static Log _log =
116         LogFactoryUtil.getLog(PortalClassLoaderServlet.class);
117 
118     private HttpServlet _servlet;
119     private ServletConfig _servletConfig;
120 
121 }