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