1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.kernel.job.JobSchedulerUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.ObjectValuePair;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.search.lucene.CleanUpJob;
24  import com.liferay.portal.search.lucene.LuceneIndexer;
25  import com.liferay.portal.util.PortalInstances;
26  import com.liferay.portal.util.PropsUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import javax.servlet.ServletConfig;
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServlet;
35  
36  /**
37   * <a href="LuceneServlet.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Jorge Ferrer
41   */
42  public class LuceneServlet extends HttpServlet {
43  
44      public void init(ServletConfig servletConfig) throws ServletException {
45          super.init(servletConfig);
46  
47          long[] companyIds = PortalInstances.getCompanyIds();
48  
49          for (int i = 0; i < companyIds.length; i++) {
50              long companyId = companyIds[i];
51  
52              if (GetterUtil.getBoolean(
53                      PropsUtil.get(PropsKeys.INDEX_ON_STARTUP))) {
54  
55                  if (_log.isInfoEnabled()) {
56                      _log.info("Indexing Lucene on startup");
57                  }
58  
59                  LuceneIndexer indexer = new LuceneIndexer(companyId);
60                  Thread indexerThread = null;
61  
62                  if (GetterUtil.getBoolean(
63                          PropsUtil.get(PropsKeys.INDEX_WITH_THREAD))) {
64  
65                      indexerThread = new Thread(
66                          indexer, THREAD_NAME + "." + companyId);
67  
68                      indexerThread.setPriority(THREAD_PRIORITY);
69  
70                      indexerThread.start();
71                  }
72                  else {
73                      indexer.reindex();
74                  }
75  
76                  _indexers.add(
77                      new ObjectValuePair<LuceneIndexer, Thread>(
78                          indexer, indexerThread));
79              }
80  
81              if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
82                  JobSchedulerUtil.schedule(new CleanUpJob());
83              }
84          }
85      }
86  
87      public void destroy() {
88  
89          // Wait for indexer to be gracefully interrupted
90  
91          for (int i = 0; i < _indexers.size(); i++) {
92              ObjectValuePair<LuceneIndexer, Thread> ovp = _indexers.get(i);
93  
94              LuceneIndexer indexer = ovp.getKey();
95              Thread indexerThread = ovp.getValue();
96  
97              if ((indexer != null) && (!indexer.isFinished()) &&
98                  (indexerThread != null)) {
99  
100                 if (_log.isWarnEnabled()) {
101                     _log.warn("Waiting for Lucene indexer to shutdown");
102                 }
103 
104                 indexer.halt();
105 
106                 try {
107                     indexerThread.join(THREAD_TIMEOUT);
108                 }
109                 catch (InterruptedException e) {
110                     _log.error("Lucene indexer shutdown interrupted", e);
111                 }
112             }
113         }
114 
115         // Parent
116 
117         super.destroy();
118     }
119 
120     private static final String THREAD_NAME = LuceneIndexer.class.getName();
121 
122     private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
123 
124     private static final int THREAD_TIMEOUT = 60000;
125 
126     private static Log _log = LogFactoryUtil.getLog(LuceneServlet.class);
127 
128     private List<ObjectValuePair<LuceneIndexer, Thread>> _indexers =
129         new ArrayList<ObjectValuePair<LuceneIndexer, Thread>>();
130 
131 }