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