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 (_log.isInfoEnabled()) {
69 _log.info("Reindexing Lucene started");
70 }
71
72 if (ServerDetector.isOrion()) {
73
74
77 try {
78 Thread.sleep(Time.SECOND * 10);
79 }
80 catch (InterruptedException ie) {
81 }
82 }
83
84 StopWatch stopWatch1 = null;
85
86 if (_log.isInfoEnabled()) {
87 stopWatch1 = new StopWatch();
88
89 stopWatch1.start();
90 }
91
92 LuceneUtil.delete(_companyId);
93
94 try {
95 IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
96
97 LuceneUtil.write(writer);
98 }
99 catch (IOException ioe) {
100 _log.error(ioe.getMessage());
101 }
102
103 String[] indexIds = new String[] {String.valueOf(_companyId)};
104
105 try {
106 List portlets = PortletLocalServiceUtil.getPortlets(_companyId);
107
108 Collections.sort(portlets, new PortletLuceneComparator());
109
110 Iterator itr = portlets.iterator();
111
112 while (itr.hasNext()) {
113 Portlet portlet = (Portlet)itr.next();
114
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(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 }