001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.velocity;
016    
017    import com.liferay.portal.bean.BeanLocatorImpl;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    
023    /**
024     * @author Raymond Augé
025     */
026    public class UtilLocator {
027    
028            public static UtilLocator getInstance() {
029                    return _instance;
030            }
031    
032            private UtilLocator() {
033            }
034    
035            public Object findUtil(String utilName) {
036                    Object bean = null;
037    
038                    try {
039                            bean = PortalBeanLocatorUtil.locate(_getUtilName(utilName));
040                    }
041                    catch (Exception e) {
042                            _log.error(e, e);
043                    }
044    
045                    return bean;
046            }
047    
048            public Object findUtil(
049                    String servletContextName, String utilName) {
050    
051                    Object bean = null;
052    
053                    try {
054                            bean = PortletBeanLocatorUtil.locate(
055                                    servletContextName, _getUtilName(utilName));
056                    }
057                    catch (Exception e) {
058                            _log.error(e, e);
059                    }
060    
061                    return bean;
062            }
063    
064            private String _getUtilName(String utilName) {
065                    if (!utilName.endsWith(BeanLocatorImpl.VELOCITY_SUFFIX)) {
066                            utilName += BeanLocatorImpl.VELOCITY_SUFFIX;
067                    }
068    
069                    return utilName;
070            }
071    
072            private static Log _log = LogFactoryUtil.getLog(UtilLocator.class);
073    
074            private static UtilLocator _instance = new UtilLocator();
075    
076    }