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.List;
030
031 import org.apache.commons.lang.time.StopWatch;
032
033
036 public class LuceneIndexer implements Runnable {
037
038 public LuceneIndexer(long companyId) {
039 _companyId = companyId;
040 }
041
042 public void halt() {
043 }
044
045 public boolean isFinished() {
046 return _finished;
047 }
048
049 public void run() {
050 reindex(PropsValues.INDEX_ON_STARTUP_DELAY);
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 protected void doReIndex(int delay) {
069 if (SearchEngineUtil.isIndexReadOnly()) {
070 return;
071 }
072
073 if (_log.isInfoEnabled()) {
074 _log.info("Reindexing Lucene started");
075 }
076
077 if (delay < 0) {
078 delay = 0;
079 }
080
081 try {
082 if (delay > 0) {
083 Thread.sleep(Time.SECOND * delay);
084 }
085 }
086 catch (InterruptedException ie) {
087 }
088
089 StopWatch stopWatch1 = null;
090
091 if (_log.isInfoEnabled()) {
092 stopWatch1 = new StopWatch();
093
094 stopWatch1.start();
095 }
096
097 try {
098 LuceneHelperUtil.delete(_companyId);
099
100 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
101 _companyId);
102
103 portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
104
105 for (Portlet portlet : portlets) {
106 if (!portlet.isActive()) {
107 continue;
108 }
109
110 Indexer indexer = portlet.getIndexerInstance();
111
112 if (indexer == null) {
113 continue;
114 }
115
116 String indexerClass = portlet.getIndexerClass();
117
118 StopWatch stopWatch2 = null;
119
120 if (_log.isInfoEnabled()) {
121 stopWatch2 = new StopWatch();
122
123 stopWatch2.start();
124 }
125
126 if (_log.isInfoEnabled()) {
127 _log.info("Reindexing with " + indexerClass + " started");
128 }
129
130 indexer.reindex(new String[] {String.valueOf(_companyId)});
131
132 if (_log.isInfoEnabled()) {
133 _log.info(
134 "Reindexing with " + indexerClass + " completed in " +
135 (stopWatch2.getTime() / Time.SECOND) + " seconds");
136 }
137 }
138
139 if (_log.isInfoEnabled()) {
140 _log.info(
141 "Reindexing Lucene completed in " +
142 (stopWatch1.getTime() / Time.SECOND) + " seconds");
143 }
144 }
145 catch (Exception e) {
146 _log.error("Error encountered while reindexing", e);
147
148 if (_log.isInfoEnabled()) {
149 _log.info("Reindexing Lucene failed");
150 }
151 }
152
153 _finished = true;
154 }
155
156 private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
157
158 private long _companyId;
159 private boolean _finished;
160
161 }