1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.HttpUtil;
30  import com.liferay.portal.theme.ThemeLoaderFactory;
31  import com.liferay.portal.velocity.VelocityContextPool;
32  
33  import javax.servlet.ServletContext;
34  
35  /**
36   * <a href="ThemeLoaderHotDeployListener.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
43  
44      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
45          try {
46              doInvokeDeploy(event);
47          }
48          catch (Throwable t) {
49              throwHotDeployException(
50                  event, "Error registering theme loader for ", t);
51          }
52      }
53  
54      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
55          try {
56              doInvokeUndeploy(event);
57          }
58          catch (Throwable t) {
59              throwHotDeployException(
60                  event, "Error unregistering theme loader for ", t);
61          }
62      }
63  
64      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
65          ServletContext servletContext = event.getServletContext();
66  
67          String servletContextName = servletContext.getServletContextName();
68  
69          if (_log.isDebugEnabled()) {
70              _log.debug("Invoking deploy for " + servletContextName);
71          }
72  
73          String[] xmls = new String[] {
74              HttpUtil.URLtoString(
75                  servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
76          };
77  
78          if (xmls[0] == null) {
79              return;
80          }
81  
82          if (_log.isInfoEnabled()) {
83              _log.info("Registering theme loader for " + servletContextName);
84          }
85  
86          ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
87      }
88  
89      protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
90          ServletContext servletContext = event.getServletContext();
91  
92          String servletContextName = servletContext.getServletContextName();
93  
94          if (_log.isDebugEnabled()) {
95              _log.debug("Invoking undeploy for " + servletContextName);
96          }
97  
98          boolean value = ThemeLoaderFactory.destroy(servletContextName);
99  
100         if (!value) {
101             return;
102         }
103 
104         if (_log.isInfoEnabled()) {
105             _log.info("Unregistering theme loader for " + servletContextName);
106         }
107 
108         VelocityContextPool.remove(servletContextName);
109 
110         if (_log.isInfoEnabled()) {
111             _log.info(
112                 "Theme loader for " + servletContextName +
113                     " unregistered successfully");
114         }
115     }
116 
117     private static Log _log =
118         LogFactoryUtil.getLog(ThemeLoaderHotDeployListener.class);
119 
120 }