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 aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.bean.BeanLocatorImpl;
020    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
021    import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.registry.Registry;
025    import com.liferay.registry.RegistryUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    @ProviderType
031    public class ServiceLocator {
032    
033            public static ServiceLocator getInstance() {
034                    return _instance;
035            }
036    
037            public Object findService(String serviceName) {
038                    Object bean = null;
039    
040                    try {
041                            Registry registry = RegistryUtil.getRegistry();
042    
043                            bean = registry.getService(serviceName);
044    
045                            if (bean == null) {
046                                    bean = PortalBeanLocatorUtil.locate(
047                                            _getServiceName(serviceName));
048                            }
049                    }
050                    catch (Exception e) {
051                            _log.error(e, e);
052                    }
053    
054                    return bean;
055            }
056    
057            public Object findService(String servletContextName, String serviceName) {
058                    Object bean = null;
059    
060                    try {
061                            bean = PortletBeanLocatorUtil.locate(
062                                    servletContextName, _getServiceName(serviceName));
063                    }
064                    catch (Exception e) {
065                            _log.error(e, e);
066                    }
067    
068                    return bean;
069            }
070    
071            private ServiceLocator() {
072            }
073    
074            private String _getServiceName(String serviceName) {
075                    if (!serviceName.endsWith(BeanLocatorImpl.VELOCITY_SUFFIX)) {
076                            serviceName += BeanLocatorImpl.VELOCITY_SUFFIX;
077                    }
078    
079                    return serviceName;
080            }
081    
082            private static final Log _log = LogFactoryUtil.getLog(ServiceLocator.class);
083    
084            private static final ServiceLocator _instance = new ServiceLocator();
085    
086    }