1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
42  
43      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
44          try {
45              doInvokeDeploy(event);
46          }
47          catch (Throwable t) {
48              throwHotDeployException(
49                  event, "Error registering theme loader for ", t);
50          }
51      }
52  
53      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
54          try {
55              doInvokeUndeploy(event);
56          }
57          catch (Throwable t) {
58              throwHotDeployException(
59                  event, "Error unregistering theme loader for ", t);
60          }
61      }
62  
63      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
64          ServletContext servletContext = event.getServletContext();
65  
66          String servletContextName = servletContext.getServletContextName();
67  
68          if (_log.isDebugEnabled()) {
69              _log.debug("Invoking deploy for " + servletContextName);
70          }
71  
72          String[] xmls = new String[] {
73              HttpUtil.URLtoString(
74                  servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
75          };
76  
77          if (xmls[0] == null) {
78              return;
79          }
80  
81          if (_log.isInfoEnabled()) {
82              _log.info("Registering theme loader for " + servletContextName);
83          }
84  
85          ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
86      }
87  
88      protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
89          ServletContext servletContext = event.getServletContext();
90  
91          String servletContextName = servletContext.getServletContextName();
92  
93          if (_log.isDebugEnabled()) {
94              _log.debug("Invoking undeploy for " + servletContextName);
95          }
96  
97          boolean value = ThemeLoaderFactory.destroy(servletContextName);
98  
99          if (!value) {
100             return;
101         }
102 
103         if (_log.isInfoEnabled()) {
104             _log.info("Unregistering theme loader for " + servletContextName);
105         }
106 
107         VelocityContextPool.remove(servletContextName);
108 
109         if (_log.isInfoEnabled()) {
110             _log.info(
111                 "Theme loader for " + servletContextName +
112                     " unregistered successfully");
113         }
114     }
115 
116     private static Log _log =
117         LogFactoryUtil.getLog(ThemeLoaderHotDeployListener.class);
118 
119 }