001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }