001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
018    import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.BasePortalLifecycle;
022    
023    import javax.servlet.ServletContext;
024    import javax.servlet.ServletContextAttributeEvent;
025    import javax.servlet.ServletContextAttributeListener;
026    import javax.servlet.ServletContextEvent;
027    import javax.servlet.ServletContextListener;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class PluginContextListener
033            extends BasePortalLifecycle
034            implements ServletContextAttributeListener, ServletContextListener {
035    
036            public static final String PLUGIN_CLASS_LOADER = "PLUGIN_CLASS_LOADER";
037    
038            public void attributeAdded(
039                    ServletContextAttributeEvent servletContextAttributeEvent) {
040    
041                    String name = servletContextAttributeEvent.getName();
042    
043                    if (_addedPluginClassLoader && name.equals(PLUGIN_CLASS_LOADER) &&
044                            (servletContextAttributeEvent.getValue() != pluginClassLoader)) {
045    
046                            if (_log.isWarnEnabled()) {
047                                    _log.warn(
048                                            "Preventing the addition of another plugin class loader");
049                            }
050    
051                            servletContext.setAttribute(PLUGIN_CLASS_LOADER, pluginClassLoader);
052                    }
053                    else if (!_addedPluginClassLoader && name.equals(PLUGIN_CLASS_LOADER)) {
054                            _addedPluginClassLoader = true;
055                    }
056            }
057    
058            public void attributeRemoved(
059                    ServletContextAttributeEvent servletContextAttributeEvent) {
060    
061                    String name = servletContextAttributeEvent.getName();
062    
063                    if (_addedPluginClassLoader && name.equals(PLUGIN_CLASS_LOADER)) {
064                            if (_log.isWarnEnabled()) {
065                                    _log.warn("Preventing the removal of the plugin class loader");
066                            }
067    
068                            servletContext.setAttribute(PLUGIN_CLASS_LOADER, pluginClassLoader);
069                    }
070            }
071    
072            public void attributeReplaced(
073                    ServletContextAttributeEvent servletContextAttributeEvent) {
074    
075                    String name = servletContextAttributeEvent.getName();
076    
077                    if (_addedPluginClassLoader && name.equals(PLUGIN_CLASS_LOADER)) {
078                            if (_log.isWarnEnabled()) {
079                                    _log.warn(
080                                            "Preventing the replacement of the plugin class loader");
081                            }
082    
083                            servletContext.removeAttribute(PLUGIN_CLASS_LOADER);
084                    }
085            }
086    
087            public void contextDestroyed(ServletContextEvent servletContextEvent) {
088                    portalDestroy();
089            }
090    
091            public void contextInitialized(ServletContextEvent servletContextEvent) {
092                    servletContext = servletContextEvent.getServletContext();
093    
094                    Thread currentThread = Thread.currentThread();
095    
096                    pluginClassLoader = currentThread.getContextClassLoader();
097    
098                    servletContext.setAttribute(PLUGIN_CLASS_LOADER, pluginClassLoader);
099    
100                    ServletContextPool.put(
101                            servletContext.getServletContextName(), servletContext);
102    
103                    registerPortalLifecycle();
104            }
105    
106            @Override
107            protected void doPortalDestroy() throws Exception {
108                    PluginContextLifecycleThreadLocal.setDestroying(true);
109    
110                    Thread currentThread = Thread.currentThread();
111    
112                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
113    
114                    if (contextClassLoader != pluginClassLoader) {
115                            currentThread.setContextClassLoader(pluginClassLoader);
116                    }
117    
118                    try {
119                            fireUndeployEvent();
120                    }
121                    finally {
122                            PluginContextLifecycleThreadLocal.setDestroying(false);
123    
124                            currentThread.setContextClassLoader(contextClassLoader);
125                    }
126            }
127    
128            @Override
129            protected void doPortalInit() throws Exception {
130                    PluginContextLifecycleThreadLocal.setInitializing(true);
131    
132                    Thread currentThread = Thread.currentThread();
133    
134                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
135    
136                    if (contextClassLoader != pluginClassLoader) {
137                            currentThread.setContextClassLoader(pluginClassLoader);
138                    }
139    
140                    try {
141                            fireDeployEvent();
142                    }
143                    finally {
144                            PluginContextLifecycleThreadLocal.setInitializing(false);
145    
146                            currentThread.setContextClassLoader(contextClassLoader);
147                    }
148            }
149    
150            protected void fireDeployEvent() {
151                    HotDeployUtil.fireDeployEvent(
152                            new HotDeployEvent(servletContext, pluginClassLoader));
153            }
154    
155            protected void fireUndeployEvent() {
156                    HotDeployUtil.fireUndeployEvent(
157                            new HotDeployEvent(servletContext, pluginClassLoader));
158            }
159    
160            protected ClassLoader pluginClassLoader;
161            protected ServletContext servletContext;
162    
163            private static Log _log = LogFactoryUtil.getLog(
164                    PluginContextListener.class);
165    
166            private boolean _addedPluginClassLoader;
167    
168    }