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