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  import java.util.HashMap;
21  import java.util.Map;
22  
23  /**
24   * <a href="PortletBeanLocatorUtil.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class PortletBeanLocatorUtil {
29  
30      public static BeanLocator getBeanLocator(String servletContextName) {
31          return _beanLocators.get(servletContextName);
32      }
33  
34      public static Object locate(String servletContextName, String name)
35          throws BeanLocatorException {
36  
37          BeanLocator beanLocator = getBeanLocator(servletContextName);
38  
39          if (beanLocator == null) {
40              _log.error(
41                  "BeanLocator is null for servlet context " +
42                      servletContextName);
43  
44              throw new BeanLocatorException(
45                  "BeanLocator has not been set for servlet context " +
46                      servletContextName);
47          }
48          else {
49              return beanLocator.locate(name);
50          }
51      }
52  
53      public static void setBeanLocator(
54          String servletContextName, BeanLocator beanLocator) {
55  
56          if (_log.isDebugEnabled()) {
57              if (beanLocator != null) {
58                  _log.debug(
59                      "Setting BeanLocator " + beanLocator.hashCode() +
60                          " for servlet context " + servletContextName);
61              }
62              else {
63                  _log.debug(
64                      "Removing BeanLocator for servlet context " +
65                          servletContextName);
66              }
67          }
68  
69          _beanLocators.put(servletContextName, beanLocator);
70      }
71  
72      private static Log _log = LogFactoryUtil.getLog(
73          PortletBeanLocatorUtil.class);
74  
75      private static Map<String, BeanLocator> _beanLocators =
76          new HashMap<String, BeanLocator>();
77  
78  }