1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.bean;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  /**
21   * <a href="PortalBeanLocatorUtil.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class PortalBeanLocatorUtil {
26  
27      public static BeanLocator getBeanLocator() {
28          return _beanLocator;
29      }
30  
31      public static Object locate(String name) throws BeanLocatorException {
32          if (_beanLocator == null) {
33              _log.error("BeanLocator is null");
34  
35              throw new BeanLocatorException("BeanLocator has not been set");
36          }
37          else {
38              Thread currentThread = Thread.currentThread();
39  
40              ClassLoader contextClassLoader =
41                  currentThread.getContextClassLoader();
42  
43              ClassLoader beanClassLoader = _beanLocator.getClassLoader();
44  
45              try {
46                  if (contextClassLoader != beanClassLoader) {
47                      currentThread.setContextClassLoader(beanClassLoader);
48                  }
49  
50                  return _beanLocator.locate(name);
51              }
52              finally {
53                  if (contextClassLoader != beanClassLoader) {
54                      currentThread.setContextClassLoader(contextClassLoader);
55                  }
56              }
57          }
58      }
59  
60      public static void setBeanLocator(BeanLocator beanLocator) {
61          if (_log.isDebugEnabled()) {
62              _log.debug("Setting BeanLocator " + beanLocator.hashCode());
63          }
64  
65          _beanLocator = beanLocator;
66      }
67  
68      private static Log _log = LogFactoryUtil.getLog(
69          PortalBeanLocatorUtil.class);
70  
71      private static BeanLocator _beanLocator;
72  
73  }