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.velocity;
16  
17  import com.liferay.portal.bean.BeanLocatorImpl;
18  import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
19  import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  
23  /**
24   * <a href="ServiceLocator.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class ServiceLocator {
29  
30      public static ServiceLocator getInstance() {
31          return _instance;
32      }
33  
34      private ServiceLocator() {
35      }
36  
37      public Object findService(String serviceName) {
38          Object bean = null;
39  
40          try {
41              bean = PortalBeanLocatorUtil.locate(_getServiceName(serviceName));
42          }
43          catch (Exception e) {
44              _log.error(e, e);
45          }
46  
47          return bean;
48      }
49  
50      public Object findService(String servletContextName, String serviceName) {
51          Object bean = null;
52  
53          try {
54              bean = PortletBeanLocatorUtil.locate(
55                  servletContextName, _getServiceName(serviceName));
56          }
57          catch (Exception e) {
58              _log.error(e, e);
59          }
60  
61          return bean;
62      }
63  
64      private String _getServiceName(String serviceName) {
65          if (!serviceName.endsWith(BeanLocatorImpl.VELOCITY_SUFFIX)) {
66              serviceName += BeanLocatorImpl.VELOCITY_SUFFIX;
67          }
68  
69          return serviceName;
70      }
71  
72      private static Log _log = LogFactoryUtil.getLog(ServiceLocator.class);
73  
74      private static ServiceLocator _instance = new ServiceLocator();
75  
76  }