1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }