1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.deploy.hot;
24  
25  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
26  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
27  import com.liferay.portal.kernel.deploy.hot.HotDeployListener;
28  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29  import com.liferay.portal.lastmodified.LastModifiedCSS;
30  import com.liferay.portal.lastmodified.LastModifiedJavaScript;
31  import com.liferay.portal.service.impl.ThemeLocalUtil;
32  import com.liferay.portal.velocity.LiferayResourceCacheUtil;
33  import com.liferay.portal.velocity.VelocityContextPool;
34  import com.liferay.util.CollectionFactory;
35  import com.liferay.util.Http;
36  
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.ServletContext;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="ThemeHotDeployListener.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Brian Myunghun Kim
50   * @author Ivica Cardic
51   *
52   */
53  public class ThemeHotDeployListener implements HotDeployListener {
54  
55      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
56          String servletContextName = null;
57  
58          try {
59              ServletContext ctx = event.getServletContext();
60  
61              servletContextName = ctx.getServletContextName();
62  
63              if (_log.isDebugEnabled()) {
64                  _log.debug("Invoking deploy for " + servletContextName);
65              }
66  
67              String[] xmls = new String[] {
68                  Http.URLtoString(
69                      ctx.getResource("/WEB-INF/liferay-look-and-feel.xml"))
70              };
71  
72              if (xmls[0] == null) {
73                  return;
74              }
75  
76              if (_log.isInfoEnabled()) {
77                  _log.info("Registering themes for " + servletContextName);
78              }
79  
80              List themeIds = ThemeLocalUtil.init(
81                  servletContextName, ctx, null, true, xmls,
82                  event.getPluginPackage());
83  
84              VelocityContextPool.put(servletContextName, ctx);
85  
86              _vars.put(servletContextName, themeIds);
87  
88              if (_log.isInfoEnabled()) {
89                  _log.info(
90                      "Themes for " + servletContextName +
91                          " registered successfully");
92              }
93          }
94          catch (Exception e) {
95              throw new HotDeployException(
96                  "Error registering themes for " + servletContextName, e);
97          }
98      }
99  
100     public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
101         String servletContextName = null;
102 
103         try {
104             ServletContext ctx = event.getServletContext();
105 
106             servletContextName = ctx.getServletContextName();
107 
108             if (_log.isDebugEnabled()) {
109                 _log.debug("Invoking undeploy for " + servletContextName);
110             }
111 
112             List themeIds = (List)_vars.remove(servletContextName);
113 
114             if (themeIds != null) {
115                 if (_log.isInfoEnabled()) {
116                     _log.info("Unregistering themes for " + servletContextName);
117                 }
118 
119                 try {
120                     ThemeLocalUtil.uninstallThemes(themeIds);
121                 }
122                 catch (Exception e1) {
123                     _log.error(e1.getMessage());
124                 }
125             }
126             else {
127                 return;
128             }
129 
130             // LEP-2057
131 
132             ClassLoader contextClassLoader =
133                 Thread.currentThread().getContextClassLoader();
134 
135             try {
136                 Thread.currentThread().setContextClassLoader(
137                     PortalClassLoaderUtil.getClassLoader());
138 
139                 VelocityContextPool.remove(servletContextName);
140 
141                 LiferayResourceCacheUtil.clear();
142 
143                 LastModifiedCSS.clear();
144                 LastModifiedJavaScript.clear();
145             }
146             finally {
147                 Thread.currentThread().setContextClassLoader(
148                     contextClassLoader);
149             }
150 
151             if (_log.isInfoEnabled()) {
152                 _log.info(
153                     "Themes for " + servletContextName +
154                         " unregistered successfully");
155             }
156         }
157         catch (Exception e2) {
158             throw new HotDeployException(
159                 "Error unregistering themes for " + servletContextName, e2);
160         }
161     }
162 
163     private static Log _log = LogFactory.getLog(ThemeHotDeployListener.class);
164 
165     private static Map _vars = CollectionFactory.getHashMap();
166 
167 }