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