1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.search.lucene;
16  
17  import com.liferay.portal.kernel.dao.shard.ShardUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.search.Indexer;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.util.ListUtil;
23  import com.liferay.portal.kernel.util.Time;
24  import com.liferay.portal.model.Portlet;
25  import com.liferay.portal.service.PortletLocalServiceUtil;
26  import com.liferay.portal.util.PropsValues;
27  import com.liferay.portal.util.comparator.PortletLuceneComparator;
28  
29  import java.util.List;
30  
31  import org.apache.commons.lang.time.StopWatch;
32  
33  /**
34   * <a href="LuceneIndexer.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class LuceneIndexer implements Runnable {
39  
40      public LuceneIndexer(long companyId) {
41          _companyId = companyId;
42      }
43  
44      public void halt() {
45      }
46  
47      public boolean isFinished() {
48          return _finished;
49      }
50  
51      public void run() {
52          reIndex(PropsValues.INDEX_ON_STARTUP_DELAY);
53      }
54  
55      public void reIndex() {
56          reIndex(0);
57      }
58  
59      public void reIndex(int delay) {
60          ShardUtil.pushCompanyService(_companyId);
61  
62          try {
63              doReIndex(delay);
64          }
65          finally {
66              ShardUtil.popCompanyService();
67          }
68      }
69  
70      protected void doReIndex(int delay) {
71          if (SearchEngineUtil.isIndexReadOnly()) {
72              return;
73          }
74  
75          if (_log.isInfoEnabled()) {
76              _log.info("Reindexing Lucene started");
77          }
78  
79          if (delay < 0) {
80              delay = 0;
81          }
82  
83          try {
84              if (delay > 0) {
85                  Thread.sleep(Time.SECOND * delay);
86              }
87          }
88          catch (InterruptedException ie) {
89          }
90  
91          StopWatch stopWatch1 = null;
92  
93          if (_log.isInfoEnabled()) {
94              stopWatch1 = new StopWatch();
95  
96              stopWatch1.start();
97          }
98  
99          try {
100             LuceneHelperUtil.delete(_companyId);
101 
102             List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
103                 _companyId);
104 
105             portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
106 
107             for (Portlet portlet : portlets) {
108                 if (!portlet.isActive()) {
109                     continue;
110                 }
111 
112                 Indexer indexer = portlet.getIndexerInstance();
113 
114                 if (indexer == null) {
115                     continue;
116                 }
117 
118                 String indexerClass = portlet.getIndexerClass();
119 
120                 StopWatch stopWatch2 = null;
121 
122                 if (_log.isInfoEnabled()) {
123                     stopWatch2 = new StopWatch();
124 
125                     stopWatch2.start();
126                 }
127 
128                 if (_log.isInfoEnabled()) {
129                     _log.info("Reindexing with " + indexerClass + " started");
130                 }
131 
132                 indexer.reIndex(new String[] {String.valueOf(_companyId)});
133 
134                 if (_log.isInfoEnabled()) {
135                     _log.info(
136                         "Reindexing with " + indexerClass + " completed in " +
137                             (stopWatch2.getTime() / Time.SECOND) + " seconds");
138                 }
139             }
140 
141             if (_log.isInfoEnabled()) {
142                 _log.info(
143                     "Reindexing Lucene completed in " +
144                         (stopWatch1.getTime() / Time.SECOND) + " seconds");
145             }
146         }
147         catch (Exception e) {
148             _log.error("Error encountered while reindexing", e);
149 
150             if (_log.isInfoEnabled()) {
151                 _log.info("Reindexing Lucene failed");
152             }
153         }
154 
155         _finished = true;
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
159 
160     private long _companyId;
161     private boolean _finished;
162 
163 }