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    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                            Thread.dumpStack();
073    
074                            if (_log.isDebugEnabled()) {
075                                    Exception e = new Exception();
076    
077                                    _log.debug(e, e);
078                            }
079    
080                            throw new BeanLocatorException("BeanLocator has not been set");
081                    }
082    
083                    Thread currentThread = Thread.currentThread();
084    
085                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
086    
087                    ClassLoader beanClassLoader = beanLocator.getClassLoader();
088    
089                    try {
090                            if (contextClassLoader != beanClassLoader) {
091                                    currentThread.setContextClassLoader(beanClassLoader);
092                            }
093    
094                            return beanLocator.locate(name);
095                    }
096                    finally {
097                            if (contextClassLoader != beanClassLoader) {
098                                    currentThread.setContextClassLoader(contextClassLoader);
099                            }
100                    }
101            }
102    
103            public static void reset() {
104                    setBeanLocator(null);
105            }
106    
107            public static void setBeanLocator(BeanLocator beanLocator) {
108                    PortalRuntimePermission.checkSetBeanProperty(
109                            PortalBeanLocatorUtil.class);
110    
111                    if (_log.isDebugEnabled()) {
112                            if (beanLocator == null) {
113                                    _log.debug("Setting BeanLocator " + beanLocator);
114                            }
115                            else {
116                                    _log.debug("Setting BeanLocator " + beanLocator.hashCode());
117                            }
118                    }
119    
120                    _beanLocator = beanLocator;
121            }
122    
123            private static Log _log = LogFactoryUtil.getLog(
124                    PortalBeanLocatorUtil.class);
125    
126            private static BeanLocator _beanLocator;
127    
128    }