1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.theme.ThemeLoaderFactory;
24  import com.liferay.portal.velocity.VelocityContextPool;
25  
26  import javax.servlet.ServletContext;
27  
28  /**
29   * <a href="ThemeLoaderHotDeployListener.java.html"><b><i>View Source</i></b>
30   * </a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
35  
36      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
37          try {
38              doInvokeDeploy(event);
39          }
40          catch (Throwable t) {
41              throwHotDeployException(
42                  event, "Error registering theme loader for ", t);
43          }
44      }
45  
46      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
47          try {
48              doInvokeUndeploy(event);
49          }
50          catch (Throwable t) {
51              throwHotDeployException(
52                  event, "Error unregistering theme loader for ", t);
53          }
54      }
55  
56      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
57          ServletContext servletContext = event.getServletContext();
58  
59          String servletContextName = servletContext.getServletContextName();
60  
61          if (_log.isDebugEnabled()) {
62              _log.debug("Invoking deploy for " + servletContextName);
63          }
64  
65          String[] xmls = new String[] {
66              HttpUtil.URLtoString(
67                  servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
68          };
69  
70          if (xmls[0] == null) {
71              return;
72          }
73  
74          if (_log.isInfoEnabled()) {
75              _log.info("Registering theme loader for " + servletContextName);
76          }
77  
78          ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
79      }
80  
81      protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
82          ServletContext servletContext = event.getServletContext();
83  
84          String servletContextName = servletContext.getServletContextName();
85  
86          if (_log.isDebugEnabled()) {
87              _log.debug("Invoking undeploy for " + servletContextName);
88          }
89  
90          boolean value = ThemeLoaderFactory.destroy(servletContextName);
91  
92          if (!value) {
93              return;
94          }
95  
96          if (_log.isInfoEnabled()) {
97              _log.info("Unregistering theme loader for " + servletContextName);
98          }
99  
100         VelocityContextPool.remove(servletContextName);
101 
102         if (_log.isInfoEnabled()) {
103             _log.info(
104                 "Theme loader for " + servletContextName +
105                     " unregistered successfully");
106         }
107     }
108 
109     private static Log _log = LogFactoryUtil.getLog(
110         ThemeLoaderHotDeployListener.class);
111 
112 }