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.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 (LuceneUtil.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 String className = portlet.getIndexerClass();
116
117 if (portlet.isActive() && className != null) {
118 StopWatch stopWatch2 = null;
119
120 if (_log.isInfoEnabled()) {
121 stopWatch2 = new StopWatch();
122
123 stopWatch2.start();
124 }
125
126 if (_log.isInfoEnabled()) {
127 _log.info("Reindexing with " + className + " started");
128 }
129
130 Indexer indexer = (Indexer)InstancePool.get(className);
131
132 indexer.reIndex(indexIds);
133
134 if (_log.isInfoEnabled()) {
135 _log.info(
136 "Reindexing with " + className + " completed in " +
137 (stopWatch2.getTime() / Time.SECOND) +
138 " seconds");
139 }
140 }
141 }
142
143 if (_log.isInfoEnabled()) {
144 _log.info(
145 "Reindexing Lucene completed in " +
146 (stopWatch1.getTime() / Time.SECOND) + " seconds");
147 }
148 }
149 catch (Exception e) {
150 _log.error("Error encountered while reindexing", e);
151
152 if (_log.isInfoEnabled()) {
153 _log.info("Reindexing Lucene failed");
154 }
155 }
156
157 _finished = true;
158 }
159
160 private static Log _log = LogFactory.getLog(LuceneIndexer.class);
161
162 private long _companyId;
163 private boolean _finished;
164
165 }