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