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