001    /**
002     * Copyright (c) 2000-present 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.dao.shard.ShardUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
023    import com.liferay.portal.kernel.util.AggregateClassLoader;
024    import com.liferay.portal.kernel.util.ArrayUtil;
025    import com.liferay.portal.kernel.util.ClassLoaderUtil;
026    import com.liferay.portal.kernel.util.ListUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.security.lang.DoPrivilegedFactory;
029    import com.liferay.portal.spring.util.FilterClassLoader;
030    
031    import java.io.FileNotFoundException;
032    
033    import java.util.List;
034    
035    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
036    import org.springframework.beans.factory.support.RootBeanDefinition;
037    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
038    import org.springframework.web.context.support.XmlWebApplicationContext;
039    
040    /**
041     * <p>
042     * This web application context will first load bean definitions in the
043     * portalContextConfigLocation parameter in web.xml. Then, the context will load
044     * bean definitions specified by the property "spring.configs" in
045     * service.properties.
046     * </p>
047     *
048     * @author Brian Wing Shun Chan
049     * @see    PortletContextLoaderListener
050     */
051    public class PortletApplicationContext extends XmlWebApplicationContext {
052    
053            public static ClassLoader getBeanClassLoader() {
054                    return _pacl.getBeanClassLoader();
055            }
056    
057            public interface PACL {
058    
059                    public ClassLoader getBeanClassLoader();
060    
061            }
062    
063            @Override
064            protected String[] getDefaultConfigLocations() {
065                    return new String[0];
066            }
067    
068            protected String[] getPortletConfigLocations() {
069                    String[] configLocations = getConfigLocations();
070    
071                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
072    
073                    Configuration serviceBuilderPropertiesConfiguration = null;
074    
075                    try {
076                            serviceBuilderPropertiesConfiguration =
077                                    ConfigurationFactoryUtil.getConfiguration(
078                                            classLoader, "service");
079                    }
080                    catch (Exception e) {
081                            if (_log.isDebugEnabled()) {
082                                    _log.debug("Unable to read service.properties");
083                            }
084    
085                            return configLocations;
086                    }
087    
088                    // Remove old spring XMLs to ensure they are not read
089    
090                    List<String> serviceBuilderPropertiesConfigLocations =
091                            ListUtil.fromArray(
092                                    serviceBuilderPropertiesConfiguration.getArray(
093                                            PropsKeys.SPRING_CONFIGS));
094    
095                    serviceBuilderPropertiesConfigLocations.remove(
096                            "WEB-INF/classes/META-INF/base-spring.xml");
097                    serviceBuilderPropertiesConfigLocations.remove(
098                            "WEB-INF/classes/META-INF/cluster-spring.xml");
099                    serviceBuilderPropertiesConfigLocations.remove(
100                            "WEB-INF/classes/META-INF/hibernate-spring.xml");
101                    serviceBuilderPropertiesConfigLocations.remove(
102                            "WEB-INF/classes/META-INF/infrastructure-spring.xml");
103    
104                    if (ShardUtil.isEnabled()) {
105                            serviceBuilderPropertiesConfigLocations.remove(
106                                    "WEB-INF/classes/META-INF/shard-data-source-spring.xml");
107                    }
108    
109                    return ArrayUtil.append(
110                            configLocations,
111                            serviceBuilderPropertiesConfigLocations.toArray(
112                                    new String[serviceBuilderPropertiesConfigLocations.size()]));
113            }
114    
115            @Override
116            protected void initBeanDefinitionReader(
117                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
118    
119                    xmlBeanDefinitionReader.setBeanClassLoader(getBeanClassLoader());
120            }
121    
122            protected void injectExplicitBean(
123                    Class<?> clazz, BeanDefinitionRegistry beanDefinitionRegistry) {
124    
125                    beanDefinitionRegistry.registerBeanDefinition(
126                            clazz.getName(), new RootBeanDefinition(clazz));
127            }
128    
129            protected void injectExplicitBeans(
130                    BeanDefinitionRegistry beanDefinitionRegistry) {
131    
132                    injectExplicitBean(DoPrivilegedFactory.class, beanDefinitionRegistry);
133            }
134    
135            @Override
136            protected void loadBeanDefinitions(
137                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
138    
139                    String[] configLocations = getPortletConfigLocations();
140    
141                    if (configLocations == null) {
142                            return;
143                    }
144    
145                    BeanDefinitionRegistry beanDefinitionRegistry =
146                            xmlBeanDefinitionReader.getBeanFactory();
147    
148                    injectExplicitBeans(beanDefinitionRegistry);
149    
150                    for (String configLocation : configLocations) {
151                            try {
152                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
153                            }
154                            catch (Exception e) {
155                                    Throwable cause = e.getCause();
156    
157                                    if (cause instanceof FileNotFoundException) {
158                                            if (_log.isWarnEnabled()) {
159                                                    _log.warn(cause.getMessage());
160                                            }
161                                    }
162                                    else {
163                                            _log.error(e, e);
164                                    }
165                            }
166                    }
167            }
168    
169            private static final Log _log = LogFactoryUtil.getLog(
170                    PortletApplicationContext.class);
171    
172            private static final PACL _pacl = new NoPACL();
173    
174            private static class NoPACL implements PACL {
175    
176                    @Override
177                    public ClassLoader getBeanClassLoader() {
178                            ClassLoader beanClassLoader =
179                                    AggregateClassLoader.getAggregateClassLoader(
180                                            new ClassLoader[] {
181                                                    PortletClassLoaderUtil.getClassLoader(),
182                                                    ClassLoaderUtil.getPortalClassLoader()
183                                            });
184    
185                            return new FilterClassLoader(beanClassLoader);
186                    }
187    
188            }
189    
190    }