1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.servlet.PortletServlet;
30  import com.liferay.portal.kernel.util.HttpUtil;
31  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
32  import com.liferay.portal.service.ThemeLocalServiceUtil;
33  import com.liferay.portal.velocity.LiferayResourceCacheUtil;
34  import com.liferay.portal.velocity.VelocityContextPool;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.ServletContext;
41  
42  /**
43   * <a href="ThemeHotDeployListener.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Brian Myunghun Kim
47   * @author Ivica Cardic
48   */
49  public class ThemeHotDeployListener extends BaseHotDeployListener {
50  
51      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
52          try {
53              doInvokeDeploy(event);
54          }
55          catch (Throwable t) {
56              throwHotDeployException(event, "Error registering themes for ", t);
57          }
58      }
59  
60      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
61          try {
62              doInvokeUndeploy(event);
63          }
64          catch (Throwable t) {
65              throwHotDeployException(
66                  event, "Error unregistering themes for ", t);
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          // Class loader
97  
98          ClassLoader portletClassLoader = event.getContextClassLoader();
99  
100         servletContext.setAttribute(
101             PortletServlet.PORTLET_CLASS_LOADER, portletClassLoader);
102 
103         // Velocity
104 
105         VelocityContextPool.put(servletContextName, servletContext);
106 
107         // Variables
108 
109         _vars.put(servletContextName, themeIds);
110 
111         if (_log.isInfoEnabled()) {
112             if (themeIds.size() == 1) {
113                 _log.info(
114                     "1 theme for " + servletContextName +
115                         " is available for use");
116             }
117             else {
118                 _log.info(
119                     themeIds.size() + " themes for " + servletContextName +
120                         " are available for use");
121             }
122         }
123     }
124 
125     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
126         ServletContext servletContext = event.getServletContext();
127 
128         String servletContextName = servletContext.getServletContextName();
129 
130         if (_log.isDebugEnabled()) {
131             _log.debug("Invoking undeploy for " + servletContextName);
132         }
133 
134         List<String> themeIds = _vars.remove(servletContextName);
135 
136         if (themeIds != null) {
137             if (_log.isInfoEnabled()) {
138                 _log.info("Unregistering themes for " + servletContextName);
139             }
140 
141             try {
142                 ThemeLocalServiceUtil.uninstallThemes(themeIds);
143             }
144             catch (Exception e) {
145                 _log.error(e, e);
146             }
147         }
148         else {
149             return;
150         }
151 
152         // LEP-2057
153 
154         Thread currentThread = Thread.currentThread();
155 
156         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
157 
158         try {
159             currentThread.setContextClassLoader(
160                 PortalClassLoaderUtil.getClassLoader());
161 
162             VelocityContextPool.remove(servletContextName);
163 
164             LiferayResourceCacheUtil.clear();
165         }
166         finally {
167             currentThread.setContextClassLoader(contextClassLoader);
168         }
169 
170         if (_log.isInfoEnabled()) {
171             if (themeIds.size() == 1) {
172                 _log.info(
173                     "1 theme for " + servletContextName + " was unregistered");
174             }
175             else {
176                 _log.info(
177                     themeIds.size() + " themes for " + servletContextName +
178                         " was unregistered");
179             }
180         }
181     }
182 
183     private static Log _log =
184         LogFactoryUtil.getLog(ThemeHotDeployListener.class);
185 
186     private static Map<String, List<String>> _vars =
187         new HashMap<String, List<String>>();
188 
189 }