001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.HttpUtil;
023    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
024    import com.liferay.portal.service.ThemeLocalServiceUtil;
025    import com.liferay.portal.velocity.LiferayResourceCacheUtil;
026    
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Map;
030    
031    import javax.servlet.ServletContext;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Brian Myunghun Kim
036     * @author Ivica Cardic
037     */
038    public class ThemeHotDeployListener extends BaseHotDeployListener {
039    
040            public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
041                    try {
042                            doInvokeDeploy(event);
043                    }
044                    catch (Throwable t) {
045                            throwHotDeployException(event, "Error registering themes for ", t);
046                    }
047            }
048    
049            public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
050                    try {
051                            doInvokeUndeploy(event);
052                    }
053                    catch (Throwable t) {
054                            throwHotDeployException(
055                                    event, "Error unregistering themes for ", t);
056                    }
057            }
058    
059            protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
060                    ServletContext servletContext = event.getServletContext();
061    
062                    String servletContextName = servletContext.getServletContextName();
063    
064                    if (_log.isDebugEnabled()) {
065                            _log.debug("Invoking deploy for " + servletContextName);
066                    }
067    
068                    String[] xmls = new String[] {
069                            HttpUtil.URLtoString(servletContext.getResource(
070                                    "/WEB-INF/liferay-look-and-feel.xml"))
071                    };
072    
073                    if (xmls[0] == null) {
074                            return;
075                    }
076    
077                    if (_log.isInfoEnabled()) {
078                            _log.info("Registering themes for " + servletContextName);
079                    }
080    
081                    List<String> themeIds = ThemeLocalServiceUtil.init(
082                            servletContextName, servletContext, null, true, xmls,
083                            event.getPluginPackage());
084    
085                    _vars.put(servletContextName, themeIds);
086    
087                    if (_log.isInfoEnabled()) {
088                            if (themeIds.size() == 1) {
089                                    _log.info(
090                                            "1 theme for " + servletContextName +
091                                                    " is available for use");
092                            }
093                            else {
094                                    _log.info(
095                                            themeIds.size() + " themes for " + servletContextName +
096                                                    " are available for use");
097                            }
098                    }
099            }
100    
101            protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
102                    ServletContext servletContext = event.getServletContext();
103    
104                    String servletContextName = servletContext.getServletContextName();
105    
106                    if (_log.isDebugEnabled()) {
107                            _log.debug("Invoking undeploy for " + servletContextName);
108                    }
109    
110                    List<String> themeIds = _vars.remove(servletContextName);
111    
112                    if (themeIds != null) {
113                            if (_log.isInfoEnabled()) {
114                                    _log.info("Unregistering themes for " + servletContextName);
115                            }
116    
117                            try {
118                                    ThemeLocalServiceUtil.uninstallThemes(themeIds);
119                            }
120                            catch (Exception e) {
121                                    _log.error(e, e);
122                            }
123                    }
124                    else {
125                            return;
126                    }
127    
128                    // LEP-2057
129    
130                    Thread currentThread = Thread.currentThread();
131    
132                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
133    
134                    try {
135                            currentThread.setContextClassLoader(
136                                    PortalClassLoaderUtil.getClassLoader());
137    
138                            LiferayResourceCacheUtil.clear();
139                    }
140                    finally {
141                            currentThread.setContextClassLoader(contextClassLoader);
142                    }
143    
144                    if (_log.isInfoEnabled()) {
145                            if (themeIds.size() == 1) {
146                                    _log.info(
147                                            "1 theme for " + servletContextName + " was unregistered");
148                            }
149                            else {
150                                    _log.info(
151                                            themeIds.size() + " themes for " + servletContextName +
152                                                    " was unregistered");
153                            }
154                    }
155            }
156    
157            private static Log _log = LogFactoryUtil.getLog(
158                    ThemeHotDeployListener.class);
159    
160            private static Map<String, List<String>> _vars =
161                    new HashMap<String, List<String>>();
162    
163    }