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