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