001    /**
002     * Copyright (c) 2000-present 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.servlet.ServletContextPool;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.theme.ThemeLoaderFactory;
025    
026    import javax.servlet.ServletContext;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ThemeLoaderHotDeployListener extends BaseHotDeployListener {
032    
033            @Override
034            public void invokeDeploy(HotDeployEvent hotDeployEvent)
035                    throws HotDeployException {
036    
037                    try {
038                            doInvokeDeploy(hotDeployEvent);
039                    }
040                    catch (Throwable t) {
041                            throwHotDeployException(
042                                    hotDeployEvent, "Error registering theme loader for ", t);
043                    }
044            }
045    
046            @Override
047            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
048                    throws HotDeployException {
049    
050                    try {
051                            doInvokeUndeploy(hotDeployEvent);
052                    }
053                    catch (Throwable t) {
054                            throwHotDeployException(
055                                    hotDeployEvent, "Error unregistering theme loader for ", t);
056                    }
057            }
058    
059            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
060                    throws Exception {
061    
062                    ServletContext servletContext = hotDeployEvent.getServletContext();
063    
064                    String servletContextName = servletContext.getServletContextName();
065    
066                    if (_log.isDebugEnabled()) {
067                            _log.debug("Invoking deploy for " + servletContextName);
068                    }
069    
070                    String[] xmls = new String[] {
071                            HttpUtil.URLtoString(
072                                    servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
073                    };
074    
075                    if (xmls[0] == null) {
076                            return;
077                    }
078    
079                    if (_log.isInfoEnabled()) {
080                            _log.info("Registering theme loader for " + servletContextName);
081                    }
082    
083                    ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
084            }
085    
086            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
087                    throws Exception {
088    
089                    ServletContext servletContext = hotDeployEvent.getServletContext();
090    
091                    String servletContextName = servletContext.getServletContextName();
092    
093                    if (_log.isDebugEnabled()) {
094                            _log.debug("Invoking undeploy for " + servletContextName);
095                    }
096    
097                    boolean value = ThemeLoaderFactory.destroy(servletContextName);
098    
099                    if (!value) {
100                            return;
101                    }
102    
103                    if (_log.isInfoEnabled()) {
104                            _log.info("Unregistering theme loader for " + servletContextName);
105                    }
106    
107                    ServletContextPool.remove(servletContextName);
108    
109                    if (_log.isInfoEnabled()) {
110                            _log.info(
111                                    "Theme loader for " + servletContextName +
112                                            " unregistered successfully");
113                    }
114            }
115    
116            private static final Log _log = LogFactoryUtil.getLog(
117                    ThemeLoaderHotDeployListener.class);
118    
119    }