001    /**
002     * Copyright (c) 2000-2011 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 event) throws HotDeployException {
034                    try {
035                            doInvokeDeploy(event);
036                    }
037                    catch (Throwable t) {
038                            throwHotDeployException(
039                                    event, "Error registering theme loader for ", t);
040                    }
041            }
042    
043            public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
044                    try {
045                            doInvokeUndeploy(event);
046                    }
047                    catch (Throwable t) {
048                            throwHotDeployException(
049                                    event, "Error unregistering theme loader for ", t);
050                    }
051            }
052    
053            protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
054                    ServletContext servletContext = event.getServletContext();
055    
056                    String servletContextName = servletContext.getServletContextName();
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Invoking deploy for " + servletContextName);
060                    }
061    
062                    String[] xmls = new String[] {
063                            HttpUtil.URLtoString(
064                                    servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
065                    };
066    
067                    if (xmls[0] == null) {
068                            return;
069                    }
070    
071                    if (_log.isInfoEnabled()) {
072                            _log.info("Registering theme loader for " + servletContextName);
073                    }
074    
075                    ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
076            }
077    
078            protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
079                    ServletContext servletContext = event.getServletContext();
080    
081                    String servletContextName = servletContext.getServletContextName();
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Invoking undeploy for " + servletContextName);
085                    }
086    
087                    boolean value = ThemeLoaderFactory.destroy(servletContextName);
088    
089                    if (!value) {
090                            return;
091                    }
092    
093                    if (_log.isInfoEnabled()) {
094                            _log.info("Unregistering theme loader for " + servletContextName);
095                    }
096    
097                    ServletContextPool.remove(servletContextName);
098    
099                    if (_log.isInfoEnabled()) {
100                            _log.info(
101                                    "Theme loader for " + servletContextName +
102                                            " unregistered successfully");
103                    }
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    ThemeLoaderHotDeployListener.class);
108    
109    }