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.deploy.hot.HotDeployListener;
28  import com.liferay.portal.kernel.util.HttpUtil;
29  import com.liferay.portal.kernel.util.ObjectValuePair;
30  import com.liferay.portal.service.impl.LayoutTemplateLocalUtil;
31  
32  import java.util.HashMap;
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                  HttpUtil.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<ObjectValuePair<String, Boolean>> layoutTemplateIds =
80                  LayoutTemplateLocalUtil.init(
81                      servletContextName, ctx, xmls, event.getPluginPackage());
82  
83              _vars.put(servletContextName, layoutTemplateIds);
84  
85              if (_log.isInfoEnabled()) {
86                  _log.info(
87                      "Layout templates for " + servletContextName +
88                          " registered successfully");
89              }
90          }
91          catch (Exception e) {
92              throw new HotDeployException(
93                  "Error registering layout templates for " + servletContextName,
94                  e);
95          }
96      }
97  
98      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
99          String servletContextName = null;
100 
101         try {
102             ServletContext ctx = event.getServletContext();
103 
104             servletContextName = ctx.getServletContextName();
105 
106             if (_log.isDebugEnabled()) {
107                 _log.debug("Invoking undeploy for " + servletContextName);
108             }
109 
110             List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
111                 _vars.get(servletContextName);
112 
113             if (layoutTemplateIds != null) {
114                 if (_log.isInfoEnabled()) {
115                     _log.info(
116                         "Unregistering layout templates for " +
117                             servletContextName);
118                 }
119 
120                 Iterator<ObjectValuePair<String, Boolean>> itr =
121                     layoutTemplateIds.iterator();
122 
123                 while (itr.hasNext()) {
124                     ObjectValuePair<String, Boolean> ovp = itr.next();
125 
126                     String layoutTemplateId = ovp.getKey();
127                     Boolean standard = ovp.getValue();
128 
129                     try {
130                         LayoutTemplateLocalUtil.uninstallLayoutTemplate(
131                             layoutTemplateId, standard.booleanValue());
132                     }
133                     catch (Exception e) {
134                         _log.error(e.getMessage());
135                     }
136                 }
137 
138                 layoutTemplateIds = null;
139 
140                 if (_log.isInfoEnabled()) {
141                     _log.info(
142                         "Layout templates for " + servletContextName +
143                             " unregistered successfully");
144                 }
145             }
146         }
147         catch (Exception e) {
148             throw new HotDeployException(
149                 "Error unregistering layout templates for " +
150                     servletContextName,
151                 e);
152         }
153     }
154 
155     private static Log _log =
156         LogFactory.getLog(LayoutTemplateHotDeployListener.class);
157 
158     private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
159         new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
160 
161 }