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.bean.BeanLocatorImpl;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.FileNotFoundException;
023    
024    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
025    import org.springframework.context.ApplicationContext;
026    import org.springframework.web.context.support.XmlWebApplicationContext;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class TunnelApplicationContext extends XmlWebApplicationContext {
032    
033            @Override
034            public void setParent(ApplicationContext applicationContext) {
035                    if (applicationContext == null) {
036                            BeanLocatorImpl beanLocatorImpl =
037                                    (BeanLocatorImpl)PortalBeanLocatorUtil.getBeanLocator();
038    
039                            applicationContext = beanLocatorImpl.getApplicationContext();
040                    }
041    
042                    super.setParent(applicationContext);
043            }
044    
045            @Override
046            protected void loadBeanDefinitions(
047                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
048    
049                    String[] configLocations = getConfigLocations();
050    
051                    if (configLocations == null) {
052                            return;
053                    }
054    
055                    for (String configLocation : configLocations) {
056                            try {
057                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
058                            }
059                            catch (Exception e) {
060                                    Throwable cause = e.getCause();
061    
062                                    if (cause instanceof FileNotFoundException) {
063                                            if (_log.isWarnEnabled()) {
064                                                    _log.warn(cause.getMessage());
065                                            }
066                                    }
067                                    else {
068                                            _log.error(e, e);
069                                    }
070                            }
071                    }
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(
075                    TunnelApplicationContext.class);
076    
077    }