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.lucene;
24  
25  import com.liferay.portal.kernel.search.Indexer;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.ServerDetector;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.service.PortletLocalServiceUtil;
30  import com.liferay.portal.util.comparator.PortletLuceneComparator;
31  import com.liferay.util.Time;
32  
33  import java.io.IOException;
34  
35  import java.util.Collections;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  import org.apache.commons.lang.time.StopWatch;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.apache.lucene.index.IndexWriter;
43  
44  /**
45   * <a href="LuceneIndexer.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class LuceneIndexer implements Runnable {
51  
52      public LuceneIndexer(long companyId) {
53          _companyId = companyId;
54      }
55  
56      public void halt() {
57      }
58  
59      public boolean isFinished() {
60          return _finished;
61      }
62  
63      public void run() {
64          reIndex();
65      }
66  
67      public void reIndex() {
68          if (LuceneUtil.INDEX_READ_ONLY) {
69              return;
70          }
71  
72          if (_log.isInfoEnabled()) {
73              _log.info("Reindexing Lucene started");
74          }
75  
76          if (ServerDetector.isOrion()) {
77  
78              // Wait 10 seconds because Orion 2.0.7 initiates LuceneServlet
79              // before it initiates MainServlet.
80  
81              try {
82                  Thread.sleep(Time.SECOND * 10);
83              }
84              catch (InterruptedException ie) {
85              }
86          }
87  
88          StopWatch stopWatch1 = null;
89  
90          if (_log.isInfoEnabled()) {
91              stopWatch1 = new StopWatch();
92  
93              stopWatch1.start();
94          }
95  
96          LuceneUtil.delete(_companyId);
97  
98          try {
99              IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
100 
101             LuceneUtil.write(writer);
102         }
103         catch (IOException ioe) {
104             _log.error(ioe.getMessage(), ioe);
105         }
106 
107         String[] indexIds = new String[] {String.valueOf(_companyId)};
108 
109         try {
110             List portlets = PortletLocalServiceUtil.getPortlets(_companyId);
111 
112             Collections.sort(portlets, new PortletLuceneComparator());
113 
114             Iterator itr = portlets.iterator();
115 
116             while (itr.hasNext()) {
117                 Portlet portlet = (Portlet)itr.next();
118 
119                 String className = portlet.getIndexerClass();
120 
121                 if (portlet.isActive() && className != null) {
122                     StopWatch stopWatch2 = null;
123 
124                     if (_log.isInfoEnabled()) {
125                         stopWatch2 = new StopWatch();
126 
127                         stopWatch2.start();
128                     }
129 
130                     if (_log.isInfoEnabled()) {
131                         _log.info("Reindexing with " + className + " started");
132                     }
133 
134                     Indexer indexer = (Indexer)InstancePool.get(className);
135 
136                     indexer.reIndex(indexIds);
137 
138                     if (_log.isInfoEnabled()) {
139                         _log.info(
140                             "Reindexing with " + className + " completed in " +
141                                 (stopWatch2.getTime() / Time.SECOND) +
142                                     " seconds");
143                     }
144                 }
145             }
146 
147             if (_log.isInfoEnabled()) {
148                 _log.info(
149                     "Reindexing Lucene completed in " +
150                         (stopWatch1.getTime() / Time.SECOND) + " seconds");
151             }
152         }
153         catch (Exception e) {
154             _log.error("Error encountered while reindexing", e);
155 
156             if (_log.isInfoEnabled()) {
157                 _log.info("Reindexing Lucene failed");
158             }
159         }
160 
161         _finished = true;
162     }
163 
164     private static Log _log = LogFactory.getLog(LuceneIndexer.class);
165 
166     private long _companyId;
167     private boolean _finished;
168 
169 }