001    /**
002     * Copyright (c) 2000-2013 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 = _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) throws BeanLocatorException {
069                    BeanLocator beanLocator = getBeanLocator();
070    
071                    if (beanLocator == null) {
072                            _log.error("BeanLocator is null");
073    
074                            Thread.dumpStack();
075    
076                            if (_log.isDebugEnabled()) {
077                                    Exception e = new Exception();
078    
079                                    _log.debug(e, e);
080                            }
081    
082                            throw new BeanLocatorException("BeanLocator has not been set");
083                    }
084    
085                    Thread currentThread = Thread.currentThread();
086    
087                    ClassLoader contextClassLoader = _pacl.getContextClassLoader(
088                            currentThread);
089    
090                    ClassLoader beanClassLoader = _pacl.getBeanLocatorClassLoader(
091                            beanLocator);
092    
093                    try {
094                            if (contextClassLoader != beanClassLoader) {
095                                    _pacl.setContextClassLoader(currentThread, beanClassLoader);
096                            }
097    
098                            return beanLocator.locate(name);
099                    }
100                    finally {
101                            if (contextClassLoader != beanClassLoader) {
102                                    _pacl.setContextClassLoader(currentThread, contextClassLoader);
103                            }
104                    }
105            }
106    
107            public static void reset() {
108                    setBeanLocator(null);
109            }
110    
111            public static void setBeanLocator(BeanLocator beanLocator) {
112                    PortalRuntimePermission.checkSetBeanProperty(
113                            PortalBeanLocatorUtil.class);
114    
115                    if (_log.isDebugEnabled()) {
116                            if (beanLocator == null) {
117                                    _log.debug("Setting BeanLocator " + beanLocator);
118                            }
119                            else {
120                                    _log.debug("Setting BeanLocator " + beanLocator.hashCode());
121                            }
122                    }
123    
124                    _beanLocator = beanLocator;
125            }
126    
127            public static interface PACL {
128    
129                    public ClassLoader getBeanLocatorClassLoader(BeanLocator beanLocator);
130    
131                    public ClassLoader getContextClassLoader(Thread currentThread);
132    
133                    public void setContextClassLoader(
134                            Thread currentThread, ClassLoader classLoader);
135    
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(
139                    PortalBeanLocatorUtil.class);
140    
141            private static BeanLocator _beanLocator;
142            private static PACL _pacl = new NoPACL();
143    
144            private static class NoPACL implements PACL {
145    
146                    @Override
147                    public ClassLoader getBeanLocatorClassLoader(BeanLocator beanLocator) {
148                            return beanLocator.getClassLoader();
149                    }
150    
151                    @Override
152                    public ClassLoader getContextClassLoader(Thread currentThread) {
153                            return currentThread.getContextClassLoader();
154                    }
155    
156                    @Override
157                    public void setContextClassLoader(
158                            Thread currentThread, ClassLoader classLoader) {
159    
160                            currentThread.setContextClassLoader(classLoader);
161                    }
162    
163            }
164    
165    }