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="UtilLocator.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Raymond Augé
27   */
28  public class UtilLocator {
29  
30      public static UtilLocator getInstance() {
31          return _instance;
32      }
33  
34      private UtilLocator() {
35      }
36  
37      public Object findUtil(String utilName) {
38          Object bean = null;
39  
40          try {
41              bean = PortalBeanLocatorUtil.locate(_getUtilName(utilName));
42          }
43          catch (Exception e) {
44              _log.error(e, e);
45          }
46  
47          return bean;
48      }
49  
50      public Object findUtil(
51          String servletContextName, String utilName) {
52  
53          Object bean = null;
54  
55          try {
56              bean = PortletBeanLocatorUtil.locate(
57                  servletContextName, _getUtilName(utilName));
58          }
59          catch (Exception e) {
60              _log.error(e, e);
61          }
62  
63          return bean;
64      }
65  
66      private String _getUtilName(String utilName) {
67          if (!utilName.endsWith(BeanLocatorImpl.VELOCITY_SUFFIX)) {
68              utilName += BeanLocatorImpl.VELOCITY_SUFFIX;
69          }
70  
71          return utilName;
72      }
73  
74      private static Log _log = LogFactoryUtil.getLog(UtilLocator.class);
75  
76      private static UtilLocator _instance = new UtilLocator();
77  
78  }