001    /**
002     * Copyright (c) 2000-2013 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.PropsKeys;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.spring.context.ArrayApplicationContext;
024    import com.liferay.portal.util.ClassLoaderUtil;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.util.List;
029    
030    import org.springframework.context.support.AbstractApplicationContext;
031    
032    /**
033     * <p>
034     * In most cases, SpringUtil.setContext() would have been called by
035     * com.liferay.portal.spring.context.PortalContextLoaderListener, configured in
036     * web.xml for the web application. However, there will be times in which
037     * SpringUtil will be called in a non-web application and, therefore, require
038     * manual instantiation of the application context.
039     * </p>
040     *
041     * @author Michael Young
042     */
043    public class SpringUtil {
044    
045            public static void loadContext() {
046                    List<String> configLocations = ListUtil.fromArray(
047                            PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
048    
049                    _loadContext(configLocations);
050            }
051    
052            public static void loadContext(List<String> extraConfigLocations) {
053                    List<String> configLocations = ListUtil.fromArray(
054                            PropsUtil.getArray(PropsKeys.SPRING_CONFIGS));
055    
056                    if (extraConfigLocations != null) {
057                            configLocations.addAll(extraConfigLocations);
058                    }
059    
060                    _loadContext(configLocations);
061            }
062    
063            private static void _loadContext(List<String> configLocations) {
064                    if (StringUtil.equalsIgnoreCase(
065                                    PropsValues.PERSISTENCE_PROVIDER, "jpa")) {
066    
067                            configLocations.remove("META-INF/hibernate-spring.xml");
068                    }
069                    else {
070                            configLocations.remove("META-INF/jpa-spring.xml");
071                    }
072    
073                    AbstractApplicationContext applicationContext =
074                            new ArrayApplicationContext(
075                                    configLocations.toArray(new String[configLocations.size()]));
076    
077                    BeanLocator beanLocator = new BeanLocatorImpl(
078                            ClassLoaderUtil.getPortalClassLoader(), applicationContext);
079    
080                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
081            }
082    
083    }