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.lucene;
022:
023: import com.liferay.portal.kernel.search.Indexer;
024: import com.liferay.portal.kernel.util.InstancePool;
025: import com.liferay.portal.kernel.util.ServerDetector;
026: import com.liferay.portal.model.Portlet;
027: import com.liferay.portal.service.PortletLocalServiceUtil;
028: import com.liferay.portal.util.comparator.PortletLuceneComparator;
029: import com.liferay.util.Time;
030:
031: import java.io.IOException;
032:
033: import java.util.Collections;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: import org.apache.commons.lang.time.StopWatch;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040: import org.apache.lucene.index.IndexWriter;
041:
042: /**
043: * <a href="LuceneIndexer.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class LuceneIndexer implements Runnable {
049:
050: public LuceneIndexer(long companyId) {
051: _companyId = companyId;
052: }
053:
054: public void halt() {
055: }
056:
057: public boolean isFinished() {
058: return _finished;
059: }
060:
061: public void run() {
062: reIndex();
063: }
064:
065: public void reIndex() {
066: if (LuceneUtil.INDEX_READ_ONLY) {
067: return;
068: }
069:
070: if (_log.isInfoEnabled()) {
071: _log.info("Reindexing Lucene started");
072: }
073:
074: if (ServerDetector.isOrion()) {
075:
076: // Wait 10 seconds because Orion 2.0.7 initiates LuceneServlet
077: // before it initiates MainServlet.
078:
079: try {
080: Thread.sleep(Time.SECOND * 10);
081: } catch (InterruptedException ie) {
082: }
083: }
084:
085: StopWatch stopWatch1 = null;
086:
087: if (_log.isInfoEnabled()) {
088: stopWatch1 = new StopWatch();
089:
090: stopWatch1.start();
091: }
092:
093: LuceneUtil.delete(_companyId);
094:
095: try {
096: IndexWriter writer = LuceneUtil.getWriter(_companyId, true);
097:
098: LuceneUtil.write(writer);
099: } catch (IOException ioe) {
100: _log.error(ioe.getMessage(), ioe);
101: }
102:
103: String[] indexIds = new String[] { String.valueOf(_companyId) };
104:
105: try {
106: List portlets = PortletLocalServiceUtil
107: .getPortlets(_companyId);
108:
109: Collections.sort(portlets, new PortletLuceneComparator());
110:
111: Iterator itr = portlets.iterator();
112:
113: while (itr.hasNext()) {
114: Portlet portlet = (Portlet) itr.next();
115:
116: String className = portlet.getIndexerClass();
117:
118: if (portlet.isActive() && className != null) {
119: StopWatch stopWatch2 = null;
120:
121: if (_log.isInfoEnabled()) {
122: stopWatch2 = new StopWatch();
123:
124: stopWatch2.start();
125: }
126:
127: if (_log.isInfoEnabled()) {
128: _log.info("Reindexing with " + className
129: + " started");
130: }
131:
132: Indexer indexer = (Indexer) InstancePool
133: .get(className);
134:
135: indexer.reIndex(indexIds);
136:
137: if (_log.isInfoEnabled()) {
138: _log.info("Reindexing with " + className
139: + " completed in "
140: + (stopWatch2.getTime() / Time.SECOND)
141: + " seconds");
142: }
143: }
144: }
145:
146: if (_log.isInfoEnabled()) {
147: _log.info("Reindexing Lucene completed in "
148: + (stopWatch1.getTime() / Time.SECOND)
149: + " seconds");
150: }
151: } catch (Exception e) {
152: _log.error("Error encountered while reindexing", e);
153:
154: if (_log.isInfoEnabled()) {
155: _log.info("Reindexing Lucene failed");
156: }
157: }
158:
159: _finished = true;
160: }
161:
162: private static Log _log = LogFactory.getLog(LuceneIndexer.class);
163:
164: private long _companyId;
165: private boolean _finished;
166:
167: }
|