1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.deploy.hot;
16  
17  import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
18  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
19  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.servlet.PortletServlet;
23  import com.liferay.portal.kernel.util.HttpUtil;
24  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
25  import com.liferay.portal.service.ThemeLocalServiceUtil;
26  import com.liferay.portal.velocity.LiferayResourceCacheUtil;
27  import com.liferay.portal.velocity.VelocityContextPool;
28  
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import javax.servlet.ServletContext;
34  
35  /**
36   * <a href="ThemeHotDeployListener.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Brian Myunghun Kim
40   * @author Ivica Cardic
41   */
42  public class ThemeHotDeployListener extends BaseHotDeployListener {
43  
44      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
45          try {
46              doInvokeDeploy(event);
47          }
48          catch (Throwable t) {
49              throwHotDeployException(event, "Error registering themes for ", t);
50          }
51      }
52  
53      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
54          try {
55              doInvokeUndeploy(event);
56          }
57          catch (Throwable t) {
58              throwHotDeployException(
59                  event, "Error unregistering themes for ", t);
60          }
61      }
62  
63      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
64          ServletContext servletContext = event.getServletContext();
65  
66          String servletContextName = servletContext.getServletContextName();
67  
68          if (_log.isDebugEnabled()) {
69              _log.debug("Invoking deploy for " + servletContextName);
70          }
71  
72          String[] xmls = new String[] {
73              HttpUtil.URLtoString(servletContext.getResource(
74                  "/WEB-INF/liferay-look-and-feel.xml"))
75          };
76  
77          if (xmls[0] == null) {
78              return;
79          }
80  
81          if (_log.isInfoEnabled()) {
82              _log.info("Registering themes for " + servletContextName);
83          }
84  
85          List<String> themeIds = ThemeLocalServiceUtil.init(
86              servletContextName, servletContext, null, true, xmls,
87              event.getPluginPackage());
88  
89          // Class loader
90  
91          ClassLoader portletClassLoader = event.getContextClassLoader();
92  
93          servletContext.setAttribute(
94              PortletServlet.PORTLET_CLASS_LOADER, portletClassLoader);
95  
96          // Velocity
97  
98          VelocityContextPool.put(servletContextName, servletContext);
99  
100         // Variables
101 
102         _vars.put(servletContextName, themeIds);
103 
104         if (_log.isInfoEnabled()) {
105             if (themeIds.size() == 1) {
106                 _log.info(
107                     "1 theme for " + servletContextName +
108                         " is available for use");
109             }
110             else {
111                 _log.info(
112                     themeIds.size() + " themes for " + servletContextName +
113                         " are available for use");
114             }
115         }
116     }
117 
118     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
119         ServletContext servletContext = event.getServletContext();
120 
121         String servletContextName = servletContext.getServletContextName();
122 
123         if (_log.isDebugEnabled()) {
124             _log.debug("Invoking undeploy for " + servletContextName);
125         }
126 
127         List<String> themeIds = _vars.remove(servletContextName);
128 
129         if (themeIds != null) {
130             if (_log.isInfoEnabled()) {
131                 _log.info("Unregistering themes for " + servletContextName);
132             }
133 
134             try {
135                 ThemeLocalServiceUtil.uninstallThemes(themeIds);
136             }
137             catch (Exception e) {
138                 _log.error(e, e);
139             }
140         }
141         else {
142             return;
143         }
144 
145         // LEP-2057
146 
147         Thread currentThread = Thread.currentThread();
148 
149         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
150 
151         try {
152             currentThread.setContextClassLoader(
153                 PortalClassLoaderUtil.getClassLoader());
154 
155             VelocityContextPool.remove(servletContextName);
156 
157             LiferayResourceCacheUtil.clear();
158         }
159         finally {
160             currentThread.setContextClassLoader(contextClassLoader);
161         }
162 
163         if (_log.isInfoEnabled()) {
164             if (themeIds.size() == 1) {
165                 _log.info(
166                     "1 theme for " + servletContextName + " was unregistered");
167             }
168             else {
169                 _log.info(
170                     themeIds.size() + " themes for " + servletContextName +
171                         " was unregistered");
172             }
173         }
174     }
175 
176     private static Log _log = LogFactoryUtil.getLog(
177         ThemeHotDeployListener.class);
178 
179     private static Map<String, List<String>> _vars =
180         new HashMap<String, List<String>>();
181 
182 }