001    /**
002     * Copyright (c) 2000-2012 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.util.HttpUtil;
024    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
025    import com.liferay.portal.service.ThemeLocalServiceUtil;
026    import com.liferay.portal.velocity.LiferayResourceCacheUtil;
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                    logRegistration(servletContextName);
087    
088                    List<String> themeIds = ThemeLocalServiceUtil.init(
089                            servletContextName, servletContext, null, true, xmls,
090                            hotDeployEvent.getPluginPackage());
091    
092                    FileTimestampUtil.reset();
093    
094                    _vars.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 = _vars.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                    Thread currentThread = Thread.currentThread();
142    
143                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
144    
145                    try {
146                            currentThread.setContextClassLoader(
147                                    PortalClassLoaderUtil.getClassLoader());
148    
149                            LiferayResourceCacheUtil.clear();
150                    }
151                    finally {
152                            currentThread.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            protected void logRegistration(String servletContextName) {
169                    if (_log.isInfoEnabled()) {
170                            _log.info("Registering themes for " + servletContextName);
171                    }
172            }
173    
174            private static Log _log = LogFactoryUtil.getLog(
175                    ThemeHotDeployListener.class);
176    
177            private static Map<String, List<String>> _vars =
178                    new HashMap<String, List<String>>();
179    
180    }