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