1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
41   * <a href="PortletApplicationContext.java.html"><b><i>View Source</i></b></a>
42   *
43   * <p>
44   * This web application context will first load bean definitions in the
45   * contextConfigLocation parameter in web.xml. Then, the context will load bean
46   * definitions specified by the property "spring.configs" in service.properties.
47   * </p>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
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 }