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