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