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.portlet.layoutsadmin.util;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceReference;
023    import com.liferay.registry.ServiceRegistration;
024    import com.liferay.registry.ServiceTracker;
025    import com.liferay.registry.ServiceTrackerCustomizer;
026    import com.liferay.registry.collections.ServiceRegistrationMap;
027    import com.liferay.registry.collections.ServiceRegistrationMapImpl;
028    
029    import java.util.Collection;
030    import java.util.List;
031    import java.util.Map;
032    import java.util.concurrent.ConcurrentHashMap;
033    
034    /**
035     * @author Eduardo Garcia
036     */
037    @ProviderType
038    public class SitemapURLProviderRegistryUtil {
039    
040            public static SitemapURLProvider getSitemapURLProvider(String className) {
041                    return _instance._getSitemapURLProvider(className);
042            }
043    
044            public static List<SitemapURLProvider> getSitemapURLProviders() {
045                    return _instance._getSitemapURLProviders();
046            }
047    
048            public static void register(SitemapURLProvider sitemapURLProvider) {
049                    _instance._register(sitemapURLProvider);
050            }
051    
052            public static void unregister(
053                    List<SitemapURLProvider> sitemapURLProviders) {
054    
055                    for (SitemapURLProvider sitemapURLProvider : sitemapURLProviders) {
056                            unregister(sitemapURLProvider);
057                    }
058            }
059    
060            public static void unregister(SitemapURLProvider sitemapURLProvider) {
061                    _instance._unregister(sitemapURLProvider);
062            }
063    
064            private SitemapURLProviderRegistryUtil() {
065                    Registry registry = RegistryUtil.getRegistry();
066    
067                    _serviceTracker = registry.trackServices(
068                            SitemapURLProvider.class,
069                            new SitemapURLProviderServiceTrackerCustomizer());
070    
071                    _serviceTracker.open();
072            }
073    
074            private SitemapURLProvider _getSitemapURLProvider(String className) {
075                    return _sitemapURLProviders.get(className);
076            }
077    
078            private List<SitemapURLProvider> _getSitemapURLProviders() {
079                    Collection<SitemapURLProvider> values = _sitemapURLProviders.values();
080    
081                    return ListUtil.fromCollection(values);
082            }
083    
084            private void _register(SitemapURLProvider sitemapURLProvider) {
085                    Registry registry = RegistryUtil.getRegistry();
086    
087                    ServiceRegistration<SitemapURLProvider> serviceRegistration =
088                            registry.registerService(
089                                    SitemapURLProvider.class, sitemapURLProvider);
090    
091                    _serviceRegistrations.put(sitemapURLProvider, serviceRegistration);
092            }
093    
094            private void _unregister(SitemapURLProvider sitemapURLProvider) {
095                    ServiceRegistration<SitemapURLProvider> serviceRegistration =
096                            _serviceRegistrations.remove(sitemapURLProvider);
097    
098                    if (serviceRegistration != null) {
099                            serviceRegistration.unregister();
100                    }
101            }
102    
103            private static final SitemapURLProviderRegistryUtil _instance =
104                    new SitemapURLProviderRegistryUtil();
105    
106            private final ServiceRegistrationMap<SitemapURLProvider>
107                    _serviceRegistrations = new ServiceRegistrationMapImpl<>();
108            private final
109                    ServiceTracker<SitemapURLProvider, SitemapURLProvider> _serviceTracker;
110            private final Map<String, SitemapURLProvider>
111                    _sitemapURLProviders = new ConcurrentHashMap<>();
112    
113            private class SitemapURLProviderServiceTrackerCustomizer
114                    implements ServiceTrackerCustomizer
115                            <SitemapURLProvider, SitemapURLProvider> {
116    
117                    @Override
118                    public SitemapURLProvider addingService(
119                            ServiceReference<SitemapURLProvider> serviceReference) {
120    
121                            Registry registry = RegistryUtil.getRegistry();
122    
123                            SitemapURLProvider sitemapURLProvider = registry.getService(
124                                    serviceReference);
125    
126                            _sitemapURLProviders.put(
127                                    sitemapURLProvider.getClassName(), sitemapURLProvider);
128    
129                            return sitemapURLProvider;
130                    }
131    
132                    @Override
133                    public void modifiedService(
134                            ServiceReference<SitemapURLProvider> serviceReference,
135                            SitemapURLProvider sitemapURLProvider) {
136                    }
137    
138                    @Override
139                    public void removedService(
140                            ServiceReference<SitemapURLProvider> serviceReference,
141                            SitemapURLProvider sitemapURLProvider) {
142    
143                            Registry registry = RegistryUtil.getRegistry();
144    
145                            registry.ungetService(serviceReference);
146    
147                            _sitemapURLProviders.remove(sitemapURLProvider.getClassName());
148                    }
149    
150            }
151    
152    }