1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.deploy.hot;
21  
22  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
23  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.util.ObjectValuePair;
28  import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
29  
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.servlet.ServletContext;
36  
37  /**
38   * <a href="LayoutTemplateHotDeployListener.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Brian Myunghun Kim
43   * @author Ivica Cardic
44   *
45   */
46  public class LayoutTemplateHotDeployListener extends BaseHotDeployListener {
47  
48      public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
49          try {
50              doInvokeDeploy(event);
51          }
52          catch (Throwable t) {
53              throwHotDeployException(
54                  event, "Error registering layout templates for ", t);
55          }
56      }
57  
58      public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
59          try {
60              doInvokeUndeploy(event);
61          }
62          catch (Throwable t) {
63              throwHotDeployException(
64                  event, "Error unregistering layout templates for ", t);
65          }
66      }
67  
68      protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
69          ServletContext servletContext = event.getServletContext();
70  
71          String servletContextName = servletContext.getServletContextName();
72  
73          if (_log.isDebugEnabled()) {
74              _log.debug("Invoking deploy for " + servletContextName);
75          }
76  
77          String[] xmls = new String[] {
78              HttpUtil.URLtoString(servletContext.getResource(
79                  "/WEB-INF/liferay-layout-templates.xml"))
80          };
81  
82          if (xmls[0] == null) {
83              return;
84          }
85  
86          if (_log.isInfoEnabled()) {
87              _log.info("Registering layout templates for " + servletContextName);
88          }
89  
90          List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
91              LayoutTemplateLocalServiceUtil.init(
92                  servletContextName, servletContext, xmls,
93                  event.getPluginPackage());
94  
95          _vars.put(servletContextName, layoutTemplateIds);
96  
97          if (_log.isInfoEnabled()) {
98              if (layoutTemplateIds.size() == 1) {
99                  _log.info(
100                     "1 layout template for " + servletContextName +
101                         " is available for use");
102             }
103             else {
104                 _log.info(
105                     layoutTemplateIds.size() + " layout templates for " +
106                         servletContextName + " are available for use");
107             }
108         }
109     }
110 
111     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
112         ServletContext servletContext = event.getServletContext();
113 
114         String servletContextName = servletContext.getServletContextName();
115 
116         if (_log.isDebugEnabled()) {
117             _log.debug("Invoking undeploy for " + servletContextName);
118         }
119 
120         List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
121             _vars.get(servletContextName);
122 
123         if (layoutTemplateIds == null) {
124             return;
125         }
126 
127         if (_log.isInfoEnabled()) {
128             _log.info(
129                 "Unregistering layout templates for " + servletContextName);
130         }
131 
132         Iterator<ObjectValuePair<String, Boolean>> itr =
133             layoutTemplateIds.iterator();
134 
135         while (itr.hasNext()) {
136             ObjectValuePair<String, Boolean> ovp = itr.next();
137 
138             String layoutTemplateId = ovp.getKey();
139             Boolean standard = ovp.getValue();
140 
141             try {
142                 LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
143                     layoutTemplateId, standard.booleanValue());
144             }
145             catch (Exception e) {
146                 _log.error(e, e);
147             }
148         }
149 
150         if (_log.isInfoEnabled()) {
151             if (layoutTemplateIds.size() == 1) {
152                 _log.info(
153                     "1 layout template for " + servletContextName +
154                         " was unregistered");
155             }
156             else {
157                 _log.info(
158                     layoutTemplateIds.size() + " layout templates for " +
159                         servletContextName + " was unregistered");
160             }
161         }
162     }
163 
164     private static Log _log =
165         LogFactoryUtil.getLog(LayoutTemplateHotDeployListener.class);
166 
167     private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
168         new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
169 
170 }