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.spring.context;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.util.PropsValues;
021    
022    import java.io.FileNotFoundException;
023    
024    import java.util.List;
025    
026    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
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            @Override
044            protected void loadBeanDefinitions(
045                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
046    
047                    try {
048                            super.loadBeanDefinitions(xmlBeanDefinitionReader);
049                    }
050                    catch (Exception e) {
051                            if (_log.isWarnEnabled()) {
052                                    _log.warn(e, e);
053                            }
054                    }
055    
056                    xmlBeanDefinitionReader.setResourceLoader(
057                            new PathMatchingResourcePatternResolver());
058    
059                    if (PropsValues.SPRING_CONFIGS == null) {
060                            return;
061                    }
062    
063                    List<String> configLocations = ListUtil.fromArray(
064                            PropsValues.SPRING_CONFIGS);
065    
066                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
067                            configLocations.remove("META-INF/hibernate-spring.xml");
068                    }
069                    else {
070                            configLocations.remove("META-INF/jpa-spring.xml");
071                    }
072    
073                    for (String configLocation : configLocations) {
074                            try {
075                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
076                            }
077                            catch (Exception e) {
078                                    Throwable cause = e.getCause();
079    
080                                    if (cause instanceof FileNotFoundException) {
081                                            if (_log.isWarnEnabled()) {
082                                                    _log.warn(cause.getMessage());
083                                            }
084                                    }
085                                    else {
086                                            _log.error(e, e);
087                                    }
088                            }
089                    }
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(
093                    PortalApplicationContext.class);
094    
095    }