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