001    /**
002     * Copyright (c) 2000-present 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.template;
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            public Object findUtil(String utilName) {
033                    Object bean = null;
034    
035                    try {
036                            bean = PortalBeanLocatorUtil.locate(_getUtilName(utilName));
037                    }
038                    catch (Exception e) {
039                            _log.error(e, e);
040                    }
041    
042                    return bean;
043            }
044    
045            public Object findUtil(String servletContextName, String utilName) {
046                    Object bean = null;
047    
048                    try {
049                            bean = PortletBeanLocatorUtil.locate(
050                                    servletContextName, _getUtilName(utilName));
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054                    }
055    
056                    return bean;
057            }
058    
059            private UtilLocator() {
060            }
061    
062            private String _getUtilName(String utilName) {
063                    if (!utilName.endsWith(BeanLocatorImpl.VELOCITY_SUFFIX)) {
064                            utilName += BeanLocatorImpl.VELOCITY_SUFFIX;
065                    }
066    
067                    return utilName;
068            }
069    
070            private static final Log _log = LogFactoryUtil.getLog(UtilLocator.class);
071    
072            private static final UtilLocator _instance = new UtilLocator();
073    
074    }