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