001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.servlet;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025: import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
026: import com.liferay.portal.kernel.util.PortalInitable;
027: import com.liferay.portal.kernel.util.PortalInitableUtil;
028:
029: import java.io.IOException;
030:
031: import javax.servlet.ServletConfig;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServlet;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: /**
038: * <a href="PortalClassLoaderServlet.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class PortalClassLoaderServlet extends HttpServlet implements
044: PortalInitable {
045:
046: public void portalInit() {
047: ClassLoader contextClassLoader = Thread.currentThread()
048: .getContextClassLoader();
049:
050: ClassLoader portalClassLoader = PortalClassLoaderUtil
051: .getClassLoader();
052:
053: try {
054: Thread.currentThread().setContextClassLoader(
055: portalClassLoader);
056:
057: String servletClass = _config
058: .getInitParameter("servlet-class");
059:
060: _servlet = (HttpServlet) portalClassLoader.loadClass(
061: servletClass).newInstance();
062:
063: _servlet.init(_config);
064: } catch (Exception e) {
065: _log.error(e, e);
066: } finally {
067: Thread.currentThread().setContextClassLoader(
068: contextClassLoader);
069: }
070: }
071:
072: public void init(ServletConfig config) throws ServletException {
073: _config = config;
074:
075: PortalInitableUtil.init(this );
076: }
077:
078: public void service(HttpServletRequest req, HttpServletResponse res)
079: throws IOException, ServletException {
080:
081: ClassLoader contextClassLoader = Thread.currentThread()
082: .getContextClassLoader();
083:
084: try {
085: Thread.currentThread().setContextClassLoader(
086: PortalClassLoaderUtil.getClassLoader());
087:
088: _servlet.service(req, res);
089: } finally {
090: Thread.currentThread().setContextClassLoader(
091: contextClassLoader);
092: }
093: }
094:
095: public void destroy() {
096: ClassLoader contextClassLoader = Thread.currentThread()
097: .getContextClassLoader();
098:
099: try {
100: Thread.currentThread().setContextClassLoader(
101: PortalClassLoaderUtil.getClassLoader());
102:
103: _servlet.destroy();
104: } finally {
105: Thread.currentThread().setContextClassLoader(
106: contextClassLoader);
107: }
108: }
109:
110: private static Log _log = LogFactoryUtil
111: .getLog(PortalClassLoaderServlet.class);
112:
113: private HttpServlet _servlet;
114: private ServletConfig _config;
115:
116: }
|