1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
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  /**
42   * <a href="ThemeHotDeployListener.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Brian Myunghun Kim
46   * @author Ivica Cardic
47   *
48   */
49  public class ThemeHotDeployListener extends BaseHotDeployListener {
50  
51      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
52          try {
53              doInvokeDeploy(event);
54          }
55          catch (Exception e) {
56              throwHotDeployException(event, "Error registering themes for ", e);
57          }
58      }
59  
60      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
61          try {
62              doInvokeUndeploy(event);
63          }
64          catch (Exception e) {
65              throwHotDeployException(
66                  event, "Error unregistering themes for ", e);
67          }
68      }
69  
70      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
71          ServletContext servletContext = event.getServletContext();
72  
73          String servletContextName = servletContext.getServletContextName();
74  
75          if (_log.isDebugEnabled()) {
76              _log.debug("Invoking deploy for " + servletContextName);
77          }
78  
79          String[] xmls = new String[] {
80              HttpUtil.URLtoString(servletContext.getResource(
81                  "/WEB-INF/liferay-look-and-feel.xml"))
82          };
83  
84          if (xmls[0] == null) {
85              return;
86          }
87  
88          if (_log.isInfoEnabled()) {
89              _log.info("Registering themes for " + servletContextName);
90          }
91  
92          List<String> themeIds = ThemeLocalServiceUtil.init(
93              servletContextName, servletContext, null, true, xmls,
94              event.getPluginPackage());
95  
96          VelocityContextPool.put(servletContextName, servletContext);
97  
98          _vars.put(servletContextName, themeIds);
99  
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 event) throws Exception {
115         ServletContext servletContext = event.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 = _vars.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         Thread currentThread = Thread.currentThread();
144 
145         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
146 
147         try {
148             currentThread.setContextClassLoader(
149                 PortalClassLoaderUtil.getClassLoader());
150 
151             VelocityContextPool.remove(servletContextName);
152 
153             LiferayResourceCacheUtil.clear();
154         }
155         finally {
156             currentThread.setContextClassLoader(contextClassLoader);
157         }
158 
159         if (_log.isInfoEnabled()) {
160             if (themeIds.size() == 1) {
161                 _log.info(
162                     "1 theme for " + servletContextName + " was unregistered");
163             }
164             else {
165                 _log.info(
166                     themeIds.size() + " themes for " + servletContextName +
167                         " was unregistered");
168             }
169         }
170     }
171 
172     private static Log _log =
173          LogFactoryUtil.getLog(ThemeHotDeployListener.class);
174 
175     private static Map<String, List<String>> _vars =
176         new HashMap<String, List<String>>();
177 
178 }