1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.util.HttpUtil;
23  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
24  import com.liferay.portal.service.ThemeLocalServiceUtil;
25  import com.liferay.portal.velocity.LiferayResourceCacheUtil;
26  
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.servlet.ServletContext;
32  
33  /**
34   * <a href="ThemeHotDeployListener.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Brian Myunghun Kim
38   * @author Ivica Cardic
39   */
40  public class ThemeHotDeployListener extends BaseHotDeployListener {
41  
42      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
43          try {
44              doInvokeDeploy(event);
45          }
46          catch (Throwable t) {
47              throwHotDeployException(event, "Error registering themes for ", t);
48          }
49      }
50  
51      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
52          try {
53              doInvokeUndeploy(event);
54          }
55          catch (Throwable t) {
56              throwHotDeployException(
57                  event, "Error unregistering themes for ", t);
58          }
59      }
60  
61      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
62          ServletContext servletContext = event.getServletContext();
63  
64          String servletContextName = servletContext.getServletContextName();
65  
66          if (_log.isDebugEnabled()) {
67              _log.debug("Invoking deploy for " + servletContextName);
68          }
69  
70          String[] xmls = new String[] {
71              HttpUtil.URLtoString(
72                  servletContext.getResource(
73                      "/WEB-INF/liferay-look-and-feel.xml"))
74          };
75  
76          if (xmls[0] == null) {
77              return;
78          }
79  
80          if (_log.isInfoEnabled()) {
81              _log.info("Registering themes for " + servletContextName);
82          }
83  
84          List<String> themeIds = ThemeLocalServiceUtil.init(
85              servletContextName, servletContext, null, true, xmls,
86              event.getPluginPackage());
87  
88          _vars.put(servletContextName, themeIds);
89  
90          if (_log.isInfoEnabled()) {
91              if (themeIds.size() == 1) {
92                  _log.info(
93                      "1 theme for " + servletContextName +
94                          " is available for use");
95              }
96              else {
97                  _log.info(
98                      themeIds.size() + " themes for " + servletContextName +
99                          " are available for use");
100             }
101         }
102     }
103 
104     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
105         ServletContext servletContext = event.getServletContext();
106 
107         String servletContextName = servletContext.getServletContextName();
108 
109         if (_log.isDebugEnabled()) {
110             _log.debug("Invoking undeploy for " + servletContextName);
111         }
112 
113         List<String> themeIds = _vars.remove(servletContextName);
114 
115         if (themeIds != null) {
116             if (_log.isInfoEnabled()) {
117                 _log.info("Unregistering themes for " + servletContextName);
118             }
119 
120             try {
121                 ThemeLocalServiceUtil.uninstallThemes(themeIds);
122             }
123             catch (Exception e) {
124                 _log.error(e, e);
125             }
126         }
127         else {
128             return;
129         }
130 
131         // LEP-2057
132 
133         Thread currentThread = Thread.currentThread();
134 
135         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
136 
137         try {
138             currentThread.setContextClassLoader(
139                 PortalClassLoaderUtil.getClassLoader());
140 
141             LiferayResourceCacheUtil.clear();
142         }
143         finally {
144             currentThread.setContextClassLoader(contextClassLoader);
145         }
146 
147         if (_log.isInfoEnabled()) {
148             if (themeIds.size() == 1) {
149                 _log.info(
150                     "1 theme for " + servletContextName + " was unregistered");
151             }
152             else {
153                 _log.info(
154                     themeIds.size() + " themes for " + servletContextName +
155                         " was unregistered");
156             }
157         }
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(
161         ThemeHotDeployListener.class);
162 
163     private static Map<String, List<String>> _vars =
164         new HashMap<String, List<String>>();
165 
166 }