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.DefaultResourceLoader;
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     */
040    public class PortalApplicationContext extends XmlWebApplicationContext {
041    
042            @Override
043            protected void loadBeanDefinitions(
044                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
045    
046                    try {
047                            super.loadBeanDefinitions(xmlBeanDefinitionReader);
048                    }
049                    catch (Exception e) {
050                            if (_log.isWarnEnabled()) {
051                                    _log.warn(e, e);
052                            }
053                    }
054    
055                    xmlBeanDefinitionReader.setResourceLoader(new DefaultResourceLoader());
056    
057                    if (PropsValues.SPRING_CONFIGS == null) {
058                            return;
059                    }
060    
061                    List<String> configLocations = ListUtil.fromArray(
062                            PropsValues.SPRING_CONFIGS);
063    
064                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
065                            configLocations.remove("META-INF/hibernate-spring.xml");
066                    }
067                    else {
068                            configLocations.remove("META-INF/jpa-spring.xml");
069                    }
070    
071                    for (String configLocation : configLocations) {
072                            try {
073                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
074                            }
075                            catch (Exception e) {
076                                    Throwable cause = e.getCause();
077    
078                                    if (cause instanceof FileNotFoundException) {
079                                            if (_log.isWarnEnabled()) {
080                                                    _log.warn(cause.getMessage());
081                                            }
082                                    }
083                                    else {
084                                            _log.error(e, e);
085                                    }
086                            }
087                    }
088            }
089    
090            private static Log _log = LogFactoryUtil.getLog(
091                    PortalApplicationContext.class);
092    
093    }