1   /**
2    * Copyright (c) 2000-2007 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 (_log.isInfoEnabled()) {
69              _log.info("Reindexing Lucene started");
70          }
71  
72          if (ServerDetector.isOrion()) {
73  
74              // Wait 10 seconds because Orion 2.0.7 initiates LuceneServlet
75              // before it initiates MainServlet.
76  
77              try {
78                  Thread.sleep(Time.SECOND * 10);
79              }
80              catch (InterruptedException ie) {
81              }
82          }
83  
84          StopWatch stopWatch1 = null;
85  
86          if (_log.isInfoEnabled()) {
87              stopWatch1 = new StopWatch();
88  
89              stopWatch1.start();
90          }
91  
92          LuceneUtil.delete(_companyId);
93  
94          try {
95              IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
96  
97              LuceneUtil.write(writer);
98          }
99          catch (IOException ioe) {
100             _log.error(ioe.getMessage());
101         }
102 
103         String[] indexIds = new String[] {String.valueOf(_companyId)};
104 
105         try {
106             List portlets = PortletLocalServiceUtil.getPortlets(_companyId);
107 
108             Collections.sort(portlets, new PortletLuceneComparator());
109 
110             Iterator itr = portlets.iterator();
111 
112             while (itr.hasNext()) {
113                 Portlet portlet = (Portlet)itr.next();
114 
115                 String className = portlet.getIndexerClass();
116 
117                 if (portlet.isActive() && className != null) {
118                     StopWatch stopWatch2 = null;
119 
120                     if (_log.isInfoEnabled()) {
121                         stopWatch2 = new StopWatch();
122 
123                         stopWatch2.start();
124                     }
125 
126                     if (_log.isInfoEnabled()) {
127                         _log.info("Reindexing with " + className + " started");
128                     }
129 
130                     Indexer indexer = (Indexer)InstancePool.get(className);
131 
132                     indexer.reIndex(indexIds);
133 
134                     if (_log.isInfoEnabled()) {
135                         _log.info(
136                             "Reindexing with " + className + " completed in " +
137                                 (stopWatch2.getTime() / Time.SECOND) +
138                                     " seconds");
139                     }
140                 }
141             }
142 
143             if (_log.isInfoEnabled()) {
144                 _log.info(
145                     "Reindexing Lucene completed in " +
146                         (stopWatch1.getTime() / Time.SECOND) + " seconds");
147             }
148         }
149         catch (Exception e) {
150             _log.error(e);
151 
152             if (_log.isInfoEnabled()) {
153                 _log.info("Reindexing Lucene failed");
154             }
155         }
156 
157         _finished = true;
158     }
159 
160     private static Log _log = LogFactory.getLog(LuceneIndexer.class);
161 
162     private long _companyId;
163     private boolean _finished;
164 
165 }