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