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