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 Brian Wing Shun Chan
025     */
026    public class ServiceLocator {
027    
028            public static ServiceLocator getInstance() {
029                    return _instance;
030            }
031    
032            private ServiceLocator() {
033            }
034    
035            public Object findService(String serviceName) {
036                    Object bean = null;
037    
038                    try {
039                            bean = PortalBeanLocatorUtil.locate(_getServiceName(serviceName));
040                    }
041                    catch (Exception e) {
042                            _log.error(e, e);
043                    }
044    
045                    return bean;
046            }
047    
048            public Object findService(String servletContextName, String serviceName) {
049                    Object bean = null;
050    
051                    try {
052                            bean = PortletBeanLocatorUtil.locate(
053                                    servletContextName, _getServiceName(serviceName));
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058    
059                    return bean;
060            }
061    
062            private String _getServiceName(String serviceName) {
063                    if (!serviceName.endsWith(BeanLocatorImpl.VELOCITY_SUFFIX)) {
064                            serviceName += BeanLocatorImpl.VELOCITY_SUFFIX;
065                    }
066    
067                    return serviceName;
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(ServiceLocator.class);
071    
072            private static ServiceLocator _instance = new ServiceLocator();
073    
074    }