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            public static interface PACL {
054    
055                    public ClassLoader getBeanClassLoader();
056    
057            }
058    
059            @Override
060            protected String[] getDefaultConfigLocations() {
061                    return new String[0];
062            }
063    
064            protected String[] getPortletConfigLocations() {
065                    String[] configLocations = getConfigLocations();
066    
067                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
068    
069                    Configuration serviceBuilderPropertiesConfiguration = null;
070    
071                    try {
072                            serviceBuilderPropertiesConfiguration =
073                                    ConfigurationFactoryUtil.getConfiguration(
074                                            classLoader, "service");
075                    }
076                    catch (Exception e) {
077                            if (_log.isDebugEnabled()) {
078                                    _log.debug("Unable to read service.properties");
079                            }
080    
081                            return configLocations;
082                    }
083    
084                    return ArrayUtil.append(
085                            configLocations,
086                            serviceBuilderPropertiesConfiguration.getArray(
087                                    PropsKeys.SPRING_CONFIGS));
088            }
089    
090            @Override
091            protected void initBeanDefinitionReader(
092                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
093    
094                    xmlBeanDefinitionReader.setBeanClassLoader(getBeanClassLoader());
095            }
096    
097            protected void injectExplicitBean(
098                    Class<?> clazz, BeanDefinitionRegistry beanDefinitionRegistry) {
099    
100                    beanDefinitionRegistry.registerBeanDefinition(
101                            clazz.getName(), new RootBeanDefinition(clazz));
102            }
103    
104            protected void injectExplicitBeans(
105                    BeanDefinitionRegistry beanDefinitionRegistry) {
106    
107                    injectExplicitBean(DoPrivilegedFactory.class, beanDefinitionRegistry);
108            }
109    
110            @Override
111            protected void loadBeanDefinitions(
112                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
113    
114                    String[] configLocations = getPortletConfigLocations();
115    
116                    if (configLocations == null) {
117                            return;
118                    }
119    
120                    BeanDefinitionRegistry beanDefinitionRegistry =
121                            xmlBeanDefinitionReader.getBeanFactory();
122    
123                    injectExplicitBeans(beanDefinitionRegistry);
124    
125                    for (String configLocation : configLocations) {
126                            try {
127                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
128                            }
129                            catch (Exception e) {
130                                    Throwable cause = e.getCause();
131    
132                                    if (cause instanceof FileNotFoundException) {
133                                            if (_log.isWarnEnabled()) {
134                                                    _log.warn(cause.getMessage());
135                                            }
136                                    }
137                                    else {
138                                            _log.error(e, e);
139                                    }
140                            }
141                    }
142            }
143    
144            private static Log _log = LogFactoryUtil.getLog(
145                    PortletApplicationContext.class);
146    
147            private static PACL _pacl = new NoPACL();
148    
149            private static class NoPACL implements PACL {
150    
151                    @Override
152                    public ClassLoader getBeanClassLoader() {
153                            ClassLoader beanClassLoader =
154                                    AggregateClassLoader.getAggregateClassLoader(
155                                            new ClassLoader[] {
156                                                    PortletClassLoaderUtil.getClassLoader(),
157                                                    ClassLoaderUtil.getPortalClassLoader()
158                                            });
159    
160                            return new FilterClassLoader(beanClassLoader);
161                    }
162    
163            }
164    
165    }