001    /**
002     * Copyright (c) 2000-2012 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.servlet.FileTimestampUtil;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
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                    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                    FileTimestampUtil.reset();
095    
096                    _themeIds.put(servletContextName, themeIds);
097    
098                    if (_log.isInfoEnabled()) {
099                            if (themeIds.size() == 1) {
100                                    _log.info(
101                                            "1 theme for " + servletContextName +
102                                                    " is available for use");
103                            }
104                            else {
105                                    _log.info(
106                                            themeIds.size() + " themes for " + servletContextName +
107                                                    " are available for use");
108                            }
109                    }
110            }
111    
112            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
113                    throws Exception {
114    
115                    ServletContext servletContext = hotDeployEvent.getServletContext();
116    
117                    String servletContextName = servletContext.getServletContextName();
118    
119                    if (_log.isDebugEnabled()) {
120                            _log.debug("Invoking undeploy for " + servletContextName);
121                    }
122    
123                    List<String> themeIds = _themeIds.remove(servletContextName);
124    
125                    if (themeIds != null) {
126                            if (_log.isInfoEnabled()) {
127                                    _log.info("Unregistering themes for " + servletContextName);
128                            }
129    
130                            try {
131                                    ThemeLocalServiceUtil.uninstallThemes(themeIds);
132                            }
133                            catch (Exception e) {
134                                    _log.error(e, e);
135                            }
136                    }
137                    else {
138                            return;
139                    }
140    
141                    // LEP-2057
142    
143                    ClassLoader contextClassLoader =
144                            PACLClassLoaderUtil.getContextClassLoader();
145    
146                    try {
147                            PACLClassLoaderUtil.setContextClassLoader(
148                                    PACLClassLoaderUtil.getPortalClassLoader());
149    
150                            LiferayResourceCacheUtil.clear();
151                    }
152                    finally {
153                            PACLClassLoaderUtil.setContextClassLoader(contextClassLoader);
154                    }
155    
156                    if (_log.isInfoEnabled()) {
157                            if (themeIds.size() == 1) {
158                                    _log.info(
159                                            "1 theme for " + servletContextName + " was unregistered");
160                            }
161                            else {
162                                    _log.info(
163                                            themeIds.size() + " themes for " + servletContextName +
164                                                    " was unregistered");
165                            }
166                    }
167            }
168    
169            private static Log _log = LogFactoryUtil.getLog(
170                    ThemeHotDeployListener.class);
171    
172            private static Map<String, List<String>> _themeIds =
173                    new HashMap<String, List<String>>();
174    
175    }