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