001    /**
002     * Copyright (c) 2000-2012 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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.servlet.ServletContextPool;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.theme.ThemeLoaderFactory;
025    
026    import javax.servlet.ServletContext;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
032    
033            public void invokeDeploy(HotDeployEvent hotDeployEvent)
034                    throws HotDeployException {
035    
036                    try {
037                            doInvokeDeploy(hotDeployEvent);
038                    }
039                    catch (Throwable t) {
040                            throwHotDeployException(
041                                    hotDeployEvent, "Error registering theme loader for ", t);
042                    }
043            }
044    
045            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
046                    throws HotDeployException {
047    
048                    try {
049                            doInvokeUndeploy(hotDeployEvent);
050                    }
051                    catch (Throwable t) {
052                            throwHotDeployException(
053                                    hotDeployEvent, "Error unregistering theme loader for ", t);
054                    }
055            }
056    
057            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
058                    throws Exception {
059    
060                    ServletContext servletContext = hotDeployEvent.getServletContext();
061    
062                    String servletContextName = servletContext.getServletContextName();
063    
064                    if (_log.isDebugEnabled()) {
065                            _log.debug("Invoking deploy for " + servletContextName);
066                    }
067    
068                    String[] xmls = new String[] {
069                            HttpUtil.URLtoString(
070                                    servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
071                    };
072    
073                    if (xmls[0] == null) {
074                            return;
075                    }
076    
077                    logRegistration(servletContextName);
078    
079                    ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
080            }
081    
082            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
083                    throws Exception {
084    
085                    ServletContext servletContext = hotDeployEvent.getServletContext();
086    
087                    String servletContextName = servletContext.getServletContextName();
088    
089                    if (_log.isDebugEnabled()) {
090                            _log.debug("Invoking undeploy for " + servletContextName);
091                    }
092    
093                    boolean value = ThemeLoaderFactory.destroy(servletContextName);
094    
095                    if (!value) {
096                            return;
097                    }
098    
099                    if (_log.isInfoEnabled()) {
100                            _log.info("Unregistering theme loader for " + servletContextName);
101                    }
102    
103                    ServletContextPool.remove(servletContextName);
104    
105                    if (_log.isInfoEnabled()) {
106                            _log.info(
107                                    "Theme loader for " + servletContextName +
108                                            " unregistered successfully");
109                    }
110            }
111    
112            protected void logRegistration(String servletContextName) {
113                    if (_log.isInfoEnabled()) {
114                            _log.info("Registering theme loader for " + servletContextName);
115                    }
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    ThemeLoaderHotDeployListener.class);
120    
121    }