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.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            @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<ObjectValuePair<String, Boolean>> layoutTemplateIds =
092                            LayoutTemplateLocalServiceUtil.init(
093                                    servletContextName, servletContext, xmls,
094                                    hotDeployEvent.getPluginPackage());
095    
096                    _vars.put(servletContextName, layoutTemplateIds);
097    
098                    if (_log.isInfoEnabled()) {
099                            if (layoutTemplateIds.size() == 1) {
100                                    _log.info(
101                                            "1 layout template for " + servletContextName +
102                                                    " is available for use");
103                            }
104                            else {
105                                    _log.info(
106                                            layoutTemplateIds.size() + " layout templates for " +
107                                                    servletContextName + " are available for use");
108                            }
109                    }
110            }
111    
112            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
113                    throws Exception {
114    
115                    ServletContext servletContext = hotDeployEvent.getServletContext();
116    
117                    String servletContextName = servletContext.getServletContextName();
118    
119                    if (_log.isDebugEnabled()) {
120                            _log.debug("Invoking undeploy for " + servletContextName);
121                    }
122    
123                    List<ObjectValuePair<String, Boolean>> layoutTemplateIds = _vars.get(
124                            servletContextName);
125    
126                    if (layoutTemplateIds == null) {
127                            return;
128                    }
129    
130                    if (_log.isInfoEnabled()) {
131                            _log.info(
132                                    "Unregistering layout templates for " + servletContextName);
133                    }
134    
135                    Iterator<ObjectValuePair<String, Boolean>> itr =
136                            layoutTemplateIds.iterator();
137    
138                    while (itr.hasNext()) {
139                            ObjectValuePair<String, Boolean> ovp = itr.next();
140    
141                            String layoutTemplateId = ovp.getKey();
142                            Boolean standard = ovp.getValue();
143    
144                            try {
145                                    LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
146                                            layoutTemplateId, standard.booleanValue());
147                            }
148                            catch (Exception e) {
149                                    _log.error(e, e);
150                            }
151                    }
152    
153                    if (_log.isInfoEnabled()) {
154                            if (layoutTemplateIds.size() == 1) {
155                                    _log.info(
156                                            "1 layout template for " + servletContextName +
157                                                    " was unregistered");
158                            }
159                            else {
160                                    _log.info(
161                                            layoutTemplateIds.size() + " layout templates for " +
162                                                    servletContextName + " was unregistered");
163                            }
164                    }
165            }
166    
167            private static Log _log = LogFactoryUtil.getLog(
168                    LayoutTemplateHotDeployListener.class);
169    
170            private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
171                    new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
172    
173    }