1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
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  /**
34   * <a href="PortletApplicationContext.java.html"><b><i>View Source</i></b></a>
35   *
36   * <p>
37   * This web application context will first load bean definitions in the
38   * portalContextConfigLocation parameter in web.xml. Then, the context will load
39   * bean definitions specified by the property "spring.configs" in
40   * service.properties.
41   * </p>
42   *
43   * @author Brian Wing Shun Chan
44   * @see    PortletContextLoader
45   * @see    PortletContextLoaderListener
46   */
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 }