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.List;
028    import java.util.Map;
029    
030    import javax.servlet.ServletContext;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Brian Myunghun Kim
035     * @author Ivica Cardic
036     */
037    public class LayoutTemplateHotDeployListener extends BaseHotDeployListener {
038    
039            public void invokeDeploy(HotDeployEvent hotDeployEvent)
040                    throws HotDeployException {
041    
042                    try {
043                            doInvokeDeploy(hotDeployEvent);
044                    }
045                    catch (Throwable t) {
046                            throwHotDeployException(
047                                    hotDeployEvent, "Error registering layout templates for ", t);
048                    }
049            }
050    
051            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
052                    throws HotDeployException {
053    
054                    try {
055                            doInvokeUndeploy(hotDeployEvent);
056                    }
057                    catch (Throwable t) {
058                            throwHotDeployException(
059                                    hotDeployEvent, "Error unregistering layout templates for ", t);
060                    }
061            }
062    
063            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
064                    throws Exception {
065    
066                    ServletContext servletContext = hotDeployEvent.getServletContext();
067    
068                    String servletContextName = servletContext.getServletContextName();
069    
070                    if (_log.isDebugEnabled()) {
071                            _log.debug("Invoking deploy for " + servletContextName);
072                    }
073    
074                    String[] xmls = new String[] {
075                            HttpUtil.URLtoString(
076                                    servletContext.getResource(
077                                            "/WEB-INF/liferay-layout-templates.xml"))
078                    };
079    
080                    if (xmls[0] == null) {
081                            return;
082                    }
083    
084                    if (_log.isInfoEnabled()) {
085                            _log.info("Registering layout templates for " + servletContextName);
086                    }
087    
088                    List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
089                            LayoutTemplateLocalServiceUtil.init(
090                                    servletContextName, servletContext, xmls,
091                                    hotDeployEvent.getPluginPackage());
092    
093                    _vars.put(servletContextName, layoutTemplateIds);
094    
095                    if (_log.isInfoEnabled()) {
096                            if (layoutTemplateIds.size() == 1) {
097                                    _log.info(
098                                            "1 layout template for " + servletContextName +
099                                                    " is available for use");
100                            }
101                            else {
102                                    _log.info(
103                                            layoutTemplateIds.size() + " layout templates for " +
104                                                    servletContextName + " are available for use");
105                            }
106                    }
107            }
108    
109            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
110                    throws Exception {
111    
112                    ServletContext servletContext = hotDeployEvent.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 = _vars.get(
121                            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                    for (ObjectValuePair<String, Boolean> ovp : layoutTemplateIds) {
133                            String layoutTemplateId = ovp.getKey();
134                            Boolean standard = ovp.getValue();
135    
136                            try {
137                                    LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
138                                            layoutTemplateId, standard.booleanValue());
139                            }
140                            catch (Exception e) {
141                                    _log.error(e, e);
142                            }
143                    }
144    
145                    if (_log.isInfoEnabled()) {
146                            if (layoutTemplateIds.size() == 1) {
147                                    _log.info(
148                                            "1 layout template for " + servletContextName +
149                                                    " was unregistered");
150                            }
151                            else {
152                                    _log.info(
153                                            layoutTemplateIds.size() + " layout templates for " +
154                                                    servletContextName + " was unregistered");
155                            }
156                    }
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(
160                    LayoutTemplateHotDeployListener.class);
161    
162            private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
163                    new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
164    
165    }