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