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
35 import java.io.FileNotFoundException;
36
37 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
38 import org.springframework.web.context.support.XmlWebApplicationContext;
39
40
52 public class PortletApplicationContext extends XmlWebApplicationContext {
53
54 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
55 reader.setBeanClassLoader(
56 new AggregateClassLoader(
57 PortletClassLoaderUtil.getClassLoader(),
58 PortalClassLoaderUtil.getClassLoader()));
59 }
60
61 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
62 String[] configLocations = getPortletConfigLocations();
63
64 if (configLocations == null) {
65 return;
66 }
67
68 for (String configLocation : configLocations) {
69 try {
70 reader.loadBeanDefinitions(configLocation);
71 }
72 catch (Exception e) {
73 Throwable cause = e.getCause();
74
75 if (cause instanceof FileNotFoundException) {
76 if (_log.isWarnEnabled()) {
77 _log.warn(cause.getMessage());
78 }
79 }
80 else {
81 _log.error(e, e);
82 }
83 }
84 }
85 }
86
87 protected String[] getPortletConfigLocations() {
88 String[] configLocations = getConfigLocations();
89
90 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
91
92 Configuration serviceBuilderPropertiesConfiguration = null;
93
94 try {
95 serviceBuilderPropertiesConfiguration =
96 ConfigurationFactoryUtil.getConfiguration(
97 classLoader, "service");
98 }
99 catch (Exception e) {
100 if (_log.isDebugEnabled()) {
101 _log.debug("Unable to read service.properties");
102 }
103
104 return configLocations;
105 }
106
107 return ArrayUtil.append(
108 configLocations,
109 serviceBuilderPropertiesConfiguration.getArray(
110 PropsKeys.SPRING_CONFIGS));
111 }
112
113 private static Log _log =
114 LogFactoryUtil.getLog(PortletApplicationContext.class);
115
116 }