1   /**
2    * Copyright (c) 2000-2008 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.portlet.PortletClassLoaderUtil;
28  import com.liferay.portal.kernel.util.AggregateClassLoader;
29  import com.liferay.portal.kernel.util.ArrayUtil;
30  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
31  import com.liferay.portal.util.PropsKeys;
32  
33  import java.io.FileNotFoundException;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
39  import org.springframework.web.context.support.XmlWebApplicationContext;
40  
41  /**
42   * <a href="PortletApplicationContext.java.html"><b><i>View Source</i></b></a>
43   *
44   * <p>
45   * This web application context will first load bean definitions in the
46   * contextConfigLocation parameter in web.xml. Then, the context will load bean
47   * definitions specified by the property "spring.configs" in service.properties.
48   * </p>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class PortletApplicationContext extends XmlWebApplicationContext {
54  
55      protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
56          reader.setBeanClassLoader(
57              new AggregateClassLoader(
58                  PortletClassLoaderUtil.getClassLoader(),
59                  PortalClassLoaderUtil.getClassLoader()));
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 =
115         LogFactory.getLog(PortletApplicationContext.class);
116 
117 }