1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  import com.liferay.portal.spring.util.FilterClassLoader;
35  
36  import java.io.FileNotFoundException;
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   * portalContextConfigLocation parameter in web.xml. Then, the context will load
47   * bean definitions specified by the property "spring.configs" in
48   * service.properties.
49   * </p>
50   *
51   * @author Brian Wing Shun Chan
52   * @see    PortletContextLoader
53   * @see    PortletContextLoaderListener
54   */
55  public class PortletApplicationContext extends XmlWebApplicationContext {
56  
57      protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
58          ClassLoader beanClassLoader =
59              AggregateClassLoader.getAggregateClassLoader(
60                  new ClassLoader[] {
61                      PortletClassLoaderUtil.getClassLoader(),
62                      PortalClassLoaderUtil.getClassLoader()
63                  });
64  
65          beanClassLoader = new FilterClassLoader(beanClassLoader);
66  
67          reader.setBeanClassLoader(beanClassLoader);
68      }
69  
70      protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) {
71          String[] configLocations = getPortletConfigLocations();
72  
73          if (configLocations == null) {
74              return;
75          }
76  
77          for (String configLocation : configLocations) {
78              try {
79                  reader.loadBeanDefinitions(configLocation);
80              }
81              catch (Exception e) {
82                  Throwable cause = e.getCause();
83  
84                  if (cause instanceof FileNotFoundException) {
85                      if (_log.isWarnEnabled()) {
86                          _log.warn(cause.getMessage());
87                      }
88                  }
89                  else {
90                      _log.error(e, e);
91                  }
92              }
93          }
94      }
95  
96      protected String[] getPortletConfigLocations() {
97          String[] configLocations = getConfigLocations();
98  
99          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
100 
101         Configuration serviceBuilderPropertiesConfiguration = null;
102 
103         try {
104             serviceBuilderPropertiesConfiguration =
105                 ConfigurationFactoryUtil.getConfiguration(
106                     classLoader, "service");
107         }
108         catch (Exception e) {
109             if (_log.isDebugEnabled()) {
110                 _log.debug("Unable to read service.properties");
111             }
112 
113             return configLocations;
114         }
115 
116         return ArrayUtil.append(
117             configLocations,
118             serviceBuilderPropertiesConfiguration.getArray(
119                 PropsKeys.SPRING_CONFIGS));
120     }
121 
122     private static Log _log =
123         LogFactoryUtil.getLog(PortletApplicationContext.class);
124 
125 }