1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.kernel.job.JobSchedulerUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ObjectValuePair;
27  import com.liferay.portal.kernel.util.ServerDetector;
28  import com.liferay.portal.search.lucene.CleanUpJob;
29  import com.liferay.portal.search.lucene.LuceneIndexer;
30  import com.liferay.portal.search.lucene.LuceneUtil;
31  import com.liferay.portal.util.PortalInstances;
32  import com.liferay.portal.util.PropsKeys;
33  import com.liferay.portal.util.PropsUtil;
34  import com.liferay.portal.util.PropsValues;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import javax.servlet.ServletConfig;
40  import javax.servlet.ServletException;
41  import javax.servlet.http.HttpServlet;
42  
43  /**
44   * <a href="LuceneServlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Jorge Ferrer
48   *
49   */
50  public class LuceneServlet extends HttpServlet {
51  
52      public void init(ServletConfig servletConfig) throws ServletException {
53          super.init(servletConfig);
54  
55          long[] companyIds = PortalInstances.getCompanyIds();
56  
57          for (int i = 0; i < companyIds.length; i++) {
58              long companyId = companyIds[i];
59  
60              if (GetterUtil.getBoolean(
61                      PropsUtil.get(PropsKeys.INDEX_ON_STARTUP))) {
62  
63                  if (_log.isInfoEnabled()) {
64                      _log.info("Indexing Lucene on startup");
65                  }
66  
67                  LuceneIndexer indexer = new LuceneIndexer(companyId);
68                  Thread indexerThread = null;
69  
70                  if (GetterUtil.getBoolean(
71                          PropsUtil.get(PropsKeys.INDEX_WITH_THREAD)) ||
72                      ServerDetector.isOrion()) {
73  
74                      indexerThread = new Thread(
75                          indexer, THREAD_NAME + "." + companyId);
76  
77                      indexerThread.setPriority(THREAD_PRIORITY);
78  
79                      indexerThread.start();
80                  }
81                  else {
82                      indexer.reIndex();
83                  }
84  
85                  _indexers.add(
86                      new ObjectValuePair<LuceneIndexer, Thread>(
87                          indexer, indexerThread));
88              }
89              else {
90                  LuceneUtil.checkLuceneDir(companyId);
91              }
92  
93              if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
94                  JobSchedulerUtil.schedule(new CleanUpJob());
95              }
96          }
97      }
98  
99      public void destroy() {
100 
101         // Wait for indexer to be gracefully interrupted
102 
103         for (int i = 0; i < _indexers.size(); i++) {
104             ObjectValuePair<LuceneIndexer, Thread> ovp = _indexers.get(i);
105 
106             LuceneIndexer indexer = ovp.getKey();
107             Thread indexerThread = ovp.getValue();
108 
109             if ((indexer != null) && (!indexer.isFinished()) &&
110                 (indexerThread != null)) {
111 
112                 if (_log.isWarnEnabled()) {
113                     _log.warn("Waiting for Lucene indexer to shutdown");
114                 }
115 
116                 indexer.halt();
117 
118                 try {
119                     indexerThread.join(THREAD_TIMEOUT);
120                 }
121                 catch (InterruptedException e) {
122                     _log.error("Lucene indexer shutdown interrupted", e);
123                 }
124             }
125         }
126 
127         // Parent
128 
129         super.destroy();
130     }
131 
132     private static final String THREAD_NAME = LuceneIndexer.class.getName();
133 
134     private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
135 
136     private static final int THREAD_TIMEOUT = 60000;
137 
138     private static Log _log = LogFactoryUtil.getLog(LuceneServlet.class);
139 
140     private List<ObjectValuePair<LuceneIndexer, Thread>> _indexers =
141         new ArrayList<ObjectValuePair<LuceneIndexer, Thread>>();
142 
143 }