001    /**
002     * Copyright (c) 2000-present 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.WebKeys;
024    import com.liferay.portal.model.LayoutTemplate;
025    import com.liferay.portal.service.LayoutTemplateLocalServiceUtil;
026    
027    import java.util.HashMap;
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            @Override
041            public void invokeDeploy(HotDeployEvent hotDeployEvent)
042                    throws HotDeployException {
043    
044                    try {
045                            doInvokeDeploy(hotDeployEvent);
046                    }
047                    catch (Throwable t) {
048                            throwHotDeployException(
049                                    hotDeployEvent, "Error registering layout templates for ", t);
050                    }
051            }
052    
053            @Override
054            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
055                    throws HotDeployException {
056    
057                    try {
058                            doInvokeUndeploy(hotDeployEvent);
059                    }
060                    catch (Throwable t) {
061                            throwHotDeployException(
062                                    hotDeployEvent, "Error unregistering layout templates for ", t);
063                    }
064            }
065    
066            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
067                    throws Exception {
068    
069                    ServletContext servletContext = hotDeployEvent.getServletContext();
070    
071                    String servletContextName = servletContext.getServletContextName();
072    
073                    if (_log.isDebugEnabled()) {
074                            _log.debug("Invoking deploy for " + servletContextName);
075                    }
076    
077                    String[] xmls = new String[] {
078                            HttpUtil.URLtoString(
079                                    servletContext.getResource(
080                                            "/WEB-INF/liferay-layout-templates.xml"))
081                    };
082    
083                    if (xmls[0] == null) {
084                            return;
085                    }
086    
087                    if (_log.isInfoEnabled()) {
088                            _log.info("Registering layout templates for " + servletContextName);
089                    }
090    
091                    List<LayoutTemplate> layoutTemplates =
092                            LayoutTemplateLocalServiceUtil.init(
093                                    servletContextName, servletContext, xmls,
094                                    hotDeployEvent.getPluginPackage());
095    
096                    _layoutTemplates.put(servletContextName, layoutTemplates);
097    
098                    servletContext.setAttribute(
099                            WebKeys.PLUGIN_LAYOUT_TEMPLATES, layoutTemplates);
100    
101                    if (_log.isInfoEnabled()) {
102                            if (layoutTemplates.size() == 1) {
103                                    _log.info(
104                                            "1 layout template for " + servletContextName +
105                                                    " is available for use");
106                            }
107                            else {
108                                    _log.info(
109                                            layoutTemplates.size() + " layout templates for " +
110                                                    servletContextName + " are available for use");
111                            }
112                    }
113            }
114    
115            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
116                    throws Exception {
117    
118                    ServletContext servletContext = hotDeployEvent.getServletContext();
119    
120                    String servletContextName = servletContext.getServletContextName();
121    
122                    if (_log.isDebugEnabled()) {
123                            _log.debug("Invoking undeploy for " + servletContextName);
124                    }
125    
126                    List<LayoutTemplate> layoutTemplates = _layoutTemplates.get(
127                            servletContextName);
128    
129                    if (layoutTemplates == null) {
130                            return;
131                    }
132    
133                    if (_log.isInfoEnabled()) {
134                            _log.info(
135                                    "Unregistering layout templates for " + servletContextName);
136                    }
137    
138                    for (LayoutTemplate layoutTemplate : layoutTemplates) {
139                            try {
140                                    LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
141                                            layoutTemplate.getLayoutTemplateId(),
142                                            layoutTemplate.isStandard());
143                            }
144                            catch (Exception e) {
145                                    _log.error(e, e);
146                            }
147                    }
148    
149                    if (_log.isInfoEnabled()) {
150                            if (layoutTemplates.size() == 1) {
151                                    _log.info(
152                                            "1 layout template for " + servletContextName +
153                                                    " was unregistered");
154                            }
155                            else {
156                                    _log.info(
157                                            layoutTemplates.size() + " layout templates for " +
158                                                    servletContextName + " were unregistered");
159                            }
160                    }
161            }
162    
163            private static final Log _log = LogFactoryUtil.getLog(
164                    LayoutTemplateHotDeployListener.class);
165    
166            private static final Map<String, List<LayoutTemplate>> _layoutTemplates =
167                    new HashMap<>();
168    
169    }