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            public void run() {
072                    reindex(PropsValues.INDEX_ON_STARTUP_DELAY);
073            }
074    
075            protected void doReIndex(int delay) {
076                    if (SearchEngineUtil.isIndexReadOnly()) {
077                            return;
078                    }
079    
080                    if (_log.isInfoEnabled()) {
081                            _log.info("Reindexing Lucene started");
082                    }
083    
084                    if (delay < 0) {
085                            delay = 0;
086                    }
087    
088                    try {
089                            if (delay > 0) {
090                                    Thread.sleep(Time.SECOND * delay);
091                            }
092                    }
093                    catch (InterruptedException ie) {
094                    }
095    
096                    StopWatch stopWatch = null;
097    
098                    if (_log.isInfoEnabled()) {
099                            stopWatch = new StopWatch();
100    
101                            stopWatch.start();
102                    }
103    
104                    try {
105                            LuceneHelperUtil.delete(_companyId);
106    
107                            List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
108                                    _companyId);
109    
110                            portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
111    
112                            for (Portlet portlet : portlets) {
113                                    if (!portlet.isActive()) {
114                                            continue;
115                                    }
116    
117                                    List<Indexer> indexers = portlet.getIndexerInstances();
118    
119                                    if (indexers == null) {
120                                            continue;
121                                    }
122    
123                                    for (Indexer indexer : indexers) {
124                                            reindex(indexer);
125                                    }
126                            }
127    
128                            if (_log.isInfoEnabled()) {
129                                    _log.info(
130                                            "Reindexing Lucene completed in " +
131                                                    (stopWatch.getTime() / Time.SECOND) + " seconds");
132                            }
133                    }
134                    catch (Exception e) {
135                            _log.error("Error encountered while reindexing", e);
136    
137                            if (_log.isInfoEnabled()) {
138                                    _log.info("Reindexing Lucene failed");
139                            }
140                    }
141    
142                    _finished = true;
143            }
144    
145            protected void reindex(Indexer indexer) throws Exception {
146                    StopWatch stopWatch = null;
147    
148                    if (_log.isInfoEnabled()) {
149                            stopWatch = new StopWatch();
150    
151                            stopWatch.start();
152                    }
153    
154                    if (_log.isInfoEnabled()) {
155                            _log.info("Reindexing with " + indexer.getClass() + " started");
156                    }
157    
158                    indexer.reindex(new String[] {String.valueOf(_companyId)});
159    
160                    _usedSearchEngineIds.add(indexer.getSearchEngineId());
161    
162                    if (_log.isInfoEnabled()) {
163                            _log.info(
164                                    "Reindexing with " + indexer.getClass() +
165                                            " completed in " + (stopWatch.getTime() / Time.SECOND) +
166                                                    " seconds");
167                    }
168            }
169    
170            private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
171    
172            private long _companyId;
173            private boolean _finished;
174            private Set<String> _usedSearchEngineIds;
175    
176    }