001
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.PortalClassLoaderUtil;
025 import com.liferay.portal.kernel.util.PropsKeys;
026 import com.liferay.portal.spring.util.FilterClassLoader;
027
028 import java.io.FileNotFoundException;
029
030 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
031 import org.springframework.web.context.support.XmlWebApplicationContext;
032
033
045 public class PortletApplicationContext extends XmlWebApplicationContext {
046
047 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
048 ClassLoader beanClassLoader =
049 AggregateClassLoader.getAggregateClassLoader(
050 new ClassLoader[] {
051 PortletClassLoaderUtil.getClassLoader(),
052 PortalClassLoaderUtil.getClassLoader()
053 });
054
055 beanClassLoader = new FilterClassLoader(beanClassLoader);
056
057 reader.setBeanClassLoader(beanClassLoader);
058 }
059
060 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
061 String[] configLocations = getPortletConfigLocations();
062
063 if (configLocations == null) {
064 return;
065 }
066
067 for (String configLocation : configLocations) {
068 try {
069 reader.loadBeanDefinitions(configLocation);
070 }
071 catch (Exception e) {
072 Throwable cause = e.getCause();
073
074 if (cause instanceof FileNotFoundException) {
075 if (_log.isWarnEnabled()) {
076 _log.warn(cause.getMessage());
077 }
078 }
079 else {
080 _log.error(e, e);
081 }
082 }
083 }
084 }
085
086 protected String[] getPortletConfigLocations() {
087 String[] configLocations = getConfigLocations();
088
089 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
090
091 Configuration serviceBuilderPropertiesConfiguration = null;
092
093 try {
094 serviceBuilderPropertiesConfiguration =
095 ConfigurationFactoryUtil.getConfiguration(
096 classLoader, "service");
097 }
098 catch (Exception e) {
099 if (_log.isDebugEnabled()) {
100 _log.debug("Unable to read service.properties");
101 }
102
103 return configLocations;
104 }
105
106 return ArrayUtil.append(
107 configLocations,
108 serviceBuilderPropertiesConfiguration.getArray(
109 PropsKeys.SPRING_CONFIGS));
110 }
111
112 private static Log _log = LogFactoryUtil.getLog(
113 PortletApplicationContext.class);
114
115 }