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