KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > search > CmsIndexingThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/CmsIndexingThread.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.26 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.search;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.CmsLog;
36 import org.opencms.report.I_CmsReport;
37 import org.opencms.search.documents.I_CmsDocumentFactory;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.lucene.document.Document;
41 import org.apache.lucene.index.IndexWriter;
42
43 /**
44  * Implements the indexing method for a single resource as thread.<p>
45  *
46  * The indexing of a single resource was wrapped into a single thread
47  * in order to prevent the indexer from hanging.<p>
48  *
49  * @author Carsten Weinholz
50  *
51  * @version $Revision: 1.26 $
52  *
53  * @since 6.0.0
54  */

55 public class CmsIndexingThread extends Thread JavaDoc {
56
57     /** The log object for this class. */
58     private static final Log LOG = CmsLog.getLog(CmsIndexingThread.class);
59
60     /** The cms object. */
61     private CmsObject m_cms;
62
63     /** The document factory to use. */
64     private I_CmsDocumentFactory m_factory;
65
66     /** The current index. */
67     private CmsSearchIndex m_index;
68
69     /** The current report. */
70     private I_CmsReport m_report;
71
72     /** The resource to index. */
73     private A_CmsIndexResource m_res;
74
75     /** The thread manager. */
76     private CmsIndexingThreadManager m_threadManager;
77
78     /** The index writer. */
79     private IndexWriter m_writer;
80
81     /**
82      * Creates a new indexing thread for a single resource.<p>
83      *
84      * @param cms the cms object
85      * @param writer the writer
86      * @param res the resource to index
87      * @param factory the document factory to index the resource with
88      * @param index the index
89      * @param report the report to write out progress information
90      * @param threadManager the thread manager
91      */

92     public CmsIndexingThread(
93         CmsObject cms,
94         IndexWriter writer,
95         A_CmsIndexResource res,
96         I_CmsDocumentFactory factory,
97         CmsSearchIndex index,
98         I_CmsReport report,
99         CmsIndexingThreadManager threadManager) {
100
101         super("OpenCms: Indexing '" + res.getName() + "'");
102
103         m_cms = cms;
104         m_writer = writer;
105         m_res = res;
106         m_factory = factory;
107         m_index = index;
108         m_report = report;
109         m_threadManager = threadManager;
110     }
111
112     /**
113      * Starts the thread to index a single resource.<p>
114      *
115      * @see java.lang.Runnable#run()
116      */

117     public void run() {
118
119         if (LOG.isDebugEnabled()) {
120             LOG.debug(Messages.get().getBundle().key(
121                 Messages.LOG_INDEXING_WITH_FACTORY_2,
122                 m_res.getRootPath(),
123                 m_factory.getName()));
124         }
125
126         try {
127
128             if (LOG.isDebugEnabled()) {
129                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_CREATING_INDEX_DOC_0));
130             }
131             Document doc = m_factory.newInstance(m_cms, m_res, m_index.getLocale());
132
133             if (doc == null) {
134                 throw new CmsIndexException(Messages.get().container(Messages.ERR_CREATING_INDEX_DOC_0));
135             }
136
137             if (LOG.isDebugEnabled()) {
138                 LOG.debug(Messages.get().getBundle().key(
139                     Messages.LOG_WRITING_INDEX_TO_WRITER_1,
140                     String.valueOf(m_writer)));
141             }
142
143             if (!isInterrupted()) {
144                 // write the document to the index
145
m_writer.addDocument(doc);
146                 // store the document in the thread manager cache
147
m_threadManager.addDocument(m_res, m_index.getLocale(), doc);
148             }
149
150             if (m_report != null && !isInterrupted()) {
151                 m_report.println(
152                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
153                     I_CmsReport.FORMAT_OK);
154
155                 if (LOG.isDebugEnabled()) {
156                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITE_SUCCESS_0));
157                 }
158             }
159
160             if (isInterrupted() && LOG.isDebugEnabled()) {
161                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ABANDONED_THREAD_FINISHED_1, m_res.getRootPath()));
162             }
163         } catch (Exception JavaDoc exc) {
164             // Ignore exception caused by empty documents, so that the report is not messed up with error message
165
Throwable JavaDoc cause = exc.getCause();
166             if ((cause != null && cause instanceof CmsIndexException && ((CmsIndexException)cause).getMessageContainer().getKey().equals(
167                 org.opencms.search.documents.Messages.ERR_NO_CONTENT_1))
168                 || (exc instanceof CmsIndexException && ((CmsIndexException)exc).getMessageContainer().getKey().equals(
169                     org.opencms.search.documents.Messages.ERR_NO_CONTENT_1))) {
170                 m_report.println(
171                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
172                     I_CmsReport.FORMAT_OK);
173                 m_threadManager.finished();
174             } else {
175                 if (m_report != null) {
176                     m_report.println();
177                     m_report.print(
178                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0),
179                         I_CmsReport.FORMAT_WARNING);
180                     m_report.println(exc);
181
182                 }
183                 if (LOG.isErrorEnabled()) {
184                     LOG.error(Messages.get().getBundle().key(
185                         Messages.ERR_INDEX_RESOURCE_FAILED_2,
186                         m_res.getRootPath(),
187                         m_index.getName()), exc);
188                 }
189             }
190         }
191
192         m_threadManager.finished();
193     }
194 }
Popular Tags