001
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
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 = null;
098
099 if (_log.isInfoEnabled()) {
100 stopWatch = new StopWatch();
101
102 stopWatch.start();
103 }
104
105 try {
106 LuceneHelperUtil.delete(_companyId);
107
108 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
109 _companyId);
110
111 portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
112
113 for (Portlet portlet : portlets) {
114 if (!portlet.isActive()) {
115 continue;
116 }
117
118 List<Indexer> indexers = portlet.getIndexerInstances();
119
120 if (indexers == null) {
121 continue;
122 }
123
124 for (Indexer indexer : indexers) {
125 reindex(indexer);
126 }
127 }
128
129 if (_log.isInfoEnabled()) {
130 _log.info(
131 "Reindexing Lucene completed in " +
132 (stopWatch.getTime() / Time.SECOND) + " seconds");
133 }
134 }
135 catch (Exception e) {
136 _log.error("Error encountered while reindexing", e);
137
138 if (_log.isInfoEnabled()) {
139 _log.info("Reindexing Lucene failed");
140 }
141 }
142
143 _finished = true;
144 }
145
146 protected void reindex(Indexer indexer) throws Exception {
147 StopWatch stopWatch = null;
148
149 if (_log.isInfoEnabled()) {
150 stopWatch = new StopWatch();
151
152 stopWatch.start();
153 }
154
155 if (_log.isInfoEnabled()) {
156 _log.info("Reindexing with " + indexer.getClass() + " started");
157 }
158
159 indexer.reindex(new String[] {String.valueOf(_companyId)});
160
161 _usedSearchEngineIds.add(indexer.getSearchEngineId());
162
163 if (_log.isInfoEnabled()) {
164 _log.info(
165 "Reindexing with " + indexer.getClass() +
166 " completed in " + (stopWatch.getTime() / Time.SECOND) +
167 " seconds");
168 }
169 }
170
171 private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
172
173 private long _companyId;
174 private boolean _finished;
175 private Set<String> _usedSearchEngineIds;
176
177 }