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