1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Indexer;
28  import com.liferay.portal.kernel.util.ListUtil;
29  import com.liferay.portal.kernel.util.Time;
30  import com.liferay.portal.model.Portlet;
31  import com.liferay.portal.service.PortletLocalServiceUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.comparator.PortletLuceneComparator;
34  
35  import java.io.IOException;
36  
37  import java.util.List;
38  
39  import org.apache.commons.lang.time.StopWatch;
40  import org.apache.lucene.index.IndexWriter;
41  
42  /**
43   * <a href="LuceneIndexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class LuceneIndexer implements Runnable {
49  
50      public LuceneIndexer(long companyId) {
51          _companyId = companyId;
52      }
53  
54      public void halt() {
55      }
56  
57      public boolean isFinished() {
58          return _finished;
59      }
60  
61      public void run() {
62          reIndex(PropsValues.INDEX_ON_STARTUP_DELAY);
63      }
64  
65      public void reIndex() {
66          reIndex(0);
67      }
68  
69      public void reIndex(int delay) {
70          if (PropsValues.INDEX_READ_ONLY) {
71              return;
72          }
73  
74          if (_log.isInfoEnabled()) {
75              _log.info("Reindexing Lucene started");
76          }
77  
78          if (delay < 0) {
79              delay = 0;
80          }
81  
82          try {
83              if (delay > 0) {
84                  Thread.sleep(Time.SECOND * delay);
85              }
86          }
87          catch (InterruptedException ie) {
88          }
89  
90          StopWatch stopWatch1 = null;
91  
92          if (_log.isInfoEnabled()) {
93              stopWatch1 = new StopWatch();
94  
95              stopWatch1.start();
96          }
97  
98          LuceneUtil.delete(_companyId);
99  
100         try {
101             IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
102 
103             LuceneUtil.write(writer);
104         }
105         catch (IOException ioe) {
106             _log.error(ioe.getMessage(), ioe);
107         }
108 
109         String[] indexIds = new String[] {String.valueOf(_companyId)};
110 
111         try {
112             List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
113                 _companyId);
114 
115             portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
116 
117             for (Portlet portlet : portlets) {
118                 if (!portlet.isActive()) {
119                     continue;
120                 }
121 
122                 Indexer indexer = portlet.getIndexerInstance();
123 
124                 if (indexer == null) {
125                     continue;
126                 }
127 
128                 String indexerClass = portlet.getIndexerClass();
129 
130                 StopWatch stopWatch2 = null;
131 
132                 if (_log.isInfoEnabled()) {
133                     stopWatch2 = new StopWatch();
134 
135                     stopWatch2.start();
136                 }
137 
138                 if (_log.isInfoEnabled()) {
139                     _log.info("Reindexing with " + indexerClass + " started");
140                 }
141 
142                 indexer.reIndex(indexIds);
143 
144                 if (_log.isInfoEnabled()) {
145                     _log.info(
146                         "Reindexing with " + indexerClass + " completed in " +
147                             (stopWatch2.getTime() / Time.SECOND) + " seconds");
148                 }
149             }
150 
151             if (_log.isInfoEnabled()) {
152                 _log.info(
153                     "Reindexing Lucene completed in " +
154                         (stopWatch1.getTime() / Time.SECOND) + " seconds");
155             }
156         }
157         catch (Exception e) {
158             _log.error("Error encountered while reindexing", e);
159 
160             if (_log.isInfoEnabled()) {
161                 _log.info("Reindexing Lucene failed");
162             }
163         }
164 
165         _finished = true;
166     }
167 
168     private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
169 
170     private long _companyId;
171     private boolean _finished;
172 
173 }