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