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