1
19
20 package com.liferay.portal.search.lucene;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.search.Indexer;
25 import com.liferay.portal.kernel.util.ListUtil;
26 import com.liferay.portal.kernel.util.Time;
27 import com.liferay.portal.model.Portlet;
28 import com.liferay.portal.service.PortletLocalServiceUtil;
29 import com.liferay.portal.util.PropsValues;
30 import com.liferay.portal.util.comparator.PortletLuceneComparator;
31
32 import java.io.IOException;
33
34 import java.util.List;
35
36 import org.apache.commons.lang.time.StopWatch;
37 import org.apache.lucene.index.IndexWriter;
38
39
45 public class LuceneIndexer implements Runnable {
46
47 public LuceneIndexer(long companyId) {
48 _companyId = companyId;
49 }
50
51 public void halt() {
52 }
53
54 public boolean isFinished() {
55 return _finished;
56 }
57
58 public void run() {
59 reIndex(PropsValues.INDEX_ON_STARTUP_DELAY);
60 }
61
62 public void reIndex() {
63 reIndex(0);
64 }
65
66 public void reIndex(int delay) {
67 if (PropsValues.INDEX_READ_ONLY) {
68 return;
69 }
70
71 if (_log.isInfoEnabled()) {
72 _log.info("Reindexing Lucene started");
73 }
74
75 if (delay < 0) {
76 delay = 0;
77 }
78
79 try {
80 if (delay > 0) {
81 Thread.sleep(Time.SECOND * delay);
82 }
83 }
84 catch (InterruptedException ie) {
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 portlets = ListUtil.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 = LogFactoryUtil.getLog(LuceneIndexer.class);
166
167 private long _companyId;
168 private boolean _finished;
169
170 }