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