1   /**
2    * Copyright (c) 2000-2008 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.util.HttpUtil;
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.ThemeLocalServiceUtil;
32  import com.liferay.portal.velocity.LiferayResourceCacheUtil;
33  import com.liferay.portal.velocity.VelocityContextPool;
34  
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.servlet.ServletContext;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="ThemeHotDeployListener.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Brian Myunghun Kim
49   * @author Ivica Cardic
50   *
51   */
52  public class ThemeHotDeployListener extends BaseHotDeployListener {
53  
54      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
55          try {
56              doInvokeDeploy(event);
57          }
58          catch (Exception e) {
59              throwHotDeployException(event, "Error registering themes for ", e);
60          }
61      }
62  
63      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
64          try {
65              doInvokeUndeploy(event);
66          }
67          catch (Exception e) {
68              throwHotDeployException(
69                  event, "Error unregistering themes for ", e);
70          }
71      }
72  
73      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
74          ServletContext servletContext = event.getServletContext();
75  
76          String servletContextName = servletContext.getServletContextName();
77  
78          if (_log.isDebugEnabled()) {
79              _log.debug("Invoking deploy for " + servletContextName);
80          }
81  
82          String[] xmls = new String[] {
83              HttpUtil.URLtoString(servletContext.getResource(
84                  "/WEB-INF/liferay-look-and-feel.xml"))
85          };
86  
87          if (xmls[0] == null) {
88              return;
89          }
90  
91          if (_log.isInfoEnabled()) {
92              _log.info("Registering themes for " + servletContextName);
93          }
94  
95          List<String> themeIds = ThemeLocalServiceUtil.init(
96              servletContextName, servletContext, null, true, xmls,
97              event.getPluginPackage());
98  
99          VelocityContextPool.put(servletContextName, servletContext);
100 
101         _vars.put(servletContextName, themeIds);
102 
103         if (_log.isInfoEnabled()) {
104             _log.info(
105                 "Themes for " + servletContextName +
106                     " registered successfully");
107         }
108     }
109 
110     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
111         ServletContext servletContext = event.getServletContext();
112 
113         String servletContextName = servletContext.getServletContextName();
114 
115         if (_log.isDebugEnabled()) {
116             _log.debug("Invoking undeploy for " + servletContextName);
117         }
118 
119         List<String> themeIds = _vars.remove(servletContextName);
120 
121         if (themeIds != null) {
122             if (_log.isInfoEnabled()) {
123                 _log.info("Unregistering themes for " + servletContextName);
124             }
125 
126             try {
127                 ThemeLocalServiceUtil.uninstallThemes(themeIds);
128             }
129             catch (Exception e) {
130                 _log.error(e, e);
131             }
132         }
133         else {
134             return;
135         }
136 
137         // LEP-2057
138 
139         ClassLoader contextClassLoader =
140             Thread.currentThread().getContextClassLoader();
141 
142         try {
143             Thread.currentThread().setContextClassLoader(
144                 PortalClassLoaderUtil.getClassLoader());
145 
146             VelocityContextPool.remove(servletContextName);
147 
148             LiferayResourceCacheUtil.clear();
149 
150             LastModifiedCSS.clear();
151             LastModifiedJavaScript.clear();
152         }
153         finally {
154             Thread.currentThread().setContextClassLoader(contextClassLoader);
155         }
156 
157         if (_log.isInfoEnabled()) {
158             _log.info(
159                 "Themes for " + servletContextName +
160                     " unregistered successfully");
161         }
162     }
163 
164     private static Log _log = LogFactory.getLog(ThemeHotDeployListener.class);
165 
166     private static Map<String, List<String>> _vars =
167         new HashMap<String, List<String>>();
168 
169 }