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