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.configuration.Configuration;
018    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
022    import com.liferay.portal.kernel.util.AggregateClassLoader;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.spring.util.FilterClassLoader;
027    
028    import java.io.FileNotFoundException;
029    
030    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
031    import org.springframework.web.context.support.XmlWebApplicationContext;
032    
033    /**
034     * <p>
035     * This web application context will first load bean definitions in the
036     * portalContextConfigLocation parameter in web.xml. Then, the context will load
037     * bean definitions specified by the property "spring.configs" in
038     * service.properties.
039     * </p>
040     *
041     * @author Brian Wing Shun Chan
042     * @see    PortletContextLoader
043     * @see    PortletContextLoaderListener
044     */
045    public class PortletApplicationContext extends XmlWebApplicationContext {
046    
047            @Override
048            protected String[] getDefaultConfigLocations() {
049                    return new String[0];
050            }
051    
052            @Override
053            protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
054                    ClassLoader beanClassLoader =
055                            AggregateClassLoader.getAggregateClassLoader(
056                                    new ClassLoader[] {
057                                            PortletClassLoaderUtil.getClassLoader(),
058                                            PortalClassLoaderUtil.getClassLoader()
059                                    });
060    
061                    beanClassLoader = new FilterClassLoader(beanClassLoader);
062    
063                    reader.setBeanClassLoader(beanClassLoader);
064            }
065    
066            @Override
067            protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
068                    String[] configLocations = getPortletConfigLocations();
069    
070                    if (configLocations == null) {
071                            return;
072                    }
073    
074                    for (String configLocation : configLocations) {
075                            try {
076                                    reader.loadBeanDefinitions(configLocation);
077                            }
078                            catch (Exception e) {
079                                    Throwable cause = e.getCause();
080    
081                                    if (cause instanceof FileNotFoundException) {
082                                            if (_log.isWarnEnabled()) {
083                                                    _log.warn(cause.getMessage());
084                                            }
085                                    }
086                                    else {
087                                            _log.error(e, e);
088                                    }
089                            }
090                    }
091            }
092    
093            protected String[] getPortletConfigLocations() {
094                    String[] configLocations = getConfigLocations();
095    
096                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
097    
098                    Configuration serviceBuilderPropertiesConfiguration = null;
099    
100                    try {
101                            serviceBuilderPropertiesConfiguration =
102                                    ConfigurationFactoryUtil.getConfiguration(
103                                            classLoader, "service");
104                    }
105                    catch (Exception e) {
106                            if (_log.isDebugEnabled()) {
107                                    _log.debug("Unable to read service.properties");
108                            }
109    
110                            return configLocations;
111                    }
112    
113                    return ArrayUtil.append(
114                            configLocations,
115                            serviceBuilderPropertiesConfiguration.getArray(
116                                    PropsKeys.SPRING_CONFIGS));
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(
120                    PortletApplicationContext.class);
121    
122    }