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.servlet;
24  
25  import com.liferay.portal.job.JobScheduler;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ObjectValuePair;
28  import com.liferay.portal.kernel.util.ServerDetector;
29  import com.liferay.portal.lucene.CleanUpJob;
30  import com.liferay.portal.lucene.LuceneIndexer;
31  import com.liferay.portal.lucene.LuceneUtil;
32  import com.liferay.portal.util.PortalInstances;
33  import com.liferay.portal.util.PropsUtil;
34  import com.liferay.portal.util.PropsValues;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import javax.servlet.ServletConfig;
40  import javax.servlet.ServletException;
41  import javax.servlet.http.HttpServlet;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  import org.quartz.SchedulerException;
47  
48  /**
49   * <a href="LuceneServlet.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Jorge Ferrer
53   *
54   */
55  public class LuceneServlet extends HttpServlet {
56  
57      public void init(ServletConfig config) throws ServletException {
58          super.init(config);
59  
60          long[] companyIds = PortalInstances.getCompanyIds();
61  
62          for (int i = 0; i < companyIds.length; i++) {
63              long companyId = companyIds[i];
64  
65              if (GetterUtil.getBoolean(
66                      PropsUtil.get(PropsUtil.INDEX_ON_STARTUP))) {
67  
68                  if (_log.isInfoEnabled()) {
69                      _log.info("Indexing Lucene on startup");
70                  }
71  
72                  LuceneIndexer indexer = new LuceneIndexer(companyId);
73                  Thread indexerThread = null;
74  
75                  if (GetterUtil.getBoolean(
76                          PropsUtil.get(PropsUtil.INDEX_WITH_THREAD)) ||
77                      ServerDetector.isOrion()) {
78  
79                      indexerThread = new Thread(
80                          indexer, THREAD_NAME + "." + companyId);
81  
82                      indexerThread.setPriority(THREAD_PRIORITY);
83  
84                      indexerThread.start();
85                  }
86                  else {
87                      indexer.reIndex();
88                  }
89  
90                  _indexers.add(new ObjectValuePair(indexer, indexerThread));
91              }
92              else {
93                  LuceneUtil.checkLuceneDir(companyId);
94              }
95  
96              if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
97                  try {
98                      JobScheduler.schedule(new CleanUpJob());
99                  }
100                 catch (SchedulerException se) {
101                     _log.error(se);
102                 }
103             }
104         }
105     }
106 
107     public void destroy() {
108 
109         // Wait for indexer to be gracefully interrupted
110 
111         for (int i = 0; i < _indexers.size(); i++) {
112             ObjectValuePair ovp = (ObjectValuePair)_indexers.get(i);
113 
114             LuceneIndexer indexer = (LuceneIndexer)ovp.getKey();
115             Thread indexerThread = (Thread)ovp.getValue();
116 
117             if ((indexer != null) && (!indexer.isFinished()) &&
118                 (indexerThread != null)) {
119 
120                 if (_log.isWarnEnabled()) {
121                     _log.warn("Waiting for Lucene indexer to shutdown");
122                 }
123 
124                 indexer.halt();
125 
126                 try {
127                     indexerThread.join(THREAD_TIMEOUT);
128                 }
129                 catch (InterruptedException e) {
130                     _log.error("Lucene indexer shutdown interrupted", e);
131                 }
132             }
133         }
134 
135         // Parent
136 
137         super.destroy();
138     }
139 
140     private static final String THREAD_NAME = LuceneIndexer.class.getName();
141 
142     private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
143 
144     private static final int THREAD_TIMEOUT = 60000;
145 
146     private static Log _log = LogFactory.getLog(LuceneServlet.class);
147 
148     private List _indexers = new ArrayList();
149 
150 }