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.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            public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
034                    try {
035                            doInvokeDeploy(event);
036                    }
037                    catch (Throwable t) {
038                            throwHotDeployException(
039                                    event, "Error registering theme loader for ", t);
040                    }
041            }
042    
043            public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
044                    try {
045                            doInvokeUndeploy(event);
046                    }
047                    catch (Throwable t) {
048                            throwHotDeployException(
049                                    event, "Error unregistering theme loader for ", t);
050                    }
051            }
052    
053            protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
054                    ServletContext servletContext = event.getServletContext();
055    
056                    String servletContextName = servletContext.getServletContextName();
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Invoking deploy for " + servletContextName);
060                    }
061    
062                    String[] xmls = new String[] {
063                            HttpUtil.URLtoString(
064                                    servletContext.getResource("/WEB-INF/liferay-theme-loader.xml"))
065                    };
066    
067                    if (xmls[0] == null) {
068                            return;
069                    }
070    
071                    if (_log.isInfoEnabled()) {
072                            _log.info("Registering theme loader for " + servletContextName);
073                    }
074    
075                    ThemeLoaderFactory.init(servletContextName, servletContext, xmls);
076            }
077    
078            protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
079                    ServletContext servletContext = event.getServletContext();
080    
081                    String servletContextName = servletContext.getServletContextName();
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Invoking undeploy for " + servletContextName);
085                    }
086    
087                    boolean value = ThemeLoaderFactory.destroy(servletContextName);
088    
089                    if (!value) {
090                            return;
091                    }
092    
093                    if (_log.isInfoEnabled()) {
094                            _log.info("Unregistering theme loader for " + servletContextName);
095                    }
096    
097                    ServletContextPool.remove(servletContextName);
098    
099                    if (_log.isInfoEnabled()) {
100                            _log.info(
101                                    "Theme loader for " + servletContextName +
102                                            " unregistered successfully");
103                    }
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    ThemeLoaderHotDeployListener.class);
108    
109    }