001
014
015 package com.liferay.portal.search;
016
017 import com.liferay.portal.kernel.dao.shard.ShardUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.search.Indexer;
021 import com.liferay.portal.kernel.search.SearchEngineUtil;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.Time;
024 import com.liferay.portal.model.Portlet;
025 import com.liferay.portal.service.PortletLocalServiceUtil;
026 import com.liferay.portal.util.PropsValues;
027 import com.liferay.portal.util.comparator.PortletLuceneComparator;
028
029 import java.util.HashSet;
030 import java.util.List;
031 import java.util.Set;
032
033 import org.apache.commons.lang.time.StopWatch;
034
035
038 public class SearchEngineInitializer implements Runnable {
039
040 public SearchEngineInitializer(long companyId) {
041 _companyId = companyId;
042 _usedSearchEngineIds = new HashSet<String>();
043 }
044
045 public Set<String> getUsedSearchEngineIds() {
046 return _usedSearchEngineIds;
047 }
048
049 public void halt() {
050 }
051
052 public boolean isFinished() {
053 return _finished;
054 }
055
056 public void reindex() {
057 reindex(0);
058 }
059
060 public void reindex(int delay) {
061 ShardUtil.pushCompanyService(_companyId);
062
063 try {
064 doReIndex(delay);
065 }
066 finally {
067 ShardUtil.popCompanyService();
068 }
069 }
070
071 @Override
072 public void run() {
073 reindex(PropsValues.INDEX_ON_STARTUP_DELAY);
074 }
075
076 protected void doReIndex(int delay) {
077 if (SearchEngineUtil.isIndexReadOnly()) {
078 return;
079 }
080
081 if (_log.isInfoEnabled()) {
082 _log.info("Reindexing Lucene started");
083 }
084
085 if (delay < 0) {
086 delay = 0;
087 }
088
089 try {
090 if (delay > 0) {
091 Thread.sleep(Time.SECOND * delay);
092 }
093 }
094 catch (InterruptedException ie) {
095 }
096
097 StopWatch stopWatch = new StopWatch();
098
099 stopWatch.start();
100
101 try {
102 SearchEngineUtil.removeCompany(_companyId);
103
104 SearchEngineUtil.initialize(_companyId);
105
106 List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
107 _companyId);
108
109 portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
110
111 for (Portlet portlet : portlets) {
112 if (!portlet.isActive()) {
113 continue;
114 }
115
116 List<Indexer> indexers = portlet.getIndexerInstances();
117
118 if (indexers == null) {
119 continue;
120 }
121
122 Set<String> searchEngineIds = new HashSet<String>();
123
124 for (Indexer indexer : indexers) {
125 String searchEngineId = indexer.getSearchEngineId();
126
127 if (searchEngineIds.add(searchEngineId)) {
128 SearchEngineUtil.deletePortletDocuments(
129 searchEngineId, _companyId, portlet.getPortletId(),
130 true);
131 }
132
133 reindex(indexer);
134 }
135 }
136
137 if (_log.isInfoEnabled()) {
138 _log.info(
139 "Reindexing Lucene completed in " +
140 (stopWatch.getTime() / Time.SECOND) + " seconds");
141 }
142 }
143 catch (Exception e) {
144 _log.error("Error encountered while reindexing", e);
145
146 if (_log.isInfoEnabled()) {
147 _log.info("Reindexing Lucene failed");
148 }
149 }
150
151 _finished = true;
152 }
153
154 protected void reindex(Indexer indexer) throws Exception {
155 StopWatch stopWatch = new StopWatch();
156
157 stopWatch.start();
158
159 if (_log.isInfoEnabled()) {
160 _log.info("Reindexing with " + indexer.getClass() + " started");
161 }
162
163 indexer.reindex(new String[] {String.valueOf(_companyId)});
164
165 _usedSearchEngineIds.add(indexer.getSearchEngineId());
166
167 if (_log.isInfoEnabled()) {
168 _log.info(
169 "Reindexing with " + indexer.getClass() +
170 " completed in " + (stopWatch.getTime() / Time.SECOND) +
171 " seconds");
172 }
173 }
174
175 private static final Log _log = LogFactoryUtil.getLog(
176 SearchEngineInitializer.class);
177
178 private final long _companyId;
179 private boolean _finished;
180 private final Set<String> _usedSearchEngineIds;
181
182 }