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             _log.info(
102                 "Themes for " + servletContextName +
103                     " registered successfully. It is now ready to be used.");
104         }
105     }
106 
107     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
108         ServletContext servletContext = event.getServletContext();
109 
110         String servletContextName = servletContext.getServletContextName();
111 
112         if (_log.isDebugEnabled()) {
113             _log.debug("Invoking undeploy for " + servletContextName);
114         }
115 
116         List<String> themeIds = _vars.remove(servletContextName);
117 
118         if (themeIds != null) {
119             if (_log.isInfoEnabled()) {
120                 _log.info("Unregistering themes for " + servletContextName);
121             }
122 
123             try {
124                 ThemeLocalServiceUtil.uninstallThemes(themeIds);
125             }
126             catch (Exception e) {
127                 _log.error(e, e);
128             }
129         }
130         else {
131             return;
132         }
133 
134         // LEP-2057
135 
136         Thread currentThread = Thread.currentThread();
137 
138         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
139 
140         try {
141             currentThread.setContextClassLoader(
142                 PortalClassLoaderUtil.getClassLoader());
143 
144             VelocityContextPool.remove(servletContextName);
145 
146             LiferayResourceCacheUtil.clear();
147         }
148         finally {
149             currentThread.setContextClassLoader(contextClassLoader);
150         }
151 
152         if (_log.isInfoEnabled()) {
153             _log.info(
154                 "Themes for " + servletContextName +
155                     " unregistered successfully");
156         }
157     }
158 
159     private static Log _log =
160          LogFactoryUtil.getLog(ThemeHotDeployListener.class);
161 
162     private static Map<String, List<String>> _vars =
163         new HashMap<String, List<String>>();
164 
165 }