001    /**
002     * Copyright (c) 2000-2012 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.PreloadClassLoader;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
027    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
028    import com.liferay.portal.security.pacl.PACLPolicyManager;
029    import com.liferay.portal.spring.util.FilterClassLoader;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.io.FileNotFoundException;
033    
034    import java.util.HashMap;
035    import java.util.Map;
036    
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                    if (_isUseRestrictedClassLoader()) {
055                            boolean enabled = PortalSecurityManagerThreadLocal.isEnabled();
056    
057                            try {
058                                    PortalSecurityManagerThreadLocal.setEnabled(false);
059    
060                                    return new PreloadClassLoader(
061                                            PortletClassLoaderUtil.getClassLoader(), _classes);
062                            }
063                            finally {
064                                    PortalSecurityManagerThreadLocal.setEnabled(enabled);
065                            }
066                    }
067    
068                    ClassLoader beanClassLoader =
069                            AggregateClassLoader.getAggregateClassLoader(
070                                    new ClassLoader[] {
071                                            PortletClassLoaderUtil.getClassLoader(),
072                                            PACLClassLoaderUtil.getPortalClassLoader()
073                                    });
074    
075                    return new FilterClassLoader(beanClassLoader);
076            }
077    
078            @Override
079            protected String[] getDefaultConfigLocations() {
080                    return new String[0];
081            }
082    
083            protected String[] getPortletConfigLocations() {
084                    String[] configLocations = getConfigLocations();
085    
086                    ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
087    
088                    Configuration serviceBuilderPropertiesConfiguration = null;
089    
090                    try {
091                            serviceBuilderPropertiesConfiguration =
092                                    ConfigurationFactoryUtil.getConfiguration(
093                                            classLoader, "service");
094                    }
095                    catch (Exception e) {
096                            if (_log.isDebugEnabled()) {
097                                    _log.debug("Unable to read service.properties");
098                            }
099    
100                            return configLocations;
101                    }
102    
103                    return ArrayUtil.append(
104                            configLocations,
105                            serviceBuilderPropertiesConfiguration.getArray(
106                                    PropsKeys.SPRING_CONFIGS));
107            }
108    
109            @Override
110            protected void initBeanDefinitionReader(
111                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
112    
113                    xmlBeanDefinitionReader.setBeanClassLoader(getBeanClassLoader());
114            }
115    
116            @Override
117            protected void loadBeanDefinitions(
118                    XmlBeanDefinitionReader xmlBeanDefinitionReader) {
119    
120                    String[] configLocations = getPortletConfigLocations();
121    
122                    if (configLocations == null) {
123                            return;
124                    }
125    
126                    for (String configLocation : configLocations) {
127                            boolean checkReadFile =
128                                    PortalSecurityManagerThreadLocal.isCheckReadFile();
129    
130                            try {
131                                    PortalSecurityManagerThreadLocal.setCheckReadFile(false);
132    
133                                    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
134                            }
135                            catch (Exception e) {
136                                    Throwable cause = e.getCause();
137    
138                                    if (cause instanceof FileNotFoundException) {
139                                            if (_log.isWarnEnabled()) {
140                                                    _log.warn(cause.getMessage());
141                                            }
142                                    }
143                                    else {
144                                            _log.error(e, e);
145                                    }
146                            }
147                            finally {
148                                    PortalSecurityManagerThreadLocal.setCheckReadFile(
149                                            checkReadFile);
150                            }
151                    }
152            }
153    
154            private static boolean _isUseRestrictedClassLoader() {
155                    return PACLPolicyManager.isActive();
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(
159                    PortletApplicationContext.class);
160    
161            private static Map<String, Class<?>> _classes =
162                    new HashMap<String, Class<?>>();
163    
164            static {
165                    for (String className :
166                                    PropsValues.
167                                            PORTAL_SECURITY_MANAGER_PRELOAD_CLASSLOADER_CLASSES) {
168    
169                            Class<?> clazz = null;
170    
171                            try {
172                                    clazz = Class.forName(className);
173                            }
174                            catch (ClassNotFoundException cnfe) {
175                                    _log.error(cnfe, cnfe);
176                            }
177    
178                            _classes.put(clazz.getName(), clazz);
179                    }
180            }
181    
182    }