001    /**
002     * Copyright (c) 2000-2010 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    /**
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    }