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