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