1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.spring.context;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ListUtil;
20  import com.liferay.portal.util.PropsValues;
21  
22  import java.io.FileNotFoundException;
23  
24  import java.util.List;
25  
26  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
27  import org.springframework.core.io.DefaultResourceLoader;
28  import org.springframework.web.context.support.XmlWebApplicationContext;
29  
30  /**
31   * <a href="PortalApplicationContext.java.html"><b><i>View Source</i></b></a>
32   *
33   * <p>
34   * This web application context will first load bean definitions in the
35   * contextConfigLocation parameter in web.xml. Then, the context will load bean
36   * definitions specified by the property "spring.configs" in portal.properties.
37   * </p>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Alexander Chow
41   */
42  public class PortalApplicationContext extends XmlWebApplicationContext {
43  
44      protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
45          try {
46              super.loadBeanDefinitions(reader);
47          }
48          catch (Exception e) {
49              if (_log.isWarnEnabled()) {
50                  _log.warn(e, e);
51              }
52          }
53  
54          reader.setResourceLoader(new DefaultResourceLoader());
55  
56          if (PropsValues.SPRING_CONFIGS == null) {
57              return;
58          }
59  
60          List<String> configLocations = ListUtil.fromArray(
61              PropsValues.SPRING_CONFIGS);
62  
63          if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
64              configLocations.remove("META-INF/hibernate-spring.xml");
65          }
66          else {
67              configLocations.remove("META-INF/jpa-spring.xml");
68          }
69  
70          for (String configLocation : configLocations) {
71              try {
72                  reader.loadBeanDefinitions(configLocation);
73              }
74              catch (Exception e) {
75                  Throwable cause = e.getCause();
76  
77                  if (cause instanceof FileNotFoundException) {
78                      if (_log.isWarnEnabled()) {
79                          _log.warn(cause.getMessage());
80                      }
81                  }
82                  else {
83                      _log.error(e, e);
84                  }
85              }
86          }
87      }
88  
89      private static Log _log = LogFactoryUtil.getLog(
90          PortalApplicationContext.class);
91  
92  }