1   /**
2    * Copyright (c) 2000-2008 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.util.HttpUtil;
28  import com.liferay.portal.theme.ThemeLoaderFactory;
29  import com.liferay.portal.velocity.VelocityContextPool;
30  
31  import javax.servlet.ServletContext;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * <a href="ThemeLoaderHotDeployListener.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
44  
45      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
46          try {
47              doInvokeDeploy(event);
48          }
49          catch (Exception e) {
50              throwHotDeployException(
51                  event, "Error registering theme loader for ", e);
52          }
53      }
54  
55      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
56          try {
57              doInvokeUndeploy(event);
58          }
59          catch (Exception e) {
60              throwHotDeployException(
61                  event, "Error unregistering theme loader for ", e);
62          }
63      }
64  
65      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
66          ServletContext servletContext = event.getServletContext();
67  
68          String servletContextName = servletContext.getServletContextName();
69  
70          if (_log.isDebugEnabled()) {
71              _log.debug("Invoking deploy for " + servletContextName);
72          }
73  
74          String[] xmls = new String[] {
75              HttpUtil.URLtoString(
76                  servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
77          };
78  
79          if (xmls[0] == null) {
80              return;
81          }
82  
83          if (_log.isInfoEnabled()) {
84              _log.info("Registering theme loader for " + servletContextName);
85          }
86  
87          ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
88      }
89  
90      protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
91          ServletContext servletContext = event.getServletContext();
92  
93          String servletContextName = servletContext.getServletContextName();
94  
95          if (_log.isDebugEnabled()) {
96              _log.debug("Invoking undeploy for " + servletContextName);
97          }
98  
99          boolean value = ThemeLoaderFactory.destroy(servletContextName);
100 
101         if (!value) {
102             return;
103         }
104 
105         if (_log.isInfoEnabled()) {
106             _log.info("Unregistering theme loader for " + servletContextName);
107         }
108 
109         VelocityContextPool.remove(servletContextName);
110 
111         if (_log.isInfoEnabled()) {
112             _log.info(
113                 "Theme loader for " + servletContextName +
114                     " unregistered successfully");
115         }
116     }
117 
118     private static Log _log =
119         LogFactory.getLog(ThemeLoaderHotDeployListener.class);
120 
121 }