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.util;
16  
17  import com.liferay.portal.bean.BeanLocatorImpl;
18  import com.liferay.portal.kernel.bean.BeanLocator;
19  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
20  import com.liferay.portal.kernel.util.ListUtil;
21  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
22  import com.liferay.portal.spring.context.ArrayApplicationContext;
23  import com.liferay.portal.util.PropsValues;
24  
25  import java.util.List;
26  
27  import org.springframework.context.support.AbstractApplicationContext;
28  
29  /**
30   * <a href="SpringUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * <p>
33   * In most cases, SpringUtil.setContext() would have been called by
34   * com.liferay.portal.spring.context.PortalContextLoaderListener, configured in
35   * web.xml for the web application. However, there will be times in which
36   * SpringUtil will be called in a non-web application and, therefore, require
37   * manual instantiation of the application context.
38   * </p>
39   *
40   * @author Michael Young
41   */
42  public class SpringUtil {
43  
44      public static void loadContext() {
45          List<String> configLocations = ListUtil.fromArray(
46              PropsValues.SPRING_CONFIGS);
47  
48          if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
49              configLocations.remove("META-INF/hibernate-spring.xml");
50          }
51          else {
52              configLocations.remove("META-INF/jpa-spring.xml");
53          }
54  
55          AbstractApplicationContext applicationContext =
56              new ArrayApplicationContext(
57                  configLocations.toArray(new String[configLocations.size()]));
58  
59          applicationContext.registerShutdownHook();
60  
61          BeanLocator beanLocator = new BeanLocatorImpl(
62              PortalClassLoaderUtil.getClassLoader(), applicationContext);
63  
64          PortalBeanLocatorUtil.setBeanLocator(beanLocator);
65      }
66  
67  }