001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.deploy.hot;
016    
017    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
018    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
019    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.ObjectValuePair;
024    import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
025    
026    import java.util.HashMap;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Map;
030    
031    import javax.servlet.ServletContext;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Brian Myunghun Kim
036     * @author Ivica Cardic
037     */
038    public class LayoutTemplateHotDeployListener extends BaseHotDeployListener {
039    
040            public void invokeDeploy(HotDeployEvent hotDeployEvent)
041                    throws HotDeployException {
042    
043                    try {
044                            doInvokeDeploy(hotDeployEvent);
045                    }
046                    catch (Throwable t) {
047                            throwHotDeployException(
048                                    hotDeployEvent, "Error registering layout templates for ", t);
049                    }
050            }
051    
052            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
053                    throws HotDeployException {
054    
055                    try {
056                            doInvokeUndeploy(hotDeployEvent);
057                    }
058                    catch (Throwable t) {
059                            throwHotDeployException(
060                                    hotDeployEvent, "Error unregistering layout templates for ", t);
061                    }
062            }
063    
064            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
065                    throws Exception {
066    
067                    ServletContext servletContext = hotDeployEvent.getServletContext();
068    
069                    String servletContextName = servletContext.getServletContextName();
070    
071                    if (_log.isDebugEnabled()) {
072                            _log.debug("Invoking deploy for " + servletContextName);
073                    }
074    
075                    String[] xmls = new String[] {
076                            HttpUtil.URLtoString(
077                                    servletContext.getResource(
078                                            "/WEB-INF/liferay-layout-templates.xml"))
079                    };
080    
081                    if (xmls[0] == null) {
082                            return;
083                    }
084    
085                    logRegistration(servletContextName);
086    
087                    List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
088                            LayoutTemplateLocalServiceUtil.init(
089                                    servletContextName, servletContext, xmls,
090                                    hotDeployEvent.getPluginPackage());
091    
092                    _vars.put(servletContextName, layoutTemplateIds);
093    
094                    if (_log.isInfoEnabled()) {
095                            if (layoutTemplateIds.size() == 1) {
096                                    _log.info(
097                                            "1 layout template for " + servletContextName +
098                                                    " is available for use");
099                            }
100                            else {
101                                    _log.info(
102                                            layoutTemplateIds.size() + " layout templates for " +
103                                                    servletContextName + " are available for use");
104                            }
105                    }
106            }
107    
108            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
109                    throws Exception {
110    
111                    ServletContext servletContext = hotDeployEvent.getServletContext();
112    
113                    String servletContextName = servletContext.getServletContextName();
114    
115                    if (_log.isDebugEnabled()) {
116                            _log.debug("Invoking undeploy for " + servletContextName);
117                    }
118    
119                    List<ObjectValuePair<String, Boolean>> layoutTemplateIds = _vars.get(
120                            servletContextName);
121    
122                    if (layoutTemplateIds == null) {
123                            return;
124                    }
125    
126                    if (_log.isInfoEnabled()) {
127                            _log.info(
128                                    "Unregistering layout templates for " + servletContextName);
129                    }
130    
131                    Iterator<ObjectValuePair<String, Boolean>> itr =
132                            layoutTemplateIds.iterator();
133    
134                    while (itr.hasNext()) {
135                            ObjectValuePair<String, Boolean> ovp = itr.next();
136    
137                            String layoutTemplateId = ovp.getKey();
138                            Boolean standard = ovp.getValue();
139    
140                            try {
141                                    LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
142                                            layoutTemplateId, standard.booleanValue());
143                            }
144                            catch (Exception e) {
145                                    _log.error(e, e);
146                            }
147                    }
148    
149                    if (_log.isInfoEnabled()) {
150                            if (layoutTemplateIds.size() == 1) {
151                                    _log.info(
152                                            "1 layout template for " + servletContextName +
153                                                    " was unregistered");
154                            }
155                            else {
156                                    _log.info(
157                                            layoutTemplateIds.size() + " layout templates for " +
158                                                    servletContextName + " was unregistered");
159                            }
160                    }
161            }
162    
163            protected void logRegistration(String servletContextName) {
164                    if (_log.isInfoEnabled()) {
165                            _log.info("Registering layout templates for " + servletContextName);
166                    }
167            }
168    
169            private static Log _log = LogFactoryUtil.getLog(
170                    LayoutTemplateHotDeployListener.class);
171    
172            private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
173                    new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
174    
175    }