001
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
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 }