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.search;
016    
017    import com.liferay.portal.kernel.dao.shard.ShardUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.search.Indexer;
021    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022    import com.liferay.portal.kernel.search.SearchEngineUtil;
023    import com.liferay.portal.kernel.util.Time;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.util.HashSet;
027    import java.util.List;
028    import java.util.Set;
029    
030    import org.apache.commons.lang.time.StopWatch;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SearchEngineInitializer implements Runnable {
036    
037            public SearchEngineInitializer(long companyId) {
038                    _companyId = companyId;
039                    _usedSearchEngineIds = new HashSet<>();
040            }
041    
042            public Set<String> getUsedSearchEngineIds() {
043                    return _usedSearchEngineIds;
044            }
045    
046            public void halt() {
047            }
048    
049            public boolean isFinished() {
050                    return _finished;
051            }
052    
053            public void reindex() {
054                    reindex(0);
055            }
056    
057            public void reindex(int delay) {
058                    ShardUtil.pushCompanyService(_companyId);
059    
060                    try {
061                            doReIndex(delay);
062                    }
063                    finally {
064                            ShardUtil.popCompanyService();
065                    }
066            }
067    
068            @Override
069            public void run() {
070                    reindex(PropsValues.INDEX_ON_STARTUP_DELAY);
071            }
072    
073            protected void doReIndex(int delay) {
074                    if (SearchEngineUtil.isIndexReadOnly()) {
075                            return;
076                    }
077    
078                    if (_log.isInfoEnabled()) {
079                            _log.info("Reindexing Lucene started");
080                    }
081    
082                    if (delay < 0) {
083                            delay = 0;
084                    }
085    
086                    try {
087                            if (delay > 0) {
088                                    Thread.sleep(Time.SECOND * delay);
089                            }
090                    }
091                    catch (InterruptedException ie) {
092                    }
093    
094                    StopWatch stopWatch = new StopWatch();
095    
096                    stopWatch.start();
097    
098                    try {
099                            SearchEngineUtil.removeCompany(_companyId);
100    
101                            SearchEngineUtil.initialize(_companyId);
102    
103                            List<Indexer> indexers = IndexerRegistryUtil.getIndexers();
104    
105                            Set<String> searchEngineIds = new HashSet<>();
106    
107                            for (Indexer indexer : indexers) {
108                                    String searchEngineId = indexer.getSearchEngineId();
109    
110                                    if (searchEngineIds.add(searchEngineId)) {
111                                            SearchEngineUtil.deleteEntityDocuments(
112                                                    searchEngineId, _companyId, indexer.getClassName(),
113                                                    true);
114                                    }
115    
116                                    reindex(indexer);
117                            }
118    
119                            if (_log.isInfoEnabled()) {
120                                    _log.info(
121                                            "Reindexing Lucene completed in " +
122                                                    (stopWatch.getTime() / Time.SECOND) + " seconds");
123                            }
124                    }
125                    catch (Exception e) {
126                            _log.error("Error encountered while reindexing", e);
127    
128                            if (_log.isInfoEnabled()) {
129                                    _log.info("Reindexing Lucene failed");
130                            }
131                    }
132    
133                    _finished = true;
134            }
135    
136            protected void reindex(Indexer indexer) throws Exception {
137                    StopWatch stopWatch = new StopWatch();
138    
139                    stopWatch.start();
140    
141                    if (_log.isInfoEnabled()) {
142                            _log.info("Reindexing with " + indexer.getClass() + " started");
143                    }
144    
145                    indexer.reindex(new String[] {String.valueOf(_companyId)});
146    
147                    _usedSearchEngineIds.add(indexer.getSearchEngineId());
148    
149                    if (_log.isInfoEnabled()) {
150                            _log.info(
151                                    "Reindexing with " + indexer.getClass() +
152                                            " completed in " + (stopWatch.getTime() / Time.SECOND) +
153                                                    " seconds");
154                    }
155            }
156    
157            private static final Log _log = LogFactoryUtil.getLog(
158                    SearchEngineInitializer.class);
159    
160            private final long _companyId;
161            private boolean _finished;
162            private final Set<String> _usedSearchEngineIds;
163    
164    }