001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.util;
016    
017    import com.liferay.portal.bean.BeanLocatorImpl;
018    import com.liferay.portal.kernel.bean.BeanLocator;
019    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
022    import com.liferay.portal.spring.context.ArrayApplicationContext;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.util.List;
026    
027    import org.springframework.context.support.AbstractApplicationContext;
028    
029    /**
030     * <p>
031     * In most cases, SpringUtil.setContext() would have been called by
032     * com.liferay.portal.spring.context.PortalContextLoaderListener, configured in
033     * web.xml for the web application. However, there will be times in which
034     * SpringUtil will be called in a non-web application and, therefore, require
035     * manual instantiation of the application context.
036     * </p>
037     *
038     * @author Michael Young
039     */
040    public class SpringUtil {
041    
042            public static void loadContext() {
043                    List<String> configLocations = ListUtil.fromArray(
044                            PropsValues.SPRING_CONFIGS);
045    
046                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
047                            configLocations.remove("META-INF/hibernate-spring.xml");
048                    }
049                    else {
050                            configLocations.remove("META-INF/jpa-spring.xml");
051                    }
052    
053                    AbstractApplicationContext applicationContext =
054                            new ArrayApplicationContext(
055                                    configLocations.toArray(new String[configLocations.size()]));
056    
057                    applicationContext.registerShutdownHook();
058    
059                    BeanLocator beanLocator = new BeanLocatorImpl(
060                            PortalClassLoaderUtil.getClassLoader(), applicationContext);
061    
062                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
063            }
064    
065    }