1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.kernel.util.ServerDetector;
24  import com.liferay.portal.search.lucene.CleanUpJob;
25  import com.liferay.portal.search.lucene.LuceneIndexer;
26  import com.liferay.portal.util.PortalInstances;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.portal.util.PropsValues;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import javax.servlet.ServletConfig;
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServlet;
36  
37  /**
38   * <a href="LuceneServlet.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Jorge Ferrer
42   */
43  public class LuceneServlet extends HttpServlet {
44  
45      public void init(ServletConfig servletConfig) throws ServletException {
46          super.init(servletConfig);
47  
48          long[] companyIds = PortalInstances.getCompanyIds();
49  
50          for (int i = 0; i < companyIds.length; i++) {
51              long companyId = companyIds[i];
52  
53              if (GetterUtil.getBoolean(
54                      PropsUtil.get(PropsKeys.INDEX_ON_STARTUP))) {
55  
56                  if (_log.isInfoEnabled()) {
57                      _log.info("Indexing Lucene on startup");
58                  }
59  
60                  LuceneIndexer indexer = new LuceneIndexer(companyId);
61                  Thread indexerThread = null;
62  
63                  if (GetterUtil.getBoolean(
64                          PropsUtil.get(PropsKeys.INDEX_WITH_THREAD)) ||
65                      ServerDetector.isOrion()) {
66  
67                      indexerThread = new Thread(
68                          indexer, THREAD_NAME + "." + companyId);
69  
70                      indexerThread.setPriority(THREAD_PRIORITY);
71  
72                      indexerThread.start();
73                  }
74                  else {
75                      indexer.reIndex();
76                  }
77  
78                  _indexers.add(
79                      new ObjectValuePair<LuceneIndexer, Thread>(
80                          indexer, indexerThread));
81              }
82  
83              if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
84                  JobSchedulerUtil.schedule(new CleanUpJob());
85              }
86          }
87      }
88  
89      public void destroy() {
90  
91          // Wait for indexer to be gracefully interrupted
92  
93          for (int i = 0; i < _indexers.size(); i++) {
94              ObjectValuePair<LuceneIndexer, Thread> ovp = _indexers.get(i);
95  
96              LuceneIndexer indexer = ovp.getKey();
97              Thread indexerThread = ovp.getValue();
98  
99              if ((indexer != null) && (!indexer.isFinished()) &&
100                 (indexerThread != null)) {
101 
102                 if (_log.isWarnEnabled()) {
103                     _log.warn("Waiting for Lucene indexer to shutdown");
104                 }
105 
106                 indexer.halt();
107 
108                 try {
109                     indexerThread.join(THREAD_TIMEOUT);
110                 }
111                 catch (InterruptedException e) {
112                     _log.error("Lucene indexer shutdown interrupted", e);
113                 }
114             }
115         }
116 
117         // Parent
118 
119         super.destroy();
120     }
121 
122     private static final String THREAD_NAME = LuceneIndexer.class.getName();
123 
124     private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
125 
126     private static final int THREAD_TIMEOUT = 60000;
127 
128     private static Log _log = LogFactoryUtil.getLog(LuceneServlet.class);
129 
130     private List<ObjectValuePair<LuceneIndexer, Thread>> _indexers =
131         new ArrayList<ObjectValuePair<LuceneIndexer, Thread>>();
132 
133 }