1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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(
74                  servletContext.getResource(
75                      "/WEB-INF/liferay-layout-templates.xml"))
76          };
77  
78          if (xmls[0] == null) {
79              return;
80          }
81  
82          if (_log.isInfoEnabled()) {
83              _log.info("Registering layout templates for " + servletContextName);
84          }
85  
86          List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
87              LayoutTemplateLocalServiceUtil.init(
88                  servletContextName, servletContext, xmls,
89                  event.getPluginPackage());
90  
91          _vars.put(servletContextName, layoutTemplateIds);
92  
93          if (_log.isInfoEnabled()) {
94              if (layoutTemplateIds.size() == 1) {
95                  _log.info(
96                      "1 layout template for " + servletContextName +
97                          " is available for use");
98              }
99              else {
100                 _log.info(
101                     layoutTemplateIds.size() + " layout templates for " +
102                         servletContextName + " are available for use");
103             }
104         }
105     }
106 
107     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
108         ServletContext servletContext = event.getServletContext();
109 
110         String servletContextName = servletContext.getServletContextName();
111 
112         if (_log.isDebugEnabled()) {
113             _log.debug("Invoking undeploy for " + servletContextName);
114         }
115 
116         List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
117             _vars.get(servletContextName);
118 
119         if (layoutTemplateIds == null) {
120             return;
121         }
122 
123         if (_log.isInfoEnabled()) {
124             _log.info(
125                 "Unregistering layout templates for " + servletContextName);
126         }
127 
128         Iterator<ObjectValuePair<String, Boolean>> itr =
129             layoutTemplateIds.iterator();
130 
131         while (itr.hasNext()) {
132             ObjectValuePair<String, Boolean> ovp = itr.next();
133 
134             String layoutTemplateId = ovp.getKey();
135             Boolean standard = ovp.getValue();
136 
137             try {
138                 LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
139                     layoutTemplateId, standard.booleanValue());
140             }
141             catch (Exception e) {
142                 _log.error(e, e);
143             }
144         }
145 
146         if (_log.isInfoEnabled()) {
147             if (layoutTemplateIds.size() == 1) {
148                 _log.info(
149                     "1 layout template for " + servletContextName +
150                         " was unregistered");
151             }
152             else {
153                 _log.info(
154                     layoutTemplateIds.size() + " layout templates for " +
155                         servletContextName + " was unregistered");
156             }
157         }
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(
161         LayoutTemplateHotDeployListener.class);
162 
163     private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
164         new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
165 
166 }