1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.spring.context;
21  
22  import com.liferay.portal.kernel.configuration.Configuration;
23  import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
27  import com.liferay.portal.kernel.util.AggregateClassLoader;
28  import com.liferay.portal.kernel.util.ArrayUtil;
29  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
30  import com.liferay.portal.util.PropsKeys;
31  
32  import java.io.FileNotFoundException;
33  
34  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
35  import org.springframework.web.context.support.XmlWebApplicationContext;
36  
37  /**
38   * <a href="PortletApplicationContext.java.html"><b><i>View Source</i></b></a>
39   *
40   * <p>
41   * This web application context will first load bean definitions in the
42   * contextConfigLocation parameter in web.xml. Then, the context will load bean
43   * definitions specified by the property "spring.configs" in service.properties.
44   * </p>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class PortletApplicationContext extends XmlWebApplicationContext {
50  
51      protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
52          reader.setBeanClassLoader(
53              new AggregateClassLoader(
54                  PortletClassLoaderUtil.getClassLoader(),
55                  PortalClassLoaderUtil.getClassLoader()));
56      }
57  
58      protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
59          String[] configLocations = getPortletConfigLocations();
60  
61          if (configLocations == null) {
62              return;
63          }
64  
65          for (String configLocation : configLocations) {
66              try {
67                  reader.loadBeanDefinitions(configLocation);
68              }
69              catch (Exception e) {
70                  Throwable cause = e.getCause();
71  
72                  if (cause instanceof FileNotFoundException) {
73                      if (_log.isWarnEnabled()) {
74                          _log.warn(cause.getMessage());
75                      }
76                  }
77                  else {
78                      _log.error(e, e);
79                  }
80              }
81          }
82      }
83  
84      protected String[] getPortletConfigLocations() {
85          String[] configLocations = getConfigLocations();
86  
87          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
88  
89          Configuration serviceBuilderPropertiesConfiguration = null;
90  
91          try {
92              serviceBuilderPropertiesConfiguration =
93                  ConfigurationFactoryUtil.getConfiguration(
94                      classLoader, "service");
95          }
96          catch (Exception e) {
97              if (_log.isDebugEnabled()) {
98                  _log.debug("Unable to read service.properties");
99              }
100 
101             return configLocations;
102         }
103 
104         return ArrayUtil.append(
105             configLocations,
106             serviceBuilderPropertiesConfiguration.getArray(
107                 PropsKeys.SPRING_CONFIGS));
108     }
109 
110     private static Log _log =
111         LogFactoryUtil.getLog(PortletApplicationContext.class);
112 
113 }