001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.kernel.util.PropsKeys;
023    import com.liferay.portal.spring.context.ArrayApplicationContext;
024    import com.liferay.portal.util.PropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.util.List;
028    
029    import org.springframework.context.support.AbstractApplicationContext;
030    
031    /**
032     * <p>
033     * In most cases, SpringUtil.setContext() would have been called by
034     * com.liferay.portal.spring.context.PortalContextLoaderListener, configured in
035     * web.xml for the web application. However, there will be times in which
036     * SpringUtil will be called in a non-web application and, therefore, require
037     * manual instantiation of the application context.
038     * </p>
039     *
040     * @author Michael Young
041     */
042    public class SpringUtil {
043    
044            public static void loadContext() {
045                    List<String> configLocations = ListUtil.fromArray(
046                            PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
047    
048                    if (PropsValues.PERSISTENCE_PROVIDER.equalsIgnoreCase("jpa")) {
049                            configLocations.remove("META-INF/hibernate-spring.xml");
050                    }
051                    else {
052                            configLocations.remove("META-INF/jpa-spring.xml");
053                    }
054    
055                    AbstractApplicationContext applicationContext =
056                            new ArrayApplicationContext(
057                                    configLocations.toArray(new String[configLocations.size()]));
058    
059                    BeanLocator beanLocator = new BeanLocatorImpl(
060                            PortalClassLoaderUtil.getClassLoader(), applicationContext);
061    
062                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
063            }
064    
065    }