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.kernel.search;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.dummy.DummyIndexer;
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.StringServiceRegistrationMap;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.List;
031    import java.util.Map;
032    import java.util.concurrent.ConcurrentHashMap;
033    
034    /**
035     * @author Raymond Aug??
036     */
037    public class IndexerRegistryUtil {
038    
039            public static Indexer getIndexer(Class<?> clazz) {
040                    return _instance._indexers.get(clazz.getName());
041            }
042    
043            public static Indexer getIndexer(String className) {
044                    return _instance._indexers.get(className);
045            }
046    
047            public static List<Indexer> getIndexers() {
048                    List<Indexer> indexers = new ArrayList<>(_instance._indexers.values());
049    
050                    return Collections.unmodifiableList(indexers);
051            }
052    
053            public static Indexer nullSafeGetIndexer(Class<?> clazz) {
054                    return _instance._nullSafeGetIndexer(clazz.getName());
055            }
056    
057            public static Indexer nullSafeGetIndexer(String className) {
058                    return _instance._nullSafeGetIndexer(className);
059            }
060    
061            public static void register(Indexer indexer) {
062                    _instance._register(indexer);
063            }
064    
065            public static void register(String className, Indexer indexer) {
066                    _instance._register(indexer);
067            }
068    
069            public static void unregister(Indexer indexer) {
070                    _instance._unregister(indexer);
071            }
072    
073            public static void unregister(String className) {
074                    _instance._unregister(className);
075            }
076    
077            private IndexerRegistryUtil() {
078                    Registry registry = RegistryUtil.getRegistry();
079    
080                    _serviceTracker = registry.trackServices(
081                            Indexer.class, new IndexerServiceTrackerCustomizer());
082    
083                    _serviceTracker.open();
084            }
085    
086            private Indexer _nullSafeGetIndexer(String className) {
087                    Indexer indexer = _indexers.get(className);
088    
089                    if (indexer != null) {
090                            return indexer;
091                    }
092    
093                    if (_log.isWarnEnabled()) {
094                            _log.warn("No indexer found for " + className);
095                    }
096    
097                    return _dummyIndexer;
098            }
099    
100            private void _register(Indexer indexer) {
101                    Registry registry = RegistryUtil.getRegistry();
102    
103                    ServiceRegistration<Indexer> serviceRegistration =
104                            registry.registerService(Indexer.class, indexer);
105    
106                    _serviceRegistrations.put(indexer.getClassName(), serviceRegistration);
107            }
108    
109            private void _unregister(Indexer indexer) {
110                    _unregister(indexer.getClassName());
111            }
112    
113            private void _unregister(String className) {
114                    ServiceRegistration<Indexer> serviceRegistration =
115                            _serviceRegistrations.remove(className);
116    
117                    if (serviceRegistration != null) {
118                            serviceRegistration.unregister();
119                    }
120            }
121    
122            private static final Log _log = LogFactoryUtil.getLog(
123                    IndexerRegistryUtil.class);
124    
125            private static final IndexerRegistryUtil _instance =
126                    new IndexerRegistryUtil();
127    
128            private static final Indexer _dummyIndexer = new DummyIndexer();
129    
130            private final Map<String, Indexer> _indexers = new ConcurrentHashMap<>();
131            private final StringServiceRegistrationMap<Indexer> _serviceRegistrations =
132                    new StringServiceRegistrationMap<>();
133            private final ServiceTracker<Indexer, Indexer> _serviceTracker;
134    
135            private class IndexerServiceTrackerCustomizer
136                    implements ServiceTrackerCustomizer<Indexer, Indexer> {
137    
138                    @Override
139                    public Indexer addingService(
140                            ServiceReference<Indexer> serviceReference) {
141    
142                            Registry registry = RegistryUtil.getRegistry();
143    
144                            Indexer indexer = registry.getService(serviceReference);
145    
146                            Class<?> clazz = indexer.getClass();
147    
148                            _indexers.put(clazz.getName(), indexer);
149    
150                            _indexers.put(indexer.getClassName(), indexer);
151    
152                            return indexer;
153                    }
154    
155                    @Override
156                    public void modifiedService(
157                            ServiceReference<Indexer> serviceReference, Indexer indexer) {
158                    }
159    
160                    @Override
161                    public void removedService(
162                            ServiceReference<Indexer> serviceReference, Indexer indexer) {
163    
164                            Registry registry = RegistryUtil.getRegistry();
165    
166                            registry.ungetService(serviceReference);
167    
168                            Class<?> clazz = indexer.getClass();
169    
170                            _indexers.remove(clazz.getName());
171    
172                            _indexers.remove(indexer.getClassName());
173                    }
174    
175            }
176    
177    }