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.kernel.bean;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.Map;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Miguel Pastor
025     */
026    public class PortalBeanLocatorUtil {
027    
028            public static BeanLocator getBeanLocator() {
029                    return _beanLocator;
030            }
031    
032            public static <T> Map<String, T> locate(Class<T> clazz) {
033                    if (_beanLocator == null) {
034                            _log.error("BeanLocator is null");
035    
036                            throw new BeanLocatorException("BeanLocator has not been set");
037                    }
038                    else {
039                            Thread currentThread = Thread.currentThread();
040    
041                            ClassLoader contextClassLoader =
042                                    currentThread.getContextClassLoader();
043    
044                            ClassLoader beanClassLoader = _beanLocator.getClassLoader();
045    
046                            try {
047                                    if (contextClassLoader != beanClassLoader) {
048                                            currentThread.setContextClassLoader(beanClassLoader);
049                                    }
050    
051                                    return _beanLocator.locate(clazz);
052                            }
053                            finally {
054                                    if (contextClassLoader != beanClassLoader) {
055                                            currentThread.setContextClassLoader(contextClassLoader);
056                                    }
057                            }
058                    }
059            }
060    
061            public static Object locate(String name) throws BeanLocatorException {
062                    if (_beanLocator == null) {
063                            _log.error("BeanLocator is null");
064    
065                            throw new BeanLocatorException("BeanLocator has not been set");
066                    }
067                    else {
068                            Thread currentThread = Thread.currentThread();
069    
070                            ClassLoader contextClassLoader =
071                                    currentThread.getContextClassLoader();
072    
073                            ClassLoader beanClassLoader = _beanLocator.getClassLoader();
074    
075                            try {
076                                    if (contextClassLoader != beanClassLoader) {
077                                            currentThread.setContextClassLoader(beanClassLoader);
078                                    }
079    
080                                    return _beanLocator.locate(name);
081                            }
082                            finally {
083                                    if (contextClassLoader != beanClassLoader) {
084                                            currentThread.setContextClassLoader(contextClassLoader);
085                                    }
086                            }
087                    }
088            }
089    
090            public static void setBeanLocator(BeanLocator beanLocator) {
091                    if (_log.isDebugEnabled()) {
092                            _log.debug("Setting BeanLocator " + beanLocator.hashCode());
093                    }
094    
095                    _beanLocator = beanLocator;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(
099                    PortalBeanLocatorUtil.class);
100    
101            private static BeanLocator _beanLocator;
102    
103    }