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.search.lucene.CleanUpJob;
31  import com.liferay.portal.search.lucene.LuceneIndexer;
32  import com.liferay.portal.search.lucene.LuceneUtil;
33  import com.liferay.portal.util.PortalInstances;
34  import com.liferay.portal.util.PropsKeys;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.portal.util.PropsValues;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import javax.servlet.ServletConfig;
42  import javax.servlet.ServletException;
43  import javax.servlet.http.HttpServlet;
44  
45  /**
46   * <a href="LuceneServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jorge Ferrer
50   *
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  
75                      indexerThread = new Thread(
76                          indexer, THREAD_NAME + "." + companyId);
77  
78                      indexerThread.setPriority(THREAD_PRIORITY);
79  
80                      indexerThread.start();
81                  }
82                  else {
83                      indexer.reIndex();
84                  }
85  
86                  _indexers.add(
87                      new ObjectValuePair<LuceneIndexer, Thread>(
88                          indexer, indexerThread));
89              }
90              else {
91                  LuceneUtil.checkLuceneDir(companyId);
92              }
93  
94              if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
95                  JobSchedulerUtil.schedule(new CleanUpJob());
96              }
97          }
98      }
99  
100     public void destroy() {
101 
102         // Wait for indexer to be gracefully interrupted
103 
104         for (int i = 0; i < _indexers.size(); i++) {
105             ObjectValuePair<LuceneIndexer, Thread> ovp = _indexers.get(i);
106 
107             LuceneIndexer indexer = ovp.getKey();
108             Thread indexerThread = ovp.getValue();
109 
110             if ((indexer != null) && (!indexer.isFinished()) &&
111                 (indexerThread != null)) {
112 
113                 if (_log.isWarnEnabled()) {
114                     _log.warn("Waiting for Lucene indexer to shutdown");
115                 }
116 
117                 indexer.halt();
118 
119                 try {
120                     indexerThread.join(THREAD_TIMEOUT);
121                 }
122                 catch (InterruptedException e) {
123                     _log.error("Lucene indexer shutdown interrupted", e);
124                 }
125             }
126         }
127 
128         // Parent
129 
130         super.destroy();
131     }
132 
133     private static final String THREAD_NAME = LuceneIndexer.class.getName();
134 
135     private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
136 
137     private static final int THREAD_TIMEOUT = 60000;
138 
139     private static Log _log = LogFactoryUtil.getLog(LuceneServlet.class);
140 
141     private List<ObjectValuePair<LuceneIndexer, Thread>> _indexers =
142         new ArrayList<ObjectValuePair<LuceneIndexer, Thread>>();
143 
144 }