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 (Exception e) {
56              throwHotDeployException(
57                  event, "Error registering layout templates for ", e);
58          }
59      }
60  
61      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
62          try {
63              doInvokeUndeploy(event);
64          }
65          catch (Exception e) {
66              throwHotDeployException(
67                  event, "Error unregistering layout templates for ", e);
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             _log.info(
102                 "Layout templates for " + servletContextName +
103                     " registered successfully. It is now ready to be used.");
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         layoutTemplateIds = null;
147 
148         if (_log.isInfoEnabled()) {
149             _log.info(
150                 "Layout templates for " + servletContextName +
151                     " unregistered successfully");
152         }
153     }
154 
155     private static Log _log =
156         LogFactoryUtil.getLog(LayoutTemplateHotDeployListener.class);
157 
158     private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
159         new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
160 
161 }