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 org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
024    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
025    import org.springframework.web.context.support.XmlWebApplicationContext;
026    
027    /**
028     * <p>
029     * This web application context will first load bean definitions in the
030     * contextConfigLocation parameter in web.xml. Then, the context will load bean
031     * definitions specified by the property "spring.configs" in portal.properties.
032     * </p>
033     *
034     * @author Brian Wing Shun Chan
035     * @author Alexander Chow
036     * @author Tomas Polesovsky
037     */
038    public class PortalApplicationContext extends XmlWebApplicationContext {
039    
040            @Override
041            protected void loadBeanDefinitions(
042                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
043    
044                    try {
045                            super.loadBeanDefinitions(xmlBeanDefinitionReader);
046                    }
047                    catch (Exception e) {
048                            if (_log.isWarnEnabled()) {
049                                    _log.warn(e, e);
050                            }
051                    }
052    
053                    xmlBeanDefinitionReader.setResourceLoader(
054                            new PathMatchingResourcePatternResolver());
055    
056                    if (PropsValues.SPRING_CONFIGS == null) {
057                            return;
058                    }
059    
060                    for (String configLocation : PropsValues.SPRING_CONFIGS) {
061                            try {
062                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
063                            }
064                            catch (Exception e) {
065                                    Throwable cause = e.getCause();
066    
067                                    if (cause instanceof FileNotFoundException) {
068                                            if (_log.isWarnEnabled()) {
069                                                    _log.warn(cause.getMessage());
070                                            }
071                                    }
072                                    else {
073                                            _log.error(e, e);
074                                    }
075                            }
076                    }
077            }
078    
079            private static final Log _log = LogFactoryUtil.getLog(
080                    PortalApplicationContext.class);
081    
082    }