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