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.kernel.bean;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class PortalBeanLocatorUtil {
024    
025            public static BeanLocator getBeanLocator() {
026                    return _beanLocator;
027            }
028    
029            public static Object locate(String name) throws BeanLocatorException {
030                    if (_beanLocator == null) {
031                            _log.error("BeanLocator is null");
032    
033                            throw new BeanLocatorException("BeanLocator has not been set");
034                    }
035                    else {
036                            Thread currentThread = Thread.currentThread();
037    
038                            ClassLoader contextClassLoader =
039                                    currentThread.getContextClassLoader();
040    
041                            ClassLoader beanClassLoader = _beanLocator.getClassLoader();
042    
043                            try {
044                                    if (contextClassLoader != beanClassLoader) {
045                                            currentThread.setContextClassLoader(beanClassLoader);
046                                    }
047    
048                                    return _beanLocator.locate(name);
049                            }
050                            finally {
051                                    if (contextClassLoader != beanClassLoader) {
052                                            currentThread.setContextClassLoader(contextClassLoader);
053                                    }
054                            }
055                    }
056            }
057    
058            public static void setBeanLocator(BeanLocator beanLocator) {
059                    if (_log.isDebugEnabled()) {
060                            _log.debug("Setting BeanLocator " + beanLocator.hashCode());
061                    }
062    
063                    _beanLocator = beanLocator;
064            }
065    
066            private static Log _log = LogFactoryUtil.getLog(
067                    PortalBeanLocatorUtil.class);
068    
069            private static BeanLocator _beanLocator;
070    
071    }