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.spring.context;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.io.FileNotFoundException;
022    
023    import javax.servlet.ServletContext;
024    
025    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
026    import org.springframework.context.ApplicationContext;
027    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
028    import org.springframework.web.context.support.XmlWebApplicationContext;
029    
030    /**
031     * <p>
032     * This web application context will first load bean definitions in the
033     * contextConfigLocation parameter in web.xml. Then, the context will load bean
034     * definitions specified by the property "spring.configs" in portal.properties.
035     * </p>
036     *
037     * @author Brian Wing Shun Chan
038     * @author Alexander Chow
039     * @author Tomas Polesovsky
040     */
041    public class PortalApplicationContext extends XmlWebApplicationContext {
042    
043            public static final String PARENT_APPLICATION_CONTEXT =
044                    PortalApplicationContext.class.getName() +
045                            "#PARENT_APPLICATION_CONTEXT";
046    
047            @Override
048            public ApplicationContext getParent() {
049                    return _parentApplicationContext;
050            }
051    
052            @Override
053            public void setServletContext(ServletContext servletContext) {
054                    super.setServletContext(servletContext);
055    
056                    _parentApplicationContext =
057                            (ApplicationContext)servletContext.getAttribute(
058                                    PARENT_APPLICATION_CONTEXT);
059    
060                    setParent(_parentApplicationContext);
061            }
062    
063            @Override
064            protected void loadBeanDefinitions(
065                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
066    
067                    try {
068                            super.loadBeanDefinitions(xmlBeanDefinitionReader);
069                    }
070                    catch (Exception e) {
071                            if (_log.isWarnEnabled()) {
072                                    _log.warn(e, e);
073                            }
074                    }
075    
076                    xmlBeanDefinitionReader.setResourceLoader(
077                            new PathMatchingResourcePatternResolver());
078    
079                    if (PropsValues.SPRING_CONFIGS == null) {
080                            return;
081                    }
082    
083                    for (String configLocation : PropsValues.SPRING_CONFIGS) {
084                            try {
085                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
086                            }
087                            catch (Exception e) {
088                                    Throwable cause = e.getCause();
089    
090                                    if (cause instanceof FileNotFoundException) {
091                                            if (_log.isWarnEnabled()) {
092                                                    _log.warn(cause.getMessage());
093                                            }
094                                    }
095                                    else {
096                                            _log.error(e, e);
097                                    }
098                            }
099                    }
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(
103                    PortalApplicationContext.class);
104    
105            private ApplicationContext _parentApplicationContext;
106    
107    }