001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.deploy.hot;
016    
017    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
018    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
019    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
020    import com.liferay.portal.kernel.servlet.SecurePluginContextListener;
021    
022    import java.lang.reflect.Method;
023    
024    import javax.servlet.ServletContext;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class ServletContextListenerHotDeployListener
030            extends BaseHotDeployListener {
031    
032            @Override
033            public void invokeDeploy(HotDeployEvent hotDeployEvent)
034                    throws HotDeployException {
035    
036                    try {
037                            doInvokeDeploy(hotDeployEvent);
038                    }
039                    catch (Throwable t) {
040                            throwHotDeployException(
041                                    hotDeployEvent,
042                                    "Error registering servlet context listeners for " +
043                                            hotDeployEvent.getServletContextName(),
044                                    t);
045                    }
046            }
047    
048            @Override
049            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
050                    throws HotDeployException {
051    
052                    try {
053                            doInvokeUndeploy(hotDeployEvent);
054                    }
055                    catch (Throwable t) {
056                            throwHotDeployException(
057                                    hotDeployEvent,
058                                    "Error unregistering servlet context listeners for " +
059                                            hotDeployEvent.getServletContextName(),
060                                    t);
061                    }
062            }
063    
064            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
065                    throws Exception {
066    
067                    ServletContext servletContext = hotDeployEvent.getServletContext();
068    
069                    Object securePluginContextListener = servletContext.getAttribute(
070                            SecurePluginContextListener.class.getName());
071    
072                    if (securePluginContextListener != null) {
073                            Class<?> clazz = securePluginContextListener.getClass();
074    
075                            Method method = clazz.getMethod("instantiatingListeners");
076    
077                            method.invoke(securePluginContextListener);
078                    }
079            }
080    
081            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
082                    throws Exception {
083            }
084    
085    }