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.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            protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
048                    ClassLoader beanClassLoader =
049                            AggregateClassLoader.getAggregateClassLoader(
050                                    new ClassLoader[] {
051                                            PortletClassLoaderUtil.getClassLoader(),
052                                            PortalClassLoaderUtil.getClassLoader()
053                                    });
054    
055                    beanClassLoader = new FilterClassLoader(beanClassLoader);
056    
057                    reader.setBeanClassLoader(beanClassLoader);
058            }
059    
060            protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
061                    String[] configLocations = getPortletConfigLocations();
062    
063                    if (configLocations == null) {
064                            return;
065                    }
066    
067                    for (String configLocation : configLocations) {
068                            try {
069                                    reader.loadBeanDefinitions(configLocation);
070                            }
071                            catch (Exception e) {
072                                    Throwable cause = e.getCause();
073    
074                                    if (cause instanceof FileNotFoundException) {
075                                            if (_log.isWarnEnabled()) {
076                                                    _log.warn(cause.getMessage());
077                                            }
078                                    }
079                                    else {
080                                            _log.error(e, e);
081                                    }
082                            }
083                    }
084            }
085    
086            protected String[] getPortletConfigLocations() {
087                    String[] configLocations = getConfigLocations();
088    
089                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
090    
091                    Configuration serviceBuilderPropertiesConfiguration = null;
092    
093                    try {
094                            serviceBuilderPropertiesConfiguration =
095                                    ConfigurationFactoryUtil.getConfiguration(
096                                            classLoader, "service");
097                    }
098                    catch (Exception e) {
099                            if (_log.isDebugEnabled()) {
100                                    _log.debug("Unable to read service.properties");
101                            }
102    
103                            return configLocations;
104                    }
105    
106                    return ArrayUtil.append(
107                            configLocations,
108                            serviceBuilderPropertiesConfiguration.getArray(
109                                    PropsKeys.SPRING_CONFIGS));
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    PortletApplicationContext.class);
114    
115    }