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