001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    if (_log.isInfoEnabled()) {
086                            _log.info("Registering layout templates for " + servletContextName);
087                    }
088    
089                    List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
090                            LayoutTemplateLocalServiceUtil.init(
091                                    servletContextName, servletContext, xmls,
092                                    hotDeployEvent.getPluginPackage());
093    
094                    _vars.put(servletContextName, layoutTemplateIds);
095    
096                    if (_log.isInfoEnabled()) {
097                            if (layoutTemplateIds.size() == 1) {
098                                    _log.info(
099                                            "1 layout template for " + servletContextName +
100                                                    " is available for use");
101                            }
102                            else {
103                                    _log.info(
104                                            layoutTemplateIds.size() + " layout templates for " +
105                                                    servletContextName + " are available for use");
106                            }
107                    }
108            }
109    
110            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
111                    throws Exception {
112    
113                    ServletContext servletContext = hotDeployEvent.getServletContext();
114    
115                    String servletContextName = servletContext.getServletContextName();
116    
117                    if (_log.isDebugEnabled()) {
118                            _log.debug("Invoking undeploy for " + servletContextName);
119                    }
120    
121                    List<ObjectValuePair<String, Boolean>> layoutTemplateIds = _vars.get(
122                            servletContextName);
123    
124                    if (layoutTemplateIds == null) {
125                            return;
126                    }
127    
128                    if (_log.isInfoEnabled()) {
129                            _log.info(
130                                    "Unregistering layout templates for " + servletContextName);
131                    }
132    
133                    Iterator<ObjectValuePair<String, Boolean>> itr =
134                            layoutTemplateIds.iterator();
135    
136                    while (itr.hasNext()) {
137                            ObjectValuePair<String, Boolean> ovp = itr.next();
138    
139                            String layoutTemplateId = ovp.getKey();
140                            Boolean standard = ovp.getValue();
141    
142                            try {
143                                    LayoutTemplateLocalServiceUtil.uninstallLayoutTemplate(
144                                            layoutTemplateId, standard.booleanValue());
145                            }
146                            catch (Exception e) {
147                                    _log.error(e, e);
148                            }
149                    }
150    
151                    if (_log.isInfoEnabled()) {
152                            if (layoutTemplateIds.size() == 1) {
153                                    _log.info(
154                                            "1 layout template for " + servletContextName +
155                                                    " was unregistered");
156                            }
157                            else {
158                                    _log.info(
159                                            layoutTemplateIds.size() + " layout templates for " +
160                                                    servletContextName + " was unregistered");
161                            }
162                    }
163            }
164    
165            private static Log _log = LogFactoryUtil.getLog(
166                    LayoutTemplateHotDeployListener.class);
167    
168            private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
169                    new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
170    
171    }