001    /**
002     * Copyright (c) 2000-present 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 is not set");
044                    }
045    
046                    Thread currentThread = Thread.currentThread();
047    
048                    ClassLoader contextClassLoader = _pacl.getContextClassLoader(
049                            currentThread);
050    
051                    ClassLoader beanClassLoader = _pacl.getBeanLocatorClassLoader(
052                            beanLocator);
053    
054                    try {
055                            if (contextClassLoader != beanClassLoader) {
056                                    _pacl.setContextClassLoader(currentThread, beanClassLoader);
057                            }
058    
059                            return beanLocator.locate(clazz);
060                    }
061                    finally {
062                            if (contextClassLoader != beanClassLoader) {
063                                    _pacl.setContextClassLoader(currentThread, contextClassLoader);
064                            }
065                    }
066            }
067    
068            public static Object locate(String name) {
069                    BeanLocator beanLocator = getBeanLocator();
070    
071                    if (beanLocator == null) {
072                            _log.error("BeanLocator is null");
073    
074                            throw new BeanLocatorException("BeanLocator is not set");
075                    }
076    
077                    Thread currentThread = Thread.currentThread();
078    
079                    ClassLoader contextClassLoader = _pacl.getContextClassLoader(
080                            currentThread);
081    
082                    ClassLoader beanClassLoader = _pacl.getBeanLocatorClassLoader(
083                            beanLocator);
084    
085                    try {
086                            if (contextClassLoader != beanClassLoader) {
087                                    _pacl.setContextClassLoader(currentThread, beanClassLoader);
088                            }
089    
090                            return beanLocator.locate(name);
091                    }
092                    finally {
093                            if (contextClassLoader != beanClassLoader) {
094                                    _pacl.setContextClassLoader(currentThread, contextClassLoader);
095                            }
096                    }
097            }
098    
099            public static void reset() {
100                    setBeanLocator(null);
101            }
102    
103            public static void setBeanLocator(BeanLocator beanLocator) {
104                    PortalRuntimePermission.checkSetBeanProperty(
105                            PortalBeanLocatorUtil.class);
106    
107                    if (_log.isDebugEnabled()) {
108                            if (beanLocator == null) {
109                                    _log.debug("Setting BeanLocator " + beanLocator);
110                            }
111                            else {
112                                    _log.debug("Setting BeanLocator " + beanLocator.hashCode());
113                            }
114                    }
115    
116                    _beanLocator = beanLocator;
117            }
118    
119            public interface PACL {
120    
121                    public ClassLoader getBeanLocatorClassLoader(BeanLocator beanLocator);
122    
123                    public ClassLoader getContextClassLoader(Thread currentThread);
124    
125                    public void setContextClassLoader(
126                            Thread currentThread, ClassLoader classLoader);
127    
128            }
129    
130            private static final Log _log = LogFactoryUtil.getLog(
131                    PortalBeanLocatorUtil.class);
132    
133            private static BeanLocator _beanLocator;
134            private static final PACL _pacl = new NoPACL();
135    
136            private static class NoPACL implements PACL {
137    
138                    @Override
139                    public ClassLoader getBeanLocatorClassLoader(BeanLocator beanLocator) {
140                            return beanLocator.getClassLoader();
141                    }
142    
143                    @Override
144                    public ClassLoader getContextClassLoader(Thread currentThread) {
145                            return currentThread.getContextClassLoader();
146                    }
147    
148                    @Override
149                    public void setContextClassLoader(
150                            Thread currentThread, ClassLoader classLoader) {
151    
152                            currentThread.setContextClassLoader(classLoader);
153                    }
154    
155            }
156    
157    }