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