1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class LuceneServlet extends HttpServlet {
53  
54      public void init(ServletConfig servletConfig) throws ServletException {
55          super.init(servletConfig);
56  
57          long[] companyIds = PortalInstances.getCompanyIds();
58  
59          for (int i = 0; i < companyIds.length; i++) {
60              long companyId = companyIds[i];
61  
62              if (GetterUtil.getBoolean(
63                      PropsUtil.get(PropsKeys.INDEX_ON_STARTUP))) {
64  
65                  if (_log.isInfoEnabled()) {
66                      _log.info("Indexing Lucene on startup");
67                  }
68  
69                  LuceneIndexer indexer = new LuceneIndexer(companyId);
70                  Thread indexerThread = null;
71  
72                  if (GetterUtil.getBoolean(
73                          PropsUtil.get(PropsKeys.INDEX_WITH_THREAD)) ||
74                      ServerDetector.isOrion()) {
75  
76                      indexerThread = new Thread(
77                          indexer, THREAD_NAME + "." + companyId);
78  
79                      indexerThread.setPriority(THREAD_PRIORITY);
80  
81                      indexerThread.start();
82                  }
83                  else {
84                      indexer.reIndex();
85                  }
86  
87                  _indexers.add(
88                      new ObjectValuePair<LuceneIndexer, Thread>(
89                          indexer, indexerThread));
90              }
91              else {
92                  LuceneUtil.checkLuceneDir(companyId);
93              }
94  
95              if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
96                  JobSchedulerUtil.schedule(new CleanUpJob());
97              }
98          }
99      }
100 
101     public void destroy() {
102 
103         // Wait for indexer to be gracefully interrupted
104 
105         for (int i = 0; i < _indexers.size(); i++) {
106             ObjectValuePair<LuceneIndexer, Thread> ovp = _indexers.get(i);
107 
108             LuceneIndexer indexer = ovp.getKey();
109             Thread indexerThread = ovp.getValue();
110 
111             if ((indexer != null) && (!indexer.isFinished()) &&
112                 (indexerThread != null)) {
113 
114                 if (_log.isWarnEnabled()) {
115                     _log.warn("Waiting for Lucene indexer to shutdown");
116                 }
117 
118                 indexer.halt();
119 
120                 try {
121                     indexerThread.join(THREAD_TIMEOUT);
122                 }
123                 catch (InterruptedException e) {
124                     _log.error("Lucene indexer shutdown interrupted", e);
125                 }
126             }
127         }
128 
129         // Parent
130 
131         super.destroy();
132     }
133 
134     private static final String THREAD_NAME = LuceneIndexer.class.getName();
135 
136     private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
137 
138     private static final int THREAD_TIMEOUT = 60000;
139 
140     private static Log _log = LogFactoryUtil.getLog(LuceneServlet.class);
141 
142     private List<ObjectValuePair<LuceneIndexer, Thread>> _indexers =
143         new ArrayList<ObjectValuePair<LuceneIndexer, Thread>>();
144 
145 }