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.kernel.util.ObjectValuePair;
24  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
25  
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.servlet.ServletContext;
32  
33  /**
34   * <a href="LayoutTemplateHotDeployListener.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Brian Myunghun Kim
39   * @author Ivica Cardic
40   */
41  public class LayoutTemplateHotDeployListener 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 layout templates 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 layout templates 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(servletContext.getResource(
74                  "/WEB-INF/liferay-layout-templates.xml"))
75          };
76  
77          if (xmls[0] == null) {
78              return;
79          }
80  
81          if (_log.isInfoEnabled()) {
82              _log.info("Registering layout templates for " + servletContextName);
83          }
84  
85          List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
86              LayoutTemplateLocalServiceUtil.init(
87                  servletContextName, servletContext, xmls,
88                  event.getPluginPackage());
89  
90          _vars.put(servletContextName, layoutTemplateIds);
91  
92          if (_log.isInfoEnabled()) {
93              if (layoutTemplateIds.size() == 1) {
94                  _log.info(
95                      "1 layout template for " + servletContextName +
96                          " is available for use");
97              }
98              else {
99                  _log.info(
100                     layoutTemplateIds.size() + " layout templates for " +
101                         servletContextName + " are available for use");
102             }
103         }
104     }
105 
106     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
107         ServletContext servletContext = event.getServletContext();
108 
109         String servletContextName = servletContext.getServletContextName();
110 
111         if (_log.isDebugEnabled()) {
112             _log.debug("Invoking undeploy for " + servletContextName);
113         }
114 
115         List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
116             _vars.get(servletContextName);
117 
118         if (layoutTemplateIds == null) {
119             return;
120         }
121 
122         if (_log.isInfoEnabled()) {
123             _log.info(
124                 "Unregistering layout templates for " + servletContextName);
125         }
126 
127         Iterator<ObjectValuePair<String, Boolean>> itr =
128             layoutTemplateIds.iterator();
129 
130         while (itr.hasNext()) {
131             ObjectValuePair<String, Boolean> ovp = itr.next();
132 
133             String layoutTemplateId = ovp.getKey();
134             Boolean standard = ovp.getValue();
135 
136             try {
137                 LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
138                     layoutTemplateId, standard.booleanValue());
139             }
140             catch (Exception e) {
141                 _log.error(e, e);
142             }
143         }
144 
145         if (_log.isInfoEnabled()) {
146             if (layoutTemplateIds.size() == 1) {
147                 _log.info(
148                     "1 layout template for " + servletContextName +
149                         " was unregistered");
150             }
151             else {
152                 _log.info(
153                     layoutTemplateIds.size() + " layout templates for " +
154                         servletContextName + " was unregistered");
155             }
156         }
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(
160         LayoutTemplateHotDeployListener.class);
161 
162     private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
163         new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
164 
165 }