1
19
20 package com.liferay.portal.search.lucene;
21
22 import com.liferay.portal.dao.shard.ShardUtil;
23 import com.liferay.portal.kernel.log.Log;
24 import com.liferay.portal.kernel.log.LogFactoryUtil;
25 import com.liferay.portal.kernel.search.Indexer;
26 import com.liferay.portal.kernel.search.SearchEngineUtil;
27 import com.liferay.portal.kernel.util.ListUtil;
28 import com.liferay.portal.kernel.util.Time;
29 import com.liferay.portal.model.Portlet;
30 import com.liferay.portal.service.PortletLocalServiceUtil;
31 import com.liferay.portal.util.PropsValues;
32 import com.liferay.portal.util.comparator.PortletLuceneComparator;
33
34 import java.io.IOException;
35
36 import java.util.List;
37
38 import org.apache.commons.lang.time.StopWatch;
39 import org.apache.lucene.index.IndexWriter;
40
41
47 public class LuceneIndexer implements Runnable {
48
49 public LuceneIndexer(long companyId) {
50 _companyId = companyId;
51 }
52
53 public void halt() {
54 }
55
56 public boolean isFinished() {
57 return _finished;
58 }
59
60 public void run() {
61 reIndex(PropsValues.INDEX_ON_STARTUP_DELAY);
62 }
63
64 public void reIndex() {
65 reIndex(0);
66 }
67
68 public void reIndex(int delay) {
69 ShardUtil.pushCompanyService(_companyId);
70
71 try {
72 doReIndex(delay);
73 }
74 finally {
75 ShardUtil.popCompanyService();
76 }
77 }
78
79 protected void doReIndex(int delay) {
80 if (SearchEngineUtil.isIndexReadOnly()) {
81 return;
82 }
83
84 if (_log.isInfoEnabled()) {
85 _log.info("Reindexing Lucene started");
86 }
87
88 if (delay < 0) {
89 delay = 0;
90 }
91
92 try {
93 if (delay > 0) {
94 Thread.sleep(Time.SECOND * delay);
95 }
96 }
97 catch (InterruptedException ie) {
98 }
99
100 StopWatch stopWatch1 = null;
101
102 if (_log.isInfoEnabled()) {
103 stopWatch1 = new StopWatch();
104
105 stopWatch1.start();
106 }
107
108 LuceneUtil.delete(_companyId);
109
110 try {
111 IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
112
113 LuceneUtil.write(writer);
114 }
115 catch (IOException ioe) {
116 _log.error(ioe.getMessage(), ioe);
117 }
118
119 String[] indexIds = new String[] {String.valueOf(_companyId)};
120
121 try {
122 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
123 _companyId);
124
125 portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
126
127 for (Portlet portlet : portlets) {
128 if (!portlet.isActive()) {
129 continue;
130 }
131
132 Indexer indexer = portlet.getIndexerInstance();
133
134 if (indexer == null) {
135 continue;
136 }
137
138 String indexerClass = portlet.getIndexerClass();
139
140 StopWatch stopWatch2 = null;
141
142 if (_log.isInfoEnabled()) {
143 stopWatch2 = new StopWatch();
144
145 stopWatch2.start();
146 }
147
148 if (_log.isInfoEnabled()) {
149 _log.info("Reindexing with " + indexerClass + " started");
150 }
151
152 indexer.reIndex(indexIds);
153
154 if (_log.isInfoEnabled()) {
155 _log.info(
156 "Reindexing with " + indexerClass + " completed in " +
157 (stopWatch2.getTime() / Time.SECOND) + " seconds");
158 }
159 }
160
161 if (_log.isInfoEnabled()) {
162 _log.info(
163 "Reindexing Lucene completed in " +
164 (stopWatch1.getTime() / Time.SECOND) + " seconds");
165 }
166 }
167 catch (Exception e) {
168 _log.error("Error encountered while reindexing", e);
169
170 if (_log.isInfoEnabled()) {
171 _log.info("Reindexing Lucene failed");
172 }
173 }
174
175 _finished = true;
176 }
177
178 private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
179
180 private long _companyId;
181 private boolean _finished;
182
183 }