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.portlet.PortletClassLoaderUtil;
28 import com.liferay.portal.kernel.util.AggregateClassLoader;
29 import com.liferay.portal.kernel.util.ArrayUtil;
30 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
31 import com.liferay.portal.util.PropsKeys;
32
33 import java.io.FileNotFoundException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
39 import org.springframework.web.context.support.XmlWebApplicationContext;
40
41
53 public class PortletApplicationContext extends XmlWebApplicationContext {
54
55 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
56 reader.setBeanClassLoader(
57 new AggregateClassLoader(
58 PortletClassLoaderUtil.getClassLoader(),
59 PortalClassLoaderUtil.getClassLoader()));
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 =
115 LogFactory.getLog(PortletApplicationContext.class);
116
117 }