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