001
014
015 package com.liferay.portal.kernel.module.framework.service;
016
017 import com.liferay.registry.Registry;
018 import com.liferay.registry.RegistryUtil;
019 import com.liferay.registry.ServiceReference;
020 import com.liferay.registry.ServiceTracker;
021 import com.liferay.registry.ServiceTrackerCustomizer;
022
023 import java.util.Map;
024 import java.util.concurrent.ConcurrentHashMap;
025
026
029 public class IdentifiableOSGiServiceUtil {
030
031 public static IdentifiableOSGiService getIdentifiableOSGiService(
032 String osgiServiceIdentifier) {
033
034 return _identifiableOSGiServices.get(osgiServiceIdentifier);
035 }
036
037 private IdentifiableOSGiServiceUtil() {
038 }
039
040 private static final Map<String, IdentifiableOSGiService>
041 _identifiableOSGiServices = new ConcurrentHashMap<>();
042 private static final
043 ServiceTracker<IdentifiableOSGiService, IdentifiableOSGiService>
044 _serviceTracker;
045
046 static {
047 Registry registry = RegistryUtil.getRegistry();
048
049 _serviceTracker = registry.trackServices(
050 IdentifiableOSGiService.class,
051 new IdentifiableOSGiServiceServiceTrackerCustomizer());
052
053 _serviceTracker.open();
054 }
055
056 private static class IdentifiableOSGiServiceServiceTrackerCustomizer
057 implements
058 ServiceTrackerCustomizer
059 <IdentifiableOSGiService, IdentifiableOSGiService> {
060
061 @Override
062 public IdentifiableOSGiService addingService(
063 ServiceReference<IdentifiableOSGiService> serviceReference) {
064
065 Registry registry = RegistryUtil.getRegistry();
066
067 IdentifiableOSGiService identifiableOSGiService =
068 registry.getService(serviceReference);
069
070 _identifiableOSGiServices.put(
071 identifiableOSGiService.getOSGiServiceIdentifier(),
072 identifiableOSGiService);
073
074 return identifiableOSGiService;
075 }
076
077 @Override
078 public void modifiedService(
079 ServiceReference<IdentifiableOSGiService> serviceReference,
080 IdentifiableOSGiService identifiableOSGiService) {
081
082 _identifiableOSGiServices.put(
083 identifiableOSGiService.getOSGiServiceIdentifier(),
084 identifiableOSGiService);
085 }
086
087 @Override
088 public void removedService(
089 ServiceReference<IdentifiableOSGiService> serviceReference,
090 IdentifiableOSGiService identifiableOSGiService) {
091
092 Registry registry = RegistryUtil.getRegistry();
093
094 registry.ungetService(serviceReference);
095
096 _identifiableOSGiServices.remove(
097 identifiableOSGiService.getOSGiServiceIdentifier());
098 }
099
100 }
101
102 }