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