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