1 31 32 package org.opencms.search; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.i18n.CmsMessageContainer; 36 import org.opencms.main.CmsLog; 37 import org.opencms.main.OpenCms; 38 import org.opencms.report.CmsLogReport; 39 import org.opencms.report.I_CmsReport; 40 import org.opencms.search.documents.I_CmsDocumentFactory; 41 42 import java.io.IOException ; 43 import java.util.Map ; 44 45 import org.apache.commons.logging.Log; 46 import org.apache.lucene.document.Document; 47 import org.apache.lucene.index.IndexWriter; 48 49 59 public class CmsIndexingThreadManager extends Thread { 60 61 62 private static final Log LOG = CmsLog.getLog(CmsIndexingThreadManager.class); 63 64 65 private int m_abandonedCounter; 66 67 68 private Map m_documentCache; 69 70 71 private int m_fileCounter; 72 73 74 private I_CmsReport m_report; 75 76 77 private int m_returnedCounter; 78 79 80 private long m_timeout; 81 82 89 public CmsIndexingThreadManager(I_CmsReport report, long timeout, String indexName, Map documentCache) { 90 91 super("OpenCms: Indexing thread manager for search index '" + indexName + "'"); 92 93 m_report = report; 94 m_timeout = timeout; 95 m_fileCounter = 0; 96 m_abandonedCounter = 0; 97 m_returnedCounter = 0; 98 m_documentCache = documentCache; 99 this.start(); 100 } 101 102 109 public synchronized void addDocument(A_CmsIndexResource res, String locale, Document doc) { 110 111 if (m_documentCache != null) { 112 m_documentCache.put(res.getId().toString().concat(locale), doc); 113 } 114 } 115 116 129 public void createIndexingThread(CmsObject cms, IndexWriter writer, A_CmsIndexResource res, CmsSearchIndex index) { 130 131 I_CmsDocumentFactory factory = OpenCms.getSearchManager().getDocumentFactory(res); 132 if ((factory == null) || !index.getDocumenttypes(res.getRootPath()).contains(factory.getName())) { 133 135 m_fileCounter++; 136 m_returnedCounter++; 137 if (m_report != null) { 138 m_report.println( 139 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_SKIPPED_0), 140 I_CmsReport.FORMAT_NOTE); 141 } 142 if (LOG.isDebugEnabled()) { 143 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SKIPPED_1, res.getRootPath())); 144 } 145 146 return; 148 } 149 150 Document cachedDoc; 151 if ((m_documentCache != null) 152 && ((cachedDoc = (Document)m_documentCache.get(res.getId().toString().concat(index.getLocale()))) != null)) { 153 155 m_fileCounter++; 156 m_returnedCounter++; 157 try { 158 writer.addDocument(cachedDoc); 159 if (m_report != null) { 160 m_report.println( 161 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 162 I_CmsReport.FORMAT_OK); 163 } 164 } catch (IOException e) { 165 if (m_report != null) { 166 m_report.println(); 167 m_report.print( 168 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0), 169 I_CmsReport.FORMAT_WARNING); 170 m_report.println(e); 171 172 } 173 if (LOG.isWarnEnabled()) { 174 LOG.warn(Messages.get().getBundle().key( 175 Messages.ERR_INDEX_RESOURCE_FAILED_2, 176 res.getRootPath(), 177 index.getName()), e); 178 } 179 } 180 } else { 181 184 CmsIndexingThread thread = new CmsIndexingThread(cms, writer, res, factory, index, m_report, this); 185 186 try { 187 m_fileCounter++; 188 thread.start(); 189 thread.join(m_timeout); 190 191 if (thread.isAlive()) { 192 193 if (LOG.isWarnEnabled()) { 194 LOG.warn(Messages.get().getBundle().key(Messages.LOG_INDEXING_TIMEOUT_1, res.getRootPath())); 195 } 196 m_report.println(); 197 m_report.print( 198 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0), 199 I_CmsReport.FORMAT_WARNING); 200 m_report.println( 201 Messages.get().container(Messages.RPT_SEARCH_INDEXING_TIMEOUT_1, res.getRootPath()), 202 I_CmsReport.FORMAT_WARNING); 203 204 m_abandonedCounter++; 205 thread.interrupt(); 206 } 207 } catch (InterruptedException exc) { 208 } 210 } 211 } 212 213 216 public synchronized void finished() { 217 218 m_returnedCounter++; 219 } 220 221 226 public int getCounter() { 227 228 return m_fileCounter; 229 } 230 231 236 public boolean isRunning() { 237 238 return (m_returnedCounter + m_abandonedCounter < m_fileCounter); 239 } 240 241 249 public void reportStatistics() { 250 251 CmsMessageContainer message = Messages.get().container( 252 Messages.RPT_SEARCH_INDEXING_STATS_4, 253 new Object [] { 254 new Integer (m_fileCounter), 255 new Integer (m_returnedCounter), 256 new Integer (m_abandonedCounter), 257 m_report.formatRuntime()}); 258 259 if (m_report != null) { 260 m_report.println(message); 261 } 262 if (!(m_report instanceof CmsLogReport) && LOG.isInfoEnabled()) { 263 LOG.info(message.key()); 265 } 266 } 267 268 275 public void run() { 276 277 int max = 20; 278 279 try { 280 Thread.sleep(30000); 282 283 while (m_fileCounter > m_returnedCounter && max-- > 0) { 284 285 Thread.sleep(30000); 286 if (LOG.isWarnEnabled()) { 288 LOG.warn(Messages.get().getBundle().key( 289 Messages.LOG_WAITING_ABANDONED_THREADS_2, 290 new Integer (m_abandonedCounter), 291 new Integer ((m_fileCounter - m_returnedCounter)))); 292 } 293 294 } 295 } catch (Exception exc) { 296 } 298 299 if (max > 0) { 300 if (LOG.isInfoEnabled()) { 301 LOG.info(Messages.get().getBundle().key(Messages.LOG_THREADS_FINISHED_0)); 302 } 303 } else { 304 LOG.error(Messages.get().getBundle().key( 305 Messages.LOG_THREADS_FINISHED_0, 306 new Integer (m_fileCounter - m_returnedCounter))); 307 } 308 } 309 } | Popular Tags |