1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.ObjectValuePair;
31  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
32  
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Map;
37  
38  import javax.servlet.ServletContext;
39  
40  /**
41   * <a href="LayoutTemplateHotDeployListener.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Brian Myunghun Kim
46   * @author Ivica Cardic
47   *
48   */
49  public class LayoutTemplateHotDeployListener extends BaseHotDeployListener {
50  
51      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
52          try {
53              doInvokeDeploy(event);
54          }
55          catch (Throwable t) {
56              throwHotDeployException(
57                  event, "Error registering layout templates for ", t);
58          }
59      }
60  
61      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
62          try {
63              doInvokeUndeploy(event);
64          }
65          catch (Throwable t) {
66              throwHotDeployException(
67                  event, "Error unregistering layout templates for ", t);
68          }
69      }
70  
71      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
72          ServletContext servletContext = event.getServletContext();
73  
74          String servletContextName = servletContext.getServletContextName();
75  
76          if (_log.isDebugEnabled()) {
77              _log.debug("Invoking deploy for " + servletContextName);
78          }
79  
80          String[] xmls = new String[] {
81              HttpUtil.URLtoString(servletContext.getResource(
82                  "/WEB-INF/liferay-layout-templates.xml"))
83          };
84  
85          if (xmls[0] == null) {
86              return;
87          }
88  
89          if (_log.isInfoEnabled()) {
90              _log.info("Registering layout templates for " + servletContextName);
91          }
92  
93          List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
94              LayoutTemplateLocalServiceUtil.init(
95                  servletContextName, servletContext, xmls,
96                  event.getPluginPackage());
97  
98          _vars.put(servletContextName, layoutTemplateIds);
99  
100         if (_log.isInfoEnabled()) {
101             if (layoutTemplateIds.size() == 1) {
102                 _log.info(
103                     "1 layout template for " + servletContextName +
104                         " is available for use");
105             }
106             else {
107                 _log.info(
108                     layoutTemplateIds.size() + " layout templates for " +
109                         servletContextName + " are available for use");
110             }
111         }
112     }
113 
114     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
115         ServletContext servletContext = event.getServletContext();
116 
117         String servletContextName = servletContext.getServletContextName();
118 
119         if (_log.isDebugEnabled()) {
120             _log.debug("Invoking undeploy for " + servletContextName);
121         }
122 
123         List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
124             _vars.get(servletContextName);
125 
126         if (layoutTemplateIds == null) {
127             return;
128         }
129 
130         if (_log.isInfoEnabled()) {
131             _log.info(
132                 "Unregistering layout templates for " + servletContextName);
133         }
134 
135         Iterator<ObjectValuePair<String, Boolean>> itr =
136             layoutTemplateIds.iterator();
137 
138         while (itr.hasNext()) {
139             ObjectValuePair<String, Boolean> ovp = itr.next();
140 
141             String layoutTemplateId = ovp.getKey();
142             Boolean standard = ovp.getValue();
143 
144             try {
145                 LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
146                     layoutTemplateId, standard.booleanValue());
147             }
148             catch (Exception e) {
149                 _log.error(e, e);
150             }
151         }
152 
153         if (_log.isInfoEnabled()) {
154             if (layoutTemplateIds.size() == 1) {
155                 _log.info(
156                     "1 layout template for " + servletContextName +
157                         " was unregistered");
158             }
159             else {
160                 _log.info(
161                     layoutTemplateIds.size() + " layout templates for " +
162                         servletContextName + " was unregistered");
163             }
164         }
165     }
166 
167     private static Log _log =
168         LogFactoryUtil.getLog(LayoutTemplateHotDeployListener.class);
169 
170     private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
171         new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
172 
173 }