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.ClassLoaderUtil;
025 import com.liferay.portal.kernel.util.ListUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.security.lang.DoPrivilegedFactory;
028 import com.liferay.portal.spring.util.FilterClassLoader;
029
030 import java.io.FileNotFoundException;
031
032 import java.util.List;
033
034 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
035 import org.springframework.beans.factory.support.RootBeanDefinition;
036 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
037 import org.springframework.web.context.support.XmlWebApplicationContext;
038
039
050 public class PortletApplicationContext extends XmlWebApplicationContext {
051
052 public static ClassLoader getBeanClassLoader() {
053 return _pacl.getBeanClassLoader();
054 }
055
056 public interface PACL {
057
058 public ClassLoader getBeanClassLoader();
059
060 }
061
062 @Override
063 protected String[] getDefaultConfigLocations() {
064 return new String[0];
065 }
066
067 protected String[] getPortletConfigLocations() {
068 String[] configLocations = getConfigLocations();
069
070 ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
071
072 Configuration serviceBuilderPropertiesConfiguration = null;
073
074 try {
075 serviceBuilderPropertiesConfiguration =
076 ConfigurationFactoryUtil.getConfiguration(
077 classLoader, "service");
078 }
079 catch (Exception e) {
080 if (_log.isDebugEnabled()) {
081 _log.debug("Unable to read service.properties");
082 }
083
084 return configLocations;
085 }
086
087
088
089 List<String> serviceBuilderPropertiesConfigLocations =
090 ListUtil.fromArray(
091 serviceBuilderPropertiesConfiguration.getArray(
092 PropsKeys.SPRING_CONFIGS));
093
094 serviceBuilderPropertiesConfigLocations.remove(
095 "WEB-INF/classes/META-INF/base-spring.xml");
096 serviceBuilderPropertiesConfigLocations.remove(
097 "WEB-INF/classes/META-INF/cluster-spring.xml");
098 serviceBuilderPropertiesConfigLocations.remove(
099 "WEB-INF/classes/META-INF/hibernate-spring.xml");
100 serviceBuilderPropertiesConfigLocations.remove(
101 "WEB-INF/classes/META-INF/infrastructure-spring.xml");
102
103 return ArrayUtil.append(
104 configLocations,
105 serviceBuilderPropertiesConfigLocations.toArray(
106 new String[serviceBuilderPropertiesConfigLocations.size()]));
107 }
108
109 @Override
110 protected void initBeanDefinitionReader(
111 XmlBeanDefinitionReader xmlBeanDefinitionReader) {
112
113 xmlBeanDefinitionReader.setBeanClassLoader(getBeanClassLoader());
114 }
115
116 protected void injectExplicitBean(
117 Class<?> clazz, BeanDefinitionRegistry beanDefinitionRegistry) {
118
119 beanDefinitionRegistry.registerBeanDefinition(
120 clazz.getName(), new RootBeanDefinition(clazz));
121 }
122
123 protected void injectExplicitBeans(
124 BeanDefinitionRegistry beanDefinitionRegistry) {
125
126 injectExplicitBean(DoPrivilegedFactory.class, beanDefinitionRegistry);
127 }
128
129 @Override
130 protected void loadBeanDefinitions(
131 XmlBeanDefinitionReader xmlBeanDefinitionReader) {
132
133 String[] configLocations = getPortletConfigLocations();
134
135 if (configLocations == null) {
136 return;
137 }
138
139 BeanDefinitionRegistry beanDefinitionRegistry =
140 xmlBeanDefinitionReader.getBeanFactory();
141
142 injectExplicitBeans(beanDefinitionRegistry);
143
144 for (String configLocation : configLocations) {
145 try {
146 xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
147 }
148 catch (Exception e) {
149 Throwable cause = e.getCause();
150
151 if (cause instanceof FileNotFoundException) {
152 if (_log.isWarnEnabled()) {
153 _log.warn(cause.getMessage());
154 }
155 }
156 else {
157 _log.error(e, e);
158 }
159 }
160 }
161 }
162
163 private static final Log _log = LogFactoryUtil.getLog(
164 PortletApplicationContext.class);
165
166 private static final PACL _pacl = new NoPACL();
167
168 private static class NoPACL implements PACL {
169
170 @Override
171 public ClassLoader getBeanClassLoader() {
172 ClassLoader beanClassLoader =
173 AggregateClassLoader.getAggregateClassLoader(
174 new ClassLoader[] {
175 PortletClassLoaderUtil.getClassLoader(),
176 ClassLoaderUtil.getPortalClassLoader()
177 });
178
179 return new FilterClassLoader(beanClassLoader);
180 }
181
182 }
183
184 }