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.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  }