1   /**
2    * Copyright (c) 2000-2008 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.search.Indexer;
26  import com.liferay.portal.kernel.util.ServerDetector;
27  import com.liferay.portal.kernel.util.Time;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.service.PortletLocalServiceUtil;
30  import com.liferay.portal.util.PropsValues;
31  import com.liferay.portal.util.comparator.PortletLuceneComparator;
32  
33  import java.io.IOException;
34  
35  import java.util.Collections;
36  import java.util.List;
37  
38  import org.apache.commons.lang.time.StopWatch;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.apache.lucene.index.IndexWriter;
42  
43  /**
44   * <a href="LuceneIndexer.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class LuceneIndexer implements Runnable {
50  
51      public LuceneIndexer(long companyId) {
52          _companyId = companyId;
53      }
54  
55      public void halt() {
56      }
57  
58      public boolean isFinished() {
59          return _finished;
60      }
61  
62      public void run() {
63          reIndex();
64      }
65  
66      public void reIndex() {
67          if (PropsValues.INDEX_READ_ONLY) {
68              return;
69          }
70  
71          if (_log.isInfoEnabled()) {
72              _log.info("Reindexing Lucene started");
73          }
74  
75          if (ServerDetector.isOrion()) {
76  
77              // Wait 10 seconds because Orion 2.0.7 initiates LuceneServlet
78              // before it initiates MainServlet.
79  
80              try {
81                  Thread.sleep(Time.SECOND * 10);
82              }
83              catch (InterruptedException ie) {
84              }
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             Collections.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 = LogFactory.getLog(LuceneIndexer.class);
166 
167     private long _companyId;
168     private boolean _finished;
169 
170 }