1 31 32 package org.opencms.search; 33 34 import org.opencms.db.CmsPublishedResource; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProject; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.report.I_CmsReport; 42 import org.opencms.search.documents.I_CmsDocumentFactory; 43 44 import java.io.IOException ; 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 import org.apache.commons.logging.Log; 50 import org.apache.lucene.document.Document; 51 import org.apache.lucene.document.Field; 52 import org.apache.lucene.index.IndexReader; 53 import org.apache.lucene.index.IndexWriter; 54 import org.apache.lucene.index.Term; 55 56 66 public class CmsVfsIndexer implements I_CmsIndexer { 67 68 69 private static final Log LOG = CmsLog.getLog(CmsVfsIndexer.class); 70 71 72 private CmsObject m_cms; 73 74 75 private CmsSearchIndex m_index; 76 77 78 private I_CmsReport m_report; 79 80 83 public void deleteResources(IndexReader reader, List resourcesToDelete) { 84 85 if ((resourcesToDelete == null) || resourcesToDelete.isEmpty()) { 86 return; 88 } 89 90 List resourcesAlreadyDeleted = new ArrayList (resourcesToDelete.size()); 92 93 Iterator i = resourcesToDelete.iterator(); 94 while (i.hasNext()) { 95 CmsPublishedResource res = (CmsPublishedResource)i.next(); 97 String rootPath = res.getRootPath(); 98 if (!resourcesAlreadyDeleted.contains(rootPath)) { 99 resourcesAlreadyDeleted.add(rootPath); 101 Term term = new Term(I_CmsDocumentFactory.DOC_PATH, rootPath); 103 try { 104 reader.deleteDocuments(term); 106 } catch (IOException e) { 107 if (LOG.isWarnEnabled()) { 108 LOG.warn(Messages.get().getBundle().key( 109 Messages.LOG_IO_INDEX_DOCUMENT_DELETE_2, 110 rootPath, 111 m_index.getName()), e); 112 } 113 } 114 } 115 } 116 } 117 118 121 public A_CmsIndexResource getIndexResource(CmsObject cms, Document doc) throws CmsException { 122 123 A_CmsIndexResource result = null; 124 125 Field f = doc.getField(I_CmsDocumentFactory.DOC_PATH); 126 if (f != null) { 127 128 String path = cms.getRequestContext().removeSiteRoot(f.stringValue()); 129 CmsResource resource = cms.readResource(path); 130 result = new CmsVfsIndexResource(resource); 132 } 133 134 return result; 135 } 136 137 140 public CmsSearchIndexUpdateData getUpdateData(CmsSearchIndexSource source, List publishedResources) { 141 142 CmsSearchIndexUpdateData result = new CmsSearchIndexUpdateData(source, this); 144 145 Iterator i = publishedResources.iterator(); 146 while (i.hasNext()) { 147 CmsPublishedResource resource = (CmsPublishedResource)i.next(); 149 if (!resource.getStructureId().isNullUUID()) { 151 if (CmsProject.isInsideProject(source.getResourcesNames(), resource.getRootPath())) { 153 if (resource.isNew()) { 155 if (isResourceInTimeWindow(resource)) { 157 result.addResourceToUpdate(resource); 159 } 160 } else if (resource.isDeleted()) { 161 result.addResourceToDelete(resource); 163 } else if (resource.isChanged() || resource.isUnChanged()) { 164 result.addResourceToDelete(resource); 168 if (isResourceInTimeWindow(resource)) { 169 result.addResourceToUpdate(resource); 171 } 172 } 173 } 174 } 175 } 176 return result; 177 } 178 179 182 public I_CmsIndexer newInstance(CmsObject cms, I_CmsReport report, CmsSearchIndex index) { 183 184 CmsVfsIndexer indexer = new CmsVfsIndexer(); 185 186 indexer.m_cms = cms; 187 indexer.m_report = report; 188 indexer.m_index = index; 189 190 return indexer; 191 } 192 193 196 public void rebuildIndex(IndexWriter writer, CmsIndexingThreadManager threadManager, CmsSearchIndexSource source) 197 throws CmsIndexException { 198 199 List resourceNames = source.getResourcesNames(); 200 Iterator i = resourceNames.iterator(); 201 while (i.hasNext()) { 202 String resourceName = (String )i.next(); 204 List resources = null; 205 try { 206 resources = m_cms.readResources(resourceName, CmsResourceFilter.DEFAULT.addRequireFile()); 208 } catch (CmsException e) { 209 if (m_report != null) { 210 m_report.println(Messages.get().container( 211 Messages.RPT_UNABLE_TO_READ_SOURCE_2, 212 resourceName, 213 e.getLocalizedMessage()), I_CmsReport.FORMAT_WARNING); 214 } 215 if (LOG.isWarnEnabled()) { 216 LOG.warn(Messages.get().getBundle().key( 217 Messages.LOG_UNABLE_TO_READ_SOURCE_2, 218 resourceName, 219 m_index.getName()), e); 220 } 221 } 222 if (resources != null) { 223 Iterator j = resources.iterator(); 225 while (j.hasNext()) { 226 CmsResource resource = (CmsResource)j.next(); 228 updateResource(writer, threadManager, resource); 229 } 230 } 231 } 232 } 233 234 237 public void updateResources(IndexWriter writer, CmsIndexingThreadManager threadManager, List resourcesToUpdate) 238 throws CmsIndexException { 239 240 if ((resourcesToUpdate == null) || resourcesToUpdate.isEmpty()) { 241 return; 243 } 244 245 List resourcesAlreadyUpdated = new ArrayList (resourcesToUpdate.size()); 247 248 Iterator i = resourcesToUpdate.iterator(); 250 while (i.hasNext()) { 251 CmsPublishedResource res = (CmsPublishedResource)i.next(); 252 CmsResource resource = null; 253 try { 254 resource = m_cms.readResource(res.getRootPath()); 255 } catch (CmsException e) { 256 if (LOG.isWarnEnabled()) { 257 LOG.warn(Messages.get().getBundle().key( 258 Messages.LOG_UNABLE_TO_READ_RESOURCE_2, 259 resource.getRootPath(), 260 m_index.getName()), e); 261 } 262 } 263 if (resource != null) { 264 if (!resourcesAlreadyUpdated.contains(resource.getRootPath())) { 265 resourcesAlreadyUpdated.add(resource.getRootPath()); 267 updateResource(writer, threadManager, resource); 268 } 269 } 270 } 271 } 272 273 279 protected boolean isResourceInTimeWindow(CmsPublishedResource resource) { 280 281 return m_cms.existsResource( 282 m_cms.getRequestContext().removeSiteRoot(resource.getRootPath()), 283 CmsResourceFilter.DEFAULT); 284 } 285 286 295 protected void updateResource(IndexWriter writer, CmsIndexingThreadManager threadManager, CmsResource resource) 296 throws CmsIndexException { 297 298 if (resource.isInternal()) { 299 return; 301 } 302 304 try { 305 306 if (m_report != null) { 307 m_report.print(org.opencms.report.Messages.get().container( 308 org.opencms.report.Messages.RPT_SUCCESSION_1, 309 String.valueOf(threadManager.getCounter() + 1)), I_CmsReport.FORMAT_NOTE); 310 m_report.print( 311 Messages.get().container(Messages.RPT_SEARCH_INDEXING_FILE_BEGIN_0), 312 I_CmsReport.FORMAT_NOTE); 313 m_report.print(org.opencms.report.Messages.get().container( 314 org.opencms.report.Messages.RPT_ARGUMENT_1, 315 m_report.removeSiteRoot(resource.getRootPath()))); 316 m_report.print( 317 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0), 318 I_CmsReport.FORMAT_DEFAULT); 319 } 320 321 A_CmsIndexResource indexResource = new CmsVfsIndexResource(resource); 322 threadManager.createIndexingThread(m_cms, writer, indexResource, m_index); 323 324 } catch (Exception e) { 325 326 if (m_report != null) { 327 m_report.println( 328 Messages.get().container(Messages.RPT_SEARCH_INDEXING_FAILED_0), 329 I_CmsReport.FORMAT_WARNING); 330 } 331 if (LOG.isWarnEnabled()) { 332 LOG.warn(Messages.get().getBundle().key( 333 Messages.ERR_INDEX_RESOURCE_FAILED_2, 334 resource.getRootPath(), 335 m_index.getName()), e); 336 } 337 throw new CmsIndexException(Messages.get().container( 338 Messages.ERR_INDEX_RESOURCE_FAILED_2, 339 resource.getRootPath(), 340 m_index.getName())); 341 } 342 } 343 }
| Popular Tags
|