001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsIndexingThread.java,v $
003: * Date : $Date: 2008-02-27 12:05:38 $
004: * Version: $Revision: 1.29 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.search;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsResource;
036: import org.opencms.main.CmsLog;
037: import org.opencms.report.I_CmsReport;
038: import org.opencms.search.documents.I_CmsDocumentFactory;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.lucene.document.Document;
042: import org.apache.lucene.index.IndexWriter;
043:
044: /**
045: * Implements the indexing method for a single resource as thread.<p>
046: *
047: * The indexing of a single resource was wrapped into a single thread
048: * in order to prevent the indexer from hanging.<p>
049: *
050: * @author Carsten Weinholz
051: *
052: * @version $Revision: 1.29 $
053: *
054: * @since 6.0.0
055: */
056: public class CmsIndexingThread extends Thread {
057:
058: /** The log object for this class. */
059: private static final Log LOG = CmsLog
060: .getLog(CmsIndexingThread.class);
061:
062: /** The cms object. */
063: private CmsObject m_cms;
064:
065: /** The document type factory to index the resource with. */
066: private I_CmsDocumentFactory m_documentType;
067:
068: /** The current index. */
069: private CmsSearchIndex m_index;
070:
071: /** The current report. */
072: private I_CmsReport m_report;
073:
074: /** The resource to index. */
075: private CmsResource m_res;
076:
077: /** The index writer. */
078: private IndexWriter m_writer;
079:
080: /**
081: * Creates a new indexing thread for a single resource.<p>
082: *
083: * @param cms the cms object
084: * @param writer the writer
085: * @param res the resource to index
086: * @param documentType the document type factory to index the resource with
087: * @param index the index
088: * @param report the report to write out progress information
089: */
090: public CmsIndexingThread(CmsObject cms, IndexWriter writer,
091: CmsResource res, I_CmsDocumentFactory documentType,
092: CmsSearchIndex index, I_CmsReport report) {
093:
094: super ("OpenCms: Indexing '" + res.getName() + "'");
095:
096: m_cms = cms;
097: m_writer = writer;
098: m_res = res;
099: m_documentType = documentType;
100: m_index = index;
101: m_report = report;
102: }
103:
104: /**
105: * Starts the thread to index a single resource.<p>
106: *
107: * @see java.lang.Runnable#run()
108: */
109: public void run() {
110:
111: if (LOG.isDebugEnabled()) {
112: LOG.debug(Messages.get().getBundle().key(
113: Messages.LOG_INDEXING_WITH_FACTORY_2,
114: m_res.getRootPath(), m_documentType.getName()));
115: }
116:
117: // flag for logging in the "final" block
118: boolean docOk = false;
119:
120: try {
121:
122: if (LOG.isDebugEnabled()) {
123: LOG.debug(Messages.get().getBundle().key(
124: Messages.LOG_CREATING_INDEX_DOC_0));
125: }
126: Document doc = m_documentType.createDocument(m_cms, m_res,
127: m_index);
128:
129: if (doc == null) {
130: throw new CmsIndexException(Messages.get().container(
131: Messages.ERR_CREATING_INDEX_DOC_0));
132: }
133:
134: if (LOG.isDebugEnabled()) {
135: LOG.debug(Messages.get().getBundle().key(
136: Messages.LOG_WRITING_INDEX_TO_WRITER_1,
137: String.valueOf(m_writer)));
138: }
139:
140: if (!isInterrupted()) {
141: // write the document to the index
142: m_writer.addDocument(doc);
143: }
144:
145: // indicate that the document was correctly handled
146: docOk = true;
147:
148: if ((m_report != null) && !isInterrupted()) {
149: m_report.println(
150: org.opencms.report.Messages.get().container(
151: org.opencms.report.Messages.RPT_OK_0),
152: I_CmsReport.FORMAT_OK);
153:
154: if (LOG.isDebugEnabled()) {
155: LOG.debug(Messages.get().getBundle().key(
156: Messages.LOG_WRITE_SUCCESS_0));
157: }
158: }
159:
160: if (isInterrupted() && LOG.isDebugEnabled()) {
161: LOG.debug(Messages.get().getBundle().key(
162: Messages.LOG_ABANDONED_THREAD_FINISHED_1,
163: m_res.getRootPath()));
164: }
165: } catch (Exception exc) {
166: // Ignore exception caused by empty documents, so that the report is not messed up with error message
167: Throwable cause = exc.getCause();
168: if (((cause != null)
169: && (cause instanceof CmsIndexException) && ((CmsIndexException) cause)
170: .getMessageContainer()
171: .getKey()
172: .equals(
173: org.opencms.search.documents.Messages.ERR_NO_CONTENT_1))
174: || ((exc instanceof CmsIndexException) && ((CmsIndexException) exc)
175: .getMessageContainer()
176: .getKey()
177: .equals(
178: org.opencms.search.documents.Messages.ERR_NO_CONTENT_1))) {
179: m_report.println(
180: org.opencms.report.Messages.get().container(
181: org.opencms.report.Messages.RPT_OK_0),
182: I_CmsReport.FORMAT_OK);
183: } else {
184: if (m_report != null) {
185: m_report
186: .println(
187: org.opencms.report.Messages
188: .get()
189: .container(
190: org.opencms.report.Messages.RPT_FAILED_0),
191: I_CmsReport.FORMAT_ERROR);
192: m_report
193: .println(
194: org.opencms.report.Messages
195: .get()
196: .container(
197: org.opencms.report.Messages.RPT_ARGUMENT_1,
198: exc.toString()),
199: I_CmsReport.FORMAT_ERROR);
200: }
201: if (LOG.isErrorEnabled()) {
202: LOG.error(Messages.get().getBundle().key(
203: Messages.ERR_INDEX_RESOURCE_FAILED_2,
204: m_res.getRootPath(), m_index.getName()),
205: exc);
206: }
207: }
208:
209: // set flag to avoid logging in finally block
210: docOk = true;
211:
212: } finally {
213: if (!docOk) {
214: // apparently there was a Throwable that causes an issue
215: if (m_report != null) {
216: m_report
217: .println(
218: org.opencms.report.Messages
219: .get()
220: .container(
221: org.opencms.report.Messages.RPT_FAILED_0),
222: I_CmsReport.FORMAT_ERROR);
223: m_report.println(Messages.get().container(
224: Messages.ERR_INDEX_RESOURCE_FAILED_2,
225: m_res.getRootPath(), m_index.getName()),
226: I_CmsReport.FORMAT_ERROR);
227: }
228: if (LOG.isErrorEnabled()) {
229: LOG.error(Messages.get().getBundle().key(
230: Messages.ERR_INDEX_RESOURCE_FAILED_2,
231: m_res.getRootPath(), m_index.getName()));
232: }
233: }
234: }
235: }
236: }
|