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