1
22
23 package com.liferay.portal.search.lucene;
24
25 import com.liferay.portal.kernel.search.Indexer;
26 import com.liferay.portal.kernel.util.ServerDetector;
27 import com.liferay.portal.kernel.util.Time;
28 import com.liferay.portal.model.Portlet;
29 import com.liferay.portal.service.PortletLocalServiceUtil;
30 import com.liferay.portal.util.PropsValues;
31 import com.liferay.portal.util.comparator.PortletLuceneComparator;
32
33 import java.io.IOException;
34
35 import java.util.Collections;
36 import java.util.List;
37
38 import org.apache.commons.lang.time.StopWatch;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.lucene.index.IndexWriter;
42
43
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();
64 }
65
66 public void reIndex() {
67 if (PropsValues.INDEX_READ_ONLY) {
68 return;
69 }
70
71 if (_log.isInfoEnabled()) {
72 _log.info("Reindexing Lucene started");
73 }
74
75 if (ServerDetector.isOrion()) {
76
77
80 try {
81 Thread.sleep(Time.SECOND * 10);
82 }
83 catch (InterruptedException ie) {
84 }
85 }
86
87 StopWatch stopWatch1 = null;
88
89 if (_log.isInfoEnabled()) {
90 stopWatch1 = new StopWatch();
91
92 stopWatch1.start();
93 }
94
95 LuceneUtil.delete(_companyId);
96
97 try {
98 IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
99
100 LuceneUtil.write(writer);
101 }
102 catch (IOException ioe) {
103 _log.error(ioe.getMessage(), ioe);
104 }
105
106 String[] indexIds = new String[] {String.valueOf(_companyId)};
107
108 try {
109 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
110 _companyId);
111
112 Collections.sort(portlets, new PortletLuceneComparator());
113
114 for (Portlet portlet : portlets) {
115 if (!portlet.isActive()) {
116 continue;
117 }
118
119 Indexer indexer = portlet.getIndexerInstance();
120
121 if (indexer == null) {
122 continue;
123 }
124
125 String indexerClass = portlet.getIndexerClass();
126
127 StopWatch stopWatch2 = null;
128
129 if (_log.isInfoEnabled()) {
130 stopWatch2 = new StopWatch();
131
132 stopWatch2.start();
133 }
134
135 if (_log.isInfoEnabled()) {
136 _log.info("Reindexing with " + indexerClass + " started");
137 }
138
139 indexer.reIndex(indexIds);
140
141 if (_log.isInfoEnabled()) {
142 _log.info(
143 "Reindexing with " + indexerClass + " completed in " +
144 (stopWatch2.getTime() / Time.SECOND) + " seconds");
145 }
146 }
147
148 if (_log.isInfoEnabled()) {
149 _log.info(
150 "Reindexing Lucene completed in " +
151 (stopWatch1.getTime() / Time.SECOND) + " seconds");
152 }
153 }
154 catch (Exception e) {
155 _log.error("Error encountered while reindexing", e);
156
157 if (_log.isInfoEnabled()) {
158 _log.info("Reindexing Lucene failed");
159 }
160 }
161
162 _finished = true;
163 }
164
165 private static Log _log = LogFactory.getLog(LuceneIndexer.class);
166
167 private long _companyId;
168 private boolean _finished;
169
170 }