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