001    /**
002     * Copyright (c) 2000-present 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.FileTimestampUtil;
023    import com.liferay.portal.kernel.template.TemplateConstants;
024    import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
025    import com.liferay.portal.kernel.util.ClassLoaderUtil;
026    import com.liferay.portal.kernel.util.HttpUtil;
027    import com.liferay.portal.kernel.util.WebKeys;
028    import com.liferay.portal.model.Theme;
029    import com.liferay.portal.service.ThemeLocalServiceUtil;
030    
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.servlet.ServletContext;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Brian Myunghun Kim
040     * @author Ivica Cardic
041     */
042    public class ThemeHotDeployListener extends BaseHotDeployListener {
043    
044            @Override
045            public void invokeDeploy(HotDeployEvent hotDeployEvent)
046                    throws HotDeployException {
047    
048                    try {
049                            doInvokeDeploy(hotDeployEvent);
050                    }
051                    catch (Throwable t) {
052                            throwHotDeployException(
053                                    hotDeployEvent, "Error registering themes for ", t);
054                    }
055            }
056    
057            @Override
058            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
059                    throws HotDeployException {
060    
061                    try {
062                            doInvokeUndeploy(hotDeployEvent);
063                    }
064                    catch (Throwable t) {
065                            throwHotDeployException(
066                                    hotDeployEvent, "Error unregistering themes for ", t);
067                    }
068            }
069    
070            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
071                    throws Exception {
072    
073                    ServletContext servletContext = hotDeployEvent.getServletContext();
074    
075                    String servletContextName = servletContext.getServletContextName();
076    
077                    if (_log.isDebugEnabled()) {
078                            _log.debug("Invoking deploy for " + servletContextName);
079                    }
080    
081                    String[] xmls = new String[] {
082                            HttpUtil.URLtoString(
083                                    servletContext.getResource(
084                                            "/WEB-INF/liferay-look-and-feel.xml"))
085                    };
086    
087                    if (xmls[0] == null) {
088                            return;
089                    }
090    
091                    if (_log.isInfoEnabled()) {
092                            _log.info("Registering themes for " + servletContextName);
093                    }
094    
095                    FileTimestampUtil.reset();
096    
097                    List<Theme> themes = ThemeLocalServiceUtil.init(
098                            servletContextName, servletContext, null, true, xmls,
099                            hotDeployEvent.getPluginPackage());
100    
101                    _themes.put(servletContextName, themes);
102    
103                    servletContext.setAttribute(WebKeys.PLUGIN_THEMES, themes);
104    
105                    if (_log.isInfoEnabled()) {
106                            if (themes.size() == 1) {
107                                    _log.info(
108                                            "1 theme for " + servletContextName +
109                                                    " is available for use");
110                            }
111                            else {
112                                    _log.info(
113                                            themes.size() + " themes for " + servletContextName +
114                                                    " are available for use");
115                            }
116                    }
117            }
118    
119            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
120                    throws Exception {
121    
122                    ServletContext servletContext = hotDeployEvent.getServletContext();
123    
124                    String servletContextName = servletContext.getServletContextName();
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Invoking undeploy for " + servletContextName);
128                    }
129    
130                    List<Theme> themes = _themes.remove(servletContextName);
131    
132                    if (themes != null) {
133                            if (_log.isInfoEnabled()) {
134                                    _log.info("Unregistering themes for " + servletContextName);
135                            }
136    
137                            try {
138                                    ThemeLocalServiceUtil.uninstallThemes(themes);
139                            }
140                            catch (Exception e) {
141                                    _log.error(e, e);
142                            }
143                    }
144                    else {
145                            return;
146                    }
147    
148                    // LEP-2057
149    
150                    ClassLoader contextClassLoader =
151                            ClassLoaderUtil.getContextClassLoader();
152    
153                    try {
154                            ClassLoaderUtil.setContextClassLoader(
155                                    ClassLoaderUtil.getPortalClassLoader());
156    
157                            TemplateResourceLoaderUtil.clearCache(
158                                    TemplateConstants.LANG_TYPE_VM);
159                    }
160                    finally {
161                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
162                    }
163    
164                    if (_log.isInfoEnabled()) {
165                            if (themes.size() == 1) {
166                                    _log.info(
167                                            "1 theme for " + servletContextName + " was unregistered");
168                            }
169                            else {
170                                    _log.info(
171                                            themes.size() + " themes for " + servletContextName +
172                                                    " were unregistered");
173                            }
174                    }
175            }
176    
177            private static final Log _log = LogFactoryUtil.getLog(
178                    ThemeHotDeployListener.class);
179    
180            private static final Map<String, List<Theme>> _themes = new HashMap<>();
181    
182    }