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 @Override
048 protected String[] getDefaultConfigLocations() {
049 return new String[0];
050 }
051
052 protected String[] getPortletConfigLocations() {
053 String[] configLocations = getConfigLocations();
054
055 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
056
057 Configuration serviceBuilderPropertiesConfiguration = null;
058
059 try {
060 serviceBuilderPropertiesConfiguration =
061 ConfigurationFactoryUtil.getConfiguration(
062 classLoader, "service");
063 }
064 catch (Exception e) {
065 if (_log.isDebugEnabled()) {
066 _log.debug("Unable to read service.properties");
067 }
068
069 return configLocations;
070 }
071
072 return ArrayUtil.append(
073 configLocations,
074 serviceBuilderPropertiesConfiguration.getArray(
075 PropsKeys.SPRING_CONFIGS));
076 }
077
078 @Override
079 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
080 ClassLoader beanClassLoader =
081 AggregateClassLoader.getAggregateClassLoader(
082 new ClassLoader[] {
083 PortletClassLoaderUtil.getClassLoader(),
084 PortalClassLoaderUtil.getClassLoader()
085 });
086
087 beanClassLoader = new FilterClassLoader(beanClassLoader);
088
089 reader.setBeanClassLoader(beanClassLoader);
090 }
091
092 @Override
093 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
094 String[] configLocations = getPortletConfigLocations();
095
096 if (configLocations == null) {
097 return;
098 }
099
100 for (String configLocation : configLocations) {
101 try {
102 reader.loadBeanDefinitions(configLocation);
103 }
104 catch (Exception e) {
105 Throwable cause = e.getCause();
106
107 if (cause instanceof FileNotFoundException) {
108 if (_log.isWarnEnabled()) {
109 _log.warn(cause.getMessage());
110 }
111 }
112 else {
113 _log.error(e, e);
114 }
115 }
116 }
117 }
118
119 private static Log _log = LogFactoryUtil.getLog(
120 PortletApplicationContext.class);
121
122 }