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.deploy.hot;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.IndexerPostProcessor;
021    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022    import com.liferay.registry.Filter;
023    import com.liferay.registry.Registry;
024    import com.liferay.registry.RegistryUtil;
025    import com.liferay.registry.ServiceReference;
026    import com.liferay.registry.ServiceTracker;
027    import com.liferay.registry.ServiceTrackerCustomizer;
028    import com.liferay.registry.util.StringPlus;
029    
030    import java.util.List;
031    
032    /**
033     * @author Raymond Aug??
034     */
035    public class IndexerPostProcessorRegistry {
036    
037            public IndexerPostProcessorRegistry() {
038                    Registry registry = RegistryUtil.getRegistry();
039    
040                    Filter indexerPostProcessorFilter = registry.getFilter(
041                            "(&(indexer.class.name=*)(objectClass=" +
042                                    IndexerPostProcessor.class.getName() + "))");
043    
044                    _serviceTracker = registry.trackServices(
045                            indexerPostProcessorFilter,
046                            new IndexerPostProcessorServiceTrackerCustomizer());
047    
048                    _serviceTracker.open();
049            }
050    
051            public void close() {
052                    _serviceTracker.close();
053            }
054    
055            private static final Log _log = LogFactoryUtil.getLog(
056                    IndexerPostProcessorRegistry.class);
057    
058            private final ServiceTracker<IndexerPostProcessor, IndexerPostProcessor>
059                    _serviceTracker;
060    
061            private class IndexerPostProcessorServiceTrackerCustomizer
062                    implements ServiceTrackerCustomizer
063                            <IndexerPostProcessor, IndexerPostProcessor> {
064    
065                    @Override
066                    public IndexerPostProcessor addingService(
067                            ServiceReference<IndexerPostProcessor> serviceReference) {
068    
069                            Registry registry = RegistryUtil.getRegistry();
070    
071                            IndexerPostProcessor indexerPostProcessor = registry.getService(
072                                    serviceReference);
073    
074                            List<String> indexerClassNames = StringPlus.asList(
075                                    serviceReference.getProperty("indexer.class.name"));
076    
077                            for (String indexerClassName : indexerClassNames) {
078                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
079                                            indexerClassName);
080    
081                                    if (indexer == null) {
082                                            _log.error(
083                                                    "No indexer for " + indexerClassName + " was found");
084    
085                                            continue;
086                                    }
087    
088                                    indexer.registerIndexerPostProcessor(indexerPostProcessor);
089                            }
090    
091                            return indexerPostProcessor;
092                    }
093    
094                    @Override
095                    public void modifiedService(
096                            ServiceReference<IndexerPostProcessor> serviceReference,
097                            IndexerPostProcessor indexerPostProcessor) {
098                    }
099    
100                    @Override
101                    public void removedService(
102                            ServiceReference<IndexerPostProcessor> serviceReference,
103                            IndexerPostProcessor indexerPostProcessor) {
104    
105                            Registry registry = RegistryUtil.getRegistry();
106    
107                            registry.ungetService(serviceReference);
108    
109                            List<String> indexerClassNames = StringPlus.asList(
110                                    serviceReference.getProperty("indexer.class.name"));
111    
112                            for (String indexerClassName : indexerClassNames ) {
113                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
114                                            indexerClassName);
115    
116                                    if (indexer == null) {
117                                            _log.error(
118                                                    "No indexer for " + indexerClassName + " was found");
119    
120                                            continue;
121                                    }
122    
123                                    indexer.unregisterIndexerPostProcessor(indexerPostProcessor);
124                            }
125                    }
126    
127            }
128    
129    }