1
22
23 package com.liferay.portal.spring.context;
24
25 import com.liferay.portal.kernel.configuration.Configuration;
26 import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
30 import com.liferay.portal.kernel.util.AggregateClassLoader;
31 import com.liferay.portal.kernel.util.ArrayUtil;
32 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
33 import com.liferay.portal.util.PropsKeys;
34 import com.liferay.portal.spring.util.FilterClassLoader;
35
36 import java.io.FileNotFoundException;
37
38 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
39 import org.springframework.web.context.support.XmlWebApplicationContext;
40
41
55 public class PortletApplicationContext extends XmlWebApplicationContext {
56
57 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
58 ClassLoader beanClassLoader =
59 AggregateClassLoader.getAggregateClassLoader(
60 new ClassLoader[] {
61 PortletClassLoaderUtil.getClassLoader(),
62 PortalClassLoaderUtil.getClassLoader()
63 });
64
65 beanClassLoader = new FilterClassLoader(beanClassLoader);
66
67 reader.setBeanClassLoader(beanClassLoader);
68 }
69
70 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
71 String[] configLocations = getPortletConfigLocations();
72
73 if (configLocations == null) {
74 return;
75 }
76
77 for (String configLocation : configLocations) {
78 try {
79 reader.loadBeanDefinitions(configLocation);
80 }
81 catch (Exception e) {
82 Throwable cause = e.getCause();
83
84 if (cause instanceof FileNotFoundException) {
85 if (_log.isWarnEnabled()) {
86 _log.warn(cause.getMessage());
87 }
88 }
89 else {
90 _log.error(e, e);
91 }
92 }
93 }
94 }
95
96 protected String[] getPortletConfigLocations() {
97 String[] configLocations = getConfigLocations();
98
99 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
100
101 Configuration serviceBuilderPropertiesConfiguration = null;
102
103 try {
104 serviceBuilderPropertiesConfiguration =
105 ConfigurationFactoryUtil.getConfiguration(
106 classLoader, "service");
107 }
108 catch (Exception e) {
109 if (_log.isDebugEnabled()) {
110 _log.debug("Unable to read service.properties");
111 }
112
113 return configLocations;
114 }
115
116 return ArrayUtil.append(
117 configLocations,
118 serviceBuilderPropertiesConfiguration.getArray(
119 PropsKeys.SPRING_CONFIGS));
120 }
121
122 private static Log _log =
123 LogFactoryUtil.getLog(PortletApplicationContext.class);
124
125 }