001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.servlet;
022:
023: import com.liferay.portal.job.JobScheduler;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ObjectValuePair;
026: import com.liferay.portal.kernel.util.ServerDetector;
027: import com.liferay.portal.lucene.CleanUpJob;
028: import com.liferay.portal.lucene.LuceneIndexer;
029: import com.liferay.portal.lucene.LuceneUtil;
030: import com.liferay.portal.util.PortalInstances;
031: import com.liferay.portal.util.PropsUtil;
032: import com.liferay.portal.util.PropsValues;
033:
034: import java.util.ArrayList;
035: import java.util.List;
036:
037: import javax.servlet.ServletConfig;
038: import javax.servlet.ServletException;
039: import javax.servlet.http.HttpServlet;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: import org.quartz.SchedulerException;
045:
046: /**
047: * <a href="LuceneServlet.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: * @author Jorge Ferrer
051: *
052: */
053: public class LuceneServlet extends HttpServlet {
054:
055: public void init(ServletConfig config) throws ServletException {
056: super .init(config);
057:
058: long[] companyIds = PortalInstances.getCompanyIds();
059:
060: for (int i = 0; i < companyIds.length; i++) {
061: long companyId = companyIds[i];
062:
063: if (GetterUtil.getBoolean(PropsUtil
064: .get(PropsUtil.INDEX_ON_STARTUP))) {
065:
066: if (_log.isInfoEnabled()) {
067: _log.info("Indexing Lucene on startup");
068: }
069:
070: LuceneIndexer indexer = new LuceneIndexer(companyId);
071: Thread indexerThread = null;
072:
073: if (GetterUtil.getBoolean(PropsUtil
074: .get(PropsUtil.INDEX_WITH_THREAD))
075: || ServerDetector.isOrion()) {
076:
077: indexerThread = new Thread(indexer, THREAD_NAME
078: + "." + companyId);
079:
080: indexerThread.setPriority(THREAD_PRIORITY);
081:
082: indexerThread.start();
083: } else {
084: indexer.reIndex();
085: }
086:
087: _indexers.add(new ObjectValuePair(indexer,
088: indexerThread));
089: } else {
090: LuceneUtil.checkLuceneDir(companyId);
091: }
092:
093: if (PropsValues.LUCENE_STORE_JDBC_AUTO_CLEAN_UP) {
094: try {
095: JobScheduler.schedule(new CleanUpJob());
096: } catch (SchedulerException se) {
097: _log.error(se);
098: }
099: }
100: }
101: }
102:
103: public void destroy() {
104:
105: // Wait for indexer to be gracefully interrupted
106:
107: for (int i = 0; i < _indexers.size(); i++) {
108: ObjectValuePair ovp = (ObjectValuePair) _indexers.get(i);
109:
110: LuceneIndexer indexer = (LuceneIndexer) ovp.getKey();
111: Thread indexerThread = (Thread) ovp.getValue();
112:
113: if ((indexer != null) && (!indexer.isFinished())
114: && (indexerThread != null)) {
115:
116: if (_log.isWarnEnabled()) {
117: _log.warn("Waiting for Lucene indexer to shutdown");
118: }
119:
120: indexer.halt();
121:
122: try {
123: indexerThread.join(THREAD_TIMEOUT);
124: } catch (InterruptedException e) {
125: _log
126: .error(
127: "Lucene indexer shutdown interrupted",
128: e);
129: }
130: }
131: }
132:
133: // Parent
134:
135: super .destroy();
136: }
137:
138: private static final String THREAD_NAME = LuceneIndexer.class
139: .getName();
140:
141: private static final int THREAD_PRIORITY = Thread.MIN_PRIORITY;
142:
143: private static final int THREAD_TIMEOUT = 60000;
144:
145: private static Log _log = LogFactory.getLog(LuceneServlet.class);
146:
147: private List _indexers = new ArrayList();
148:
149: }
|