001    /**
002     * Copyright (c) 2000-2013 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 Brian Wing Shun Chan
025     */
026    public class ServiceLocator {
027    
028            public static ServiceLocator getInstance() {
029                    return _instance;
030            }
031    
032            public Object findService(String serviceName) {
033                    Object bean = null;
034    
035                    try {
036                            bean = PortalBeanLocatorUtil.locate(_getServiceName(serviceName));
037                    }
038                    catch (Exception e) {
039                            _log.error(e, e);
040                    }
041    
042                    return bean;
043            }
044    
045            public Object findService(String servletContextName, String serviceName) {
046                    Object bean = null;
047    
048                    try {
049                            bean = PortletBeanLocatorUtil.locate(
050                                    servletContextName, _getServiceName(serviceName));
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054                    }
055    
056                    return bean;
057            }
058    
059            private ServiceLocator() {
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    }