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    
029    /**
030     * @author Raymond Aug??
031     */
032    public class IndexerPostProcessorRegistry {
033    
034            public IndexerPostProcessorRegistry() {
035                    Registry registry = RegistryUtil.getRegistry();
036    
037                    Filter indexerPostProcessorFilter = registry.getFilter(
038                            "(&(indexer.class.name=*)(objectClass=" +
039                                    IndexerPostProcessor.class.getName() + "))");
040    
041                    _serviceTracker = registry.trackServices(
042                            indexerPostProcessorFilter,
043                            new IndexerPostProcessorServiceTrackerCustomizer());
044    
045                    _serviceTracker.open();
046            }
047    
048            public void close() {
049                    _serviceTracker.close();
050            }
051    
052            private static final Log _log = LogFactoryUtil.getLog(
053                    IndexerPostProcessorRegistry.class);
054    
055            private final ServiceTracker<IndexerPostProcessor, IndexerPostProcessor>
056                    _serviceTracker;
057    
058            private class IndexerPostProcessorServiceTrackerCustomizer
059                    implements ServiceTrackerCustomizer
060                            <IndexerPostProcessor, IndexerPostProcessor> {
061    
062                    @Override
063                    public IndexerPostProcessor addingService(
064                            ServiceReference<IndexerPostProcessor> serviceReference) {
065    
066                            Registry registry = RegistryUtil.getRegistry();
067    
068                            IndexerPostProcessor indexerPostProcessor = registry.getService(
069                                    serviceReference);
070    
071                            String indexerClassName = (String)serviceReference.getProperty(
072                                    "indexer.class.name");
073    
074                            Indexer indexer = IndexerRegistryUtil.getIndexer(indexerClassName);
075    
076                            if (indexer == null) {
077                                    _log.error("No indexer for " + indexerClassName + " was found");
078    
079                                    return null;
080                            }
081    
082                            indexer.registerIndexerPostProcessor(indexerPostProcessor);
083    
084                            return indexerPostProcessor;
085                    }
086    
087                    @Override
088                    public void modifiedService(
089                            ServiceReference<IndexerPostProcessor> serviceReference,
090                            IndexerPostProcessor indexerPostProcessor) {
091                    }
092    
093                    @Override
094                    public void removedService(
095                            ServiceReference<IndexerPostProcessor> serviceReference,
096                            IndexerPostProcessor indexerPostProcessor) {
097    
098                            Registry registry = RegistryUtil.getRegistry();
099    
100                            registry.ungetService(serviceReference);
101    
102                            String indexerClassName = (String)serviceReference.getProperty(
103                                    "indexer.class.name");
104    
105                            Indexer indexer = IndexerRegistryUtil.getIndexer(indexerClassName);
106    
107                            indexer.unregisterIndexerPostProcessor(indexerPostProcessor);
108                    }
109    
110            }
111    
112    }