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