PageRenderTime 109ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/java/org/infoglue/cms/controllers/kernel/impl/simple/LuceneController.java

https://github.com/aboutyang/infoglue
Java | 2484 lines | 1885 code | 359 blank | 240 comment | 276 complexity | baa8a06ae9012cabef157e4c29ebfcab MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* ===============================================================================
  2. *
  3. * Part of the InfoGlue Content Management Platform (www.infoglue.org)
  4. *
  5. * ===============================================================================
  6. *
  7. * Copyright (C)
  8. *
  9. * This program is free software; you can redistribute it and/or modify it under
  10. * the terms of the GNU General Public License version 2, as published by the
  11. * Free Software Foundation. See the file LICENSE.html for more information.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
  19. * Place, Suite 330 / Boston, MA 02111-1307 / USA.
  20. *
  21. * ===============================================================================
  22. */
  23. package org.infoglue.cms.controllers.kernel.impl.simple;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStreamWriter;
  30. import java.io.StringReader;
  31. import java.io.Writer;
  32. import java.nio.channels.OverlappingFileLockException;
  33. import java.util.ArrayList;
  34. import java.util.Calendar;
  35. import java.util.Collection;
  36. import java.util.Date;
  37. import java.util.HashMap;
  38. import java.util.HashSet;
  39. import java.util.Iterator;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.Set;
  43. import java.util.concurrent.atomic.AtomicBoolean;
  44. import org.apache.log4j.Level;
  45. import org.apache.log4j.Logger;
  46. import org.apache.lucene.analysis.Analyzer;
  47. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  48. import org.apache.lucene.document.DateTools;
  49. import org.apache.lucene.document.Document;
  50. import org.apache.lucene.document.Field;
  51. import org.apache.lucene.document.NumericField;
  52. import org.apache.lucene.index.IndexReader;
  53. import org.apache.lucene.index.IndexWriter;
  54. import org.apache.lucene.index.IndexWriterConfig;
  55. import org.apache.lucene.index.Term;
  56. import org.apache.lucene.queryParser.MultiFieldQueryParser;
  57. import org.apache.lucene.queryParser.QueryParser;
  58. import org.apache.lucene.search.BooleanClause;
  59. import org.apache.lucene.search.IndexSearcher;
  60. import org.apache.lucene.search.Query;
  61. import org.apache.lucene.search.ScoreDoc;
  62. import org.apache.lucene.search.Sort;
  63. import org.apache.lucene.search.TermQuery;
  64. import org.apache.lucene.search.TopDocs;
  65. import org.apache.lucene.store.Directory;
  66. import org.apache.lucene.store.NIOFSDirectory;
  67. import org.apache.lucene.store.SingleInstanceLockFactory;
  68. import org.apache.lucene.util.Version;
  69. import org.apache.poi.hwpf.HWPFDocument;
  70. import org.apache.poi.hwpf.extractor.WordExtractor;
  71. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  72. import org.exolab.castor.jdo.Database;
  73. import org.exolab.castor.jdo.OQLQuery;
  74. import org.exolab.castor.jdo.QueryResults;
  75. import org.infoglue.cms.entities.content.Content;
  76. import org.infoglue.cms.entities.content.ContentCategory;
  77. import org.infoglue.cms.entities.content.ContentVO;
  78. import org.infoglue.cms.entities.content.ContentVersion;
  79. import org.infoglue.cms.entities.content.ContentVersionVO;
  80. import org.infoglue.cms.entities.content.DigitalAsset;
  81. import org.infoglue.cms.entities.content.DigitalAssetVO;
  82. import org.infoglue.cms.entities.content.SmallestContentVersionVO;
  83. import org.infoglue.cms.entities.content.impl.simple.ContentImpl;
  84. import org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl;
  85. import org.infoglue.cms.entities.content.impl.simple.DigitalAssetImpl;
  86. import org.infoglue.cms.entities.content.impl.simple.MediumDigitalAssetImpl;
  87. import org.infoglue.cms.entities.content.impl.simple.SmallestContentVersionImpl;
  88. import org.infoglue.cms.entities.kernel.BaseEntityVO;
  89. import org.infoglue.cms.entities.management.CategoryAttribute;
  90. import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
  91. import org.infoglue.cms.entities.management.LanguageVO;
  92. import org.infoglue.cms.entities.structure.SiteNode;
  93. import org.infoglue.cms.entities.structure.SiteNodeVO;
  94. import org.infoglue.cms.entities.structure.SiteNodeVersion;
  95. import org.infoglue.cms.entities.structure.SiteNodeVersionVO;
  96. import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl;
  97. import org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl;
  98. import org.infoglue.cms.exception.Bug;
  99. import org.infoglue.cms.exception.SystemException;
  100. import org.infoglue.cms.util.CmsPropertyHandler;
  101. import org.infoglue.cms.util.NotificationListener;
  102. import org.infoglue.cms.util.NotificationMessage;
  103. import org.infoglue.deliver.util.CacheController;
  104. import org.infoglue.deliver.util.RequestAnalyser;
  105. import org.infoglue.deliver.util.Timer;
  106. import org.pdfbox.pdmodel.PDDocument;
  107. import org.pdfbox.util.PDFTextStripper;
  108. public class LuceneController extends BaseController implements NotificationListener
  109. {
  110. private static Directory directory = null;
  111. private static IndexWriter writer = null;
  112. private static IndexReader indexReader = null;
  113. private static int reopened = 0;
  114. private final static Logger logger = Logger.getLogger(LuceneController.class.getName());
  115. private static int indexedDocumentsSinceLastOptimize = 0;
  116. private Integer lastCommitedContentVersionId = -1;
  117. private static Integer numberOfVersionToIndexInBatch = 1000;
  118. private static AtomicBoolean indexingInitialized = new AtomicBoolean(false);
  119. private static AtomicBoolean stopIndexing = new AtomicBoolean(false);
  120. private static AtomicBoolean deleteIndexOnStop = new AtomicBoolean(false);
  121. public static void setNumberOfVersionToIndexInBatch(Integer numberOfVersionToIndexInBatch)
  122. {
  123. numberOfVersionToIndexInBatch = numberOfVersionToIndexInBatch;
  124. }
  125. public static void stopIndexing()
  126. {
  127. stopIndexing.set(true);
  128. }
  129. /**
  130. * Default Constructor
  131. */
  132. public static LuceneController getController()
  133. {
  134. return new LuceneController();
  135. }
  136. private static List<NotificationMessage> qeuedMessages = new ArrayList<NotificationMessage>();
  137. private StandardAnalyzer getStandardAnalyzer() throws Exception
  138. {
  139. return new StandardAnalyzer(Version.LUCENE_34);
  140. }
  141. private Directory getDirectory() throws Exception
  142. {
  143. if(LuceneController.directory != null)
  144. return directory;
  145. String index = CmsPropertyHandler.getContextDiskPath() + File.separator + "lucene" + File.separator + "index";
  146. index = index.replaceAll("//", "/");
  147. //System.out.println("index:" + index);
  148. File INDEX_DIR = new File(index);
  149. directory = new NIOFSDirectory(INDEX_DIR);
  150. directory.setLockFactory(new SingleInstanceLockFactory());
  151. boolean indexExists = IndexReader.indexExists(directory);
  152. if(!indexExists)
  153. {
  154. createIndex(directory);
  155. }
  156. return directory;
  157. }
  158. private void createIndex(Directory directory) throws Exception
  159. {
  160. IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, getStandardAnalyzer());
  161. IndexWriter indexWriter = new IndexWriter(directory, config);
  162. indexWriter.deleteDocuments(new Term("initializer", "true"));
  163. indexWriter.close(true);
  164. }
  165. private IndexWriter getIndexWriter() throws Exception
  166. {
  167. //Singleton returns
  168. if(writer != null)
  169. return writer;
  170. Timer t = new Timer();
  171. Directory directory = getDirectory();
  172. StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
  173. IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, analyzer);
  174. if(getIsIndexedLocked(true))
  175. {
  176. logger.warn("Directory is locked - leaving the messages in the qeuedMessages list...");
  177. throw new Exception("Lock not granted");
  178. }
  179. else
  180. {
  181. writer = new IndexWriter(directory, config);
  182. return writer;
  183. }
  184. }
  185. private IndexReader getIndexReader() throws Exception
  186. {
  187. if(indexReader == null)
  188. {
  189. indexReader = IndexReader.open(getDirectory(), true);
  190. }
  191. synchronized (indexReader)
  192. {
  193. if(!indexReader.isCurrent())
  194. {
  195. reopened++;
  196. indexReader.close();
  197. indexReader = IndexReader.open(getDirectory(), true);
  198. //indexReader = IndexReader.openIfChanged(indexReader, true);
  199. logger.info("reopened:" + reopened);
  200. }
  201. }
  202. return indexReader;
  203. }
  204. private IndexSearcher getIndexSearcher() throws Exception
  205. {
  206. return new IndexSearcher(getIndexReader());
  207. }
  208. private Boolean getIsIndexedLocked() throws Exception
  209. {
  210. return getIsIndexedLocked(false);
  211. }
  212. private Boolean getIsIndexedLocked(boolean returnIfFileLockException) throws Exception
  213. {
  214. Directory directory = getDirectory();
  215. try
  216. {
  217. return IndexWriter.isLocked(directory);
  218. }
  219. catch (OverlappingFileLockException e)
  220. {
  221. return returnIfFileLockException;
  222. }
  223. }
  224. private void unlockIndex() throws Exception
  225. {
  226. Directory directory = getDirectory();
  227. IndexWriter.unlock(directory);
  228. }
  229. public Map<String,Object> getIndexInformation() throws Exception
  230. {
  231. Map<String,Object> info = new HashMap<String,Object>();
  232. try
  233. {
  234. Directory directory = getDirectory();
  235. IndexReader reader = getIndexReader();
  236. int maxDoc = reader.maxDoc();
  237. int numDoc = reader.numDocs();
  238. long lastModified = getIndexReader().lastModified(directory);
  239. info.put("maxDoc", new Integer(maxDoc));
  240. info.put("numDoc", new Integer(numDoc));
  241. info.put("lastModified", new Date(lastModified));
  242. info.put("lastCommitedContentVersionId", getLastCommitedContentVersionId());
  243. List<LanguageVO> languageVOList = LanguageController.getController().getLanguageVOList();
  244. Iterator<LanguageVO> languageVOListIterator = languageVOList.iterator();
  245. outer:while(languageVOListIterator.hasNext())
  246. {
  247. LanguageVO languageVO = (LanguageVO)languageVOListIterator.next();
  248. info.put("indexAllLastCommittedContentVersionId_" + languageVO.getId(), getIndexAllLastCommittedContentVersionId(languageVO.getId()));
  249. info.put("indexAllLastCommittedMetaContentVersionId_" + languageVO.getId(), getIndexAllLastCommittedMetaContentVersionId(languageVO.getId()));
  250. }
  251. //reader.close();
  252. //directory.close();
  253. }
  254. catch (Exception e)
  255. {
  256. logger.error("Error creating index:" + e.getMessage(), e);
  257. throw e;
  258. }
  259. return info;
  260. }
  261. public Integer getIndexAllLastCommittedContentVersionId(Integer languageId) throws Exception
  262. {
  263. Integer indexAllLastCommittedContentVersionId = null;
  264. try
  265. {
  266. Document indexAllDocumentMetaData = getIndexAllStatusDocument();
  267. if(indexAllDocumentMetaData != null && indexAllDocumentMetaData.get("lastCommitedContentVersionId_" + languageId) != null && !indexAllDocumentMetaData.get("lastCommitedContentVersionId_" + languageId).equals("null"))
  268. indexAllLastCommittedContentVersionId = new Integer(indexAllDocumentMetaData.get("lastCommitedContentVersionId_" + languageId));
  269. }
  270. catch (Exception e)
  271. {
  272. logger.error("Error creating index:" + e.getMessage(), e);
  273. throw e;
  274. }
  275. return indexAllLastCommittedContentVersionId;
  276. }
  277. public Integer getIndexAllLastCommittedMetaContentVersionId(Integer languageId) throws Exception
  278. {
  279. Integer indexAllLastCommittedSiteNodeVersionId = null;
  280. try
  281. {
  282. Document indexAllDocumentMetaData = getIndexAllStatusDocument();
  283. if(indexAllDocumentMetaData != null && indexAllDocumentMetaData.get("lastCommitedMetaContentVersionId_" + languageId) != null && !indexAllDocumentMetaData.get("lastCommitedMetaContentVersionId_" + languageId).equals("null"))
  284. indexAllLastCommittedSiteNodeVersionId = new Integer(indexAllDocumentMetaData.get("lastCommitedMetaContentVersionId_" + languageId));
  285. }
  286. catch (Exception e)
  287. {
  288. logger.error("Error creating index:" + e.getMessage(), e);
  289. throw e;
  290. }
  291. return indexAllLastCommittedSiteNodeVersionId;
  292. }
  293. public Document createStatusDocument(Integer lastCommitedContentVersionId) throws Exception
  294. {
  295. Document doc = new Document();
  296. doc.add(new Field("lastCommitedContentVersionId", "" + lastCommitedContentVersionId, Field.Store.YES, Field.Index.NOT_ANALYZED));
  297. doc.add(new Field("lastCommitedModifiedDate", "" + new Date().getTime(), Field.Store.YES, Field.Index.NOT_ANALYZED));
  298. doc.add(new Field("meta", new StringReader("lastCommitedContentVersionId")));
  299. return doc;
  300. }
  301. public Document getStatusDocument() throws Exception
  302. {
  303. List<Document> docs = queryDocuments("meta", "lastCommitedContentVersionId", 5);
  304. logger.info(docs.size() + " total matching documents for 'lastCommitedContentVersionId'");
  305. return (docs != null && docs.size() > 0 ? docs.get(0) : null);
  306. }
  307. public Document getIndexAllStatusDocument() throws Exception
  308. {
  309. List<Document> docs = queryDocuments(new Term("meta", "indexAllRunning"), 5);
  310. logger.info(docs.size() + " total matching documents for 'indexAllRunning'");
  311. return (docs != null && docs.size() > 0 ? docs.get(0) : null);
  312. }
  313. public Integer getLastCommitedContentVersionId() throws Exception
  314. {
  315. Integer lastCommitedContentVersionId = -1;
  316. Document doc = getStatusDocument();
  317. logger.info("STATUS doc:" + doc);
  318. if(doc != null)
  319. {
  320. String lastCommitedContentVersionIdString = doc.get("lastCommitedContentVersionId");
  321. logger.info("doc:" + doc);
  322. logger.info("lastCommitedContentVersionId:" + lastCommitedContentVersionIdString);
  323. lastCommitedContentVersionId = Integer.parseInt(lastCommitedContentVersionIdString);
  324. }
  325. return lastCommitedContentVersionId;
  326. }
  327. private void setLastCommitedContentVersionId(IndexWriter writer, Integer lastCommitedContentVersionId) throws Exception
  328. {
  329. Integer prevLastCommitedContentVersionId = getLastCommitedContentVersionId();
  330. logger.info("prevLastCommitedContentVersionId:" + prevLastCommitedContentVersionId);
  331. logger.info("lastCommitedContentVersionId:" + lastCommitedContentVersionId);
  332. if(lastCommitedContentVersionId == -1 || prevLastCommitedContentVersionId > lastCommitedContentVersionId)
  333. return;
  334. logger.info("setLastCommitedContentVersionId:" + lastCommitedContentVersionId);
  335. Query query = new QueryParser(Version.LUCENE_34, "meta", getStandardAnalyzer()).parse("lastCommitedContentVersionId");
  336. writer.deleteDocuments(query);
  337. writer.addDocument(createStatusDocument(lastCommitedContentVersionId));
  338. }
  339. public Date getLastCommitedModifiedDate() throws Exception
  340. {
  341. Date lastCommitedModifiedDate = new Date(10000);
  342. Document doc = getStatusDocument();
  343. if(doc != null)
  344. {
  345. String lastCommitedModifiedDateString = doc.get("lastCommitedModifiedDate");
  346. logger.info("doc:" + doc);
  347. logger.info("lastCommitedModifiedDate:" + lastCommitedModifiedDateString);
  348. Date d = new Date();
  349. d.setTime(Long.parseLong(lastCommitedModifiedDateString));
  350. lastCommitedModifiedDate = d;
  351. }
  352. return lastCommitedModifiedDate;
  353. }
  354. private void registerIndexAllProcessOngoing(Integer lastCommitedContentVersionId, Integer lastCommitedSiteNodeVersionId, Integer languageId) throws Exception
  355. {
  356. //M�ste skrivas om f�r att uppdatera b�ttre....
  357. //Document doc = new Document();
  358. IndexWriter writer = getIndexWriter();
  359. IndexSearcher searcher = getIndexSearcher();
  360. Term term = new Term("meta", "indexAllRunning");
  361. TermQuery query = new TermQuery(term);
  362. //Query query = new QueryParser(Version.LUCENE_34, "meta", getStandardAnalyzer()).parse("indexAllRunning");
  363. TopDocs hits = searcher.search(query, 50);
  364. //System.out.println("hits:" + hits);
  365. //System.out.println("hits.scoreDocs.length:" + hits.scoreDocs.length);
  366. if(hits.scoreDocs.length > 1)
  367. System.out.println("Must be wrong - should only be one of these docs:" + hits.scoreDocs.length);
  368. if(hits.scoreDocs.length > 0)
  369. {
  370. for(ScoreDoc scoreDoc : hits.scoreDocs)
  371. {
  372. org.apache.lucene.document.Document docExisting = searcher.doc(scoreDoc.doc);
  373. //System.out.println("Updating doc...:" + docExisting);
  374. //System.out.println("lastCommitedContentVersionId:" + lastCommitedContentVersionId);
  375. //System.out.println("lastCommitedSiteNodeVersionId:" + lastCommitedSiteNodeVersionId);
  376. //System.out.println("languageId:" + languageId);
  377. if(lastCommitedContentVersionId != null && lastCommitedContentVersionId != -1)
  378. {
  379. docExisting.removeFields("lastCommitedContentVersionId_" + languageId);
  380. docExisting.add(new Field("lastCommitedContentVersionId_" + languageId, "" + lastCommitedContentVersionId, Field.Store.YES, Field.Index.NOT_ANALYZED));
  381. }
  382. if(lastCommitedSiteNodeVersionId != null && lastCommitedSiteNodeVersionId != -1)
  383. {
  384. docExisting.removeFields("lastCommitedMetaContentVersionId_" + languageId);
  385. docExisting.add(new Field("lastCommitedMetaContentVersionId_" + languageId, "" + lastCommitedSiteNodeVersionId, Field.Store.YES, Field.Index.NOT_ANALYZED));
  386. }
  387. docExisting.removeFields("lastCommitedModifiedDate");
  388. docExisting.add(new Field("lastCommitedModifiedDate", "" + new Date().getTime(), Field.Store.YES, Field.Index.NOT_ANALYZED));
  389. //docExisting.add(new Field("meta", new StringReader("indexAllRunning")));
  390. //docExisting.add(new Field("meta", "indexAllRunning", Field.Store.YES, Field.Index.NOT_ANALYZED));
  391. writer.updateDocument(term, docExisting);
  392. //System.out.println("Updating doc...:" + docExisting);
  393. //Term t = new Term("meta", "indexAllRunning");
  394. break;
  395. }
  396. }
  397. else
  398. {
  399. Document docExisting = new Document();
  400. //System.out.println("lastCommitedContentVersionId:" + lastCommitedContentVersionId);
  401. //System.out.println("lastCommitedSiteNodeVersionId:" + lastCommitedSiteNodeVersionId);
  402. //System.out.println("languageId:" + languageId);
  403. if(lastCommitedContentVersionId != null)
  404. docExisting.add(new Field("lastCommitedContentVersionId_" + languageId, "" + lastCommitedContentVersionId, Field.Store.YES, Field.Index.NOT_ANALYZED));
  405. if(lastCommitedSiteNodeVersionId != null)
  406. docExisting.add(new Field("lastCommitedMetaContentVersionId_" + languageId, "" + lastCommitedSiteNodeVersionId, Field.Store.YES, Field.Index.NOT_ANALYZED));
  407. docExisting.add(new Field("lastCommitedModifiedDate", "" + new Date().getTime(), Field.Store.YES, Field.Index.NOT_ANALYZED));
  408. //docExisting.add(new Field("meta", new StringReader("indexAllRunning")));
  409. docExisting.add(new Field("meta", "indexAllRunning", Field.Store.YES, Field.Index.NOT_ANALYZED));
  410. writer.addDocument(docExisting);
  411. }
  412. searcher.close();
  413. //Query query = new QueryParser(Version.LUCENE_34, "meta", getStandardAnalyzer()).parse("indexAllRunning");
  414. //writer.deleteDocuments(query);
  415. //writer.updateDocument(term, doc);
  416. //writer.addDocument(doc);
  417. //writer.close(true);
  418. writer.commit();
  419. }
  420. private void registerIndexAllProcessDone() throws Exception
  421. {
  422. IndexWriter writer = getIndexWriter();
  423. //Query query = new QueryParser(Version.LUCENE_34, "meta", getStandardAnalyzer()).parse("indexAllRunning");
  424. Term term = new Term("meta", "indexAllRunning");
  425. TermQuery query = new TermQuery(term);
  426. writer.deleteDocuments(query);
  427. writer.commit();
  428. }
  429. public void clearIndex() throws Exception
  430. {
  431. if (indexingInitialized.compareAndSet(false, true))
  432. {
  433. logger.warn("Clearing index..");
  434. try
  435. {
  436. logger.info("NumDocs:" + getIndexReader().numDocs());
  437. IndexWriter writer = getIndexWriter();
  438. writer.deleteAll();
  439. //writer.close(true);
  440. writer.commit();
  441. logger.info("NumDocs after delete:" + getIndexReader().numDocs());
  442. }
  443. catch (Exception e)
  444. {
  445. stopIndexing.set(true);
  446. deleteIndexOnStop.set(true);
  447. logger.error("Error clearing index:" + e.getMessage(), e);
  448. }
  449. finally
  450. {
  451. logger.info("Releasing indexing flag");
  452. this.indexingInitialized.set(false);
  453. stopIndexing.set(false);
  454. }
  455. }
  456. else
  457. {
  458. stopIndexing.set(true);
  459. deleteIndexOnStop.set(true);
  460. logger.error("Could not delete index while indexing. Queueing it....");
  461. }
  462. }
  463. public TopDocs query(String text, Integer numberOfHits) throws Exception
  464. {
  465. return query("contents", text, numberOfHits);
  466. }
  467. public TopDocs query(String field, String text, Integer numberOfHits) throws Exception
  468. {
  469. IndexSearcher searcher = getIndexSearcher();
  470. Query query = new QueryParser(Version.LUCENE_34, "contents", getStandardAnalyzer()).parse(text);
  471. TopDocs hits = searcher.search(query, numberOfHits);
  472. logger.info(hits.totalHits + " total matching documents for '" + text + "'");
  473. return hits;
  474. }
  475. public List<Document> queryDocuments(Term term, Integer numberOfHits) throws Exception
  476. {
  477. IndexSearcher searcher = getIndexSearcher();
  478. Query query = new TermQuery(term);
  479. TopDocs hits = searcher.search(query, numberOfHits);
  480. logger.info(hits.totalHits + " total matching documents for '" + term.field() + ":" + term.text() + "'");
  481. List<Document> docs = new ArrayList<Document>();
  482. for(ScoreDoc scoreDoc : hits.scoreDocs)
  483. {
  484. org.apache.lucene.document.Document doc = searcher.doc(scoreDoc.doc);
  485. docs.add(doc);
  486. }
  487. searcher.close();
  488. return docs;
  489. }
  490. public List<Document> queryDocuments(String field, String text, Integer numberOfHits) throws Exception
  491. {
  492. IndexSearcher searcher = getIndexSearcher();
  493. Query query = new QueryParser(Version.LUCENE_34, field, getStandardAnalyzer()).parse(text);
  494. logger.info("query:" + query);
  495. TopDocs hits = searcher.search(query, numberOfHits);
  496. logger.info(hits.totalHits + " total matching documents for '" + field + ":" + text + "'");
  497. List<Document> docs = new ArrayList<Document>();
  498. for(ScoreDoc scoreDoc : hits.scoreDocs)
  499. {
  500. org.apache.lucene.document.Document doc = searcher.doc(scoreDoc.doc);
  501. docs.add(doc);
  502. }
  503. searcher.close();
  504. return docs;
  505. }
  506. public TopDocs query(String[] fields, BooleanClause.Occur[] flags, String[] queries, Sort sort, Integer numberOfHits) throws Exception
  507. {
  508. IndexSearcher searcher = getIndexSearcher();
  509. Query query = MultiFieldQueryParser.parse(Version.LUCENE_34, queries, fields, flags, getStandardAnalyzer());
  510. //Query query = new QueryParser(Version.LUCENE_34, "contents", getStandardAnalyzer()).parse(text);
  511. TopDocs hits = searcher.search(query, numberOfHits);
  512. logger.info(hits.totalHits + " total matching documents for '" + queries + "'");
  513. return hits;
  514. }
  515. public List<Document> queryDocuments(String[] fields, BooleanClause.Occur[] flags, String[] queries, Sort sort, Integer numberOfHits, Map searchMetaData) throws Exception
  516. {
  517. IndexSearcher searcher = getIndexSearcher();
  518. Query query = MultiFieldQueryParser.parse(Version.LUCENE_34, queries, fields, flags, getStandardAnalyzer());
  519. logger.info("query:" + query);
  520. //Query query = new QueryParser(Version.LUCENE_34, "contents", getStandardAnalyzer()).parse(text);
  521. TopDocs hits = searcher.search(query, numberOfHits);
  522. searchMetaData.put("totalHits", hits.totalHits);
  523. logger.info(hits.totalHits + " total matching documents for '" + query + "'");
  524. //System.out.println(hits.totalHits + " total matching documents for '" + queries + "'");
  525. List<Document> docs = new ArrayList<Document>();
  526. for(ScoreDoc scoreDoc : hits.scoreDocs)
  527. {
  528. org.apache.lucene.document.Document doc = searcher.doc(scoreDoc.doc);
  529. docs.add(doc);
  530. }
  531. searcher.close();
  532. return docs;
  533. }
  534. private void query(IndexSearcher searcher, Analyzer analyzer, String text) throws Exception
  535. {
  536. Query query = new QueryParser(Version.LUCENE_34, "contents", analyzer).parse(text);
  537. TopDocs hits = searcher.search(query, 50);
  538. logger.info(hits.totalHits + " total matching documents for '" + text + "'");
  539. for(ScoreDoc scoreDoc : hits.scoreDocs)
  540. {
  541. org.apache.lucene.document.Document doc = searcher.doc(scoreDoc.doc);
  542. String cvId = doc.get("contentVersionId");
  543. logger.info("cvId: " + cvId);
  544. }
  545. }
  546. public boolean indexAll() throws Exception
  547. {
  548. if(!CmsPropertyHandler.getInternalSearchEngine().equalsIgnoreCase("lucene"))
  549. return false;
  550. logger.warn("INDEXING ALL - correct: " + indexingInitialized + "/" + deleteIndexOnStop + "/" + stopIndexing + "?");
  551. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  552. if(deleteIndexOnStop.get())
  553. {
  554. clearIndex();
  555. deleteIndexOnStop.set(false);
  556. stopIndexing.set(false);
  557. }
  558. else
  559. {
  560. stopIndexing.set(false);
  561. }
  562. logger.warn("Resetting stopIndexing to false....");
  563. logger.warn("------------------------------Got indexAll directive....");
  564. if (indexingInitialized.compareAndSet(false, true))
  565. {
  566. //createTestIndex();
  567. //indexingInitialized.set(false);
  568. //if(true)
  569. // return true;
  570. try
  571. {
  572. Timer t = new Timer();
  573. Timer t2 = new Timer();
  574. //Indexing all normal contents now
  575. logger.info("Indexing all normal contents: " + CmsPropertyHandler.getContextDiskPath());
  576. List<LanguageVO> languageVOList = LanguageController.getController().getLanguageVOList();
  577. Iterator<LanguageVO> languageVOListIterator = languageVOList.iterator();
  578. outer:while(languageVOListIterator.hasNext())
  579. {
  580. LanguageVO languageVO = (LanguageVO)languageVOListIterator.next();
  581. logger.info("Getting notification messages for " + languageVO.getName());
  582. Integer previousIndexAllLastContentVersionId = getIndexAllLastCommittedContentVersionId(languageVO.getId());
  583. int startID = 0;
  584. if(previousIndexAllLastContentVersionId != null)
  585. startID = previousIndexAllLastContentVersionId;
  586. logger.info("Starting from " + startID);
  587. int newLastContentVersionId = getContentNotificationMessages(languageVO, startID);
  588. logger.info("newLastContentVersionId: " + newLastContentVersionId + " on " + languageVO.getName());
  589. registerIndexAllProcessOngoing(newLastContentVersionId, null, languageVO.getId());
  590. //previousIndexAllLastContentVersionId = newLastContentVersionId;
  591. RequestAnalyser.getRequestAnalyser().registerComponentStatistics("getNotificationMessages", t.getElapsedTime());
  592. logger.info("newLastContentVersionId " + newLastContentVersionId);
  593. while(newLastContentVersionId != -1)
  594. {
  595. logger.info("stopIndexing.get():" + stopIndexing.get());
  596. if(stopIndexing.get())
  597. break outer;
  598. Thread.sleep(5000);
  599. newLastContentVersionId = getContentNotificationMessages(languageVO, newLastContentVersionId);
  600. logger.info("newLastContentVersionId: " + newLastContentVersionId + " on " + languageVO.getName());
  601. registerIndexAllProcessOngoing(newLastContentVersionId, null, languageVO.getId());
  602. //previousIndexAllLastContentVersionId = newLastContentVersionId;
  603. RequestAnalyser.getRequestAnalyser().registerComponentStatistics("getNotificationMessages 2", t.getElapsedTime());
  604. logger.info("newLastContentVersionId " + newLastContentVersionId);
  605. }
  606. }
  607. languageVOList = LanguageController.getController().getLanguageVOList();
  608. languageVOListIterator = languageVOList.iterator();
  609. outer:while(languageVOListIterator.hasNext())
  610. {
  611. LanguageVO languageVO = (LanguageVO)languageVOListIterator.next();
  612. logger.info("languageVO from " + languageVO);
  613. List<NotificationMessage> notificationMessages = new ArrayList<NotificationMessage>();
  614. Integer previousIndexAllLastMetaContentVersionId = getIndexAllLastCommittedMetaContentVersionId(languageVO.getId());
  615. logger.info("previousIndexAllLastMetaContentVersionId: " + previousIndexAllLastMetaContentVersionId);
  616. int startID = 0;
  617. if(previousIndexAllLastMetaContentVersionId != null)
  618. startID = previousIndexAllLastMetaContentVersionId;
  619. logger.info("Starting from " + startID);
  620. int newLastMetaContentVersionId = getPageNotificationMessages(notificationMessages, languageVO, startID);
  621. logger.info("newLastSiteNodeVersionId " + newLastMetaContentVersionId + " on " + languageVO.getName());
  622. logger.info("notificationMessages: " + notificationMessages.size());
  623. registerIndexAllProcessOngoing(null, newLastMetaContentVersionId, languageVO.getId());
  624. //previousIndexAllLastMetaContentVersionId = newLastMetaContentVersionId;
  625. RequestAnalyser.getRequestAnalyser().registerComponentStatistics("getNotificationMessagesForStructure", t.getElapsedTime());
  626. logger.info("newLastMetaContentVersionId " + newLastMetaContentVersionId);
  627. while(newLastMetaContentVersionId != -1)
  628. {
  629. logger.info("stopIndexing.get():" + stopIndexing.get());
  630. if(stopIndexing.get())
  631. break outer;
  632. Thread.sleep(5000);
  633. newLastMetaContentVersionId = getPageNotificationMessages(notificationMessages, languageVO, newLastMetaContentVersionId);
  634. logger.info("newLastMetaContentVersionId " + newLastMetaContentVersionId + " on " + languageVO.getName());
  635. logger.info("notificationMessages: " + notificationMessages.size());
  636. registerIndexAllProcessOngoing(null, newLastMetaContentVersionId, languageVO.getId());
  637. //previousIndexAllLastMetaContentVersionId = newLastMetaContentVersionId;
  638. RequestAnalyser.getRequestAnalyser().registerComponentStatistics("getNotificationMessages 2", t.getElapsedTime());
  639. logger.info("newLastMetaContentVersionId " + newLastMetaContentVersionId);
  640. }
  641. }
  642. registerIndexAllProcessDone();
  643. t2.printElapsedTime("All indexing took");
  644. }
  645. catch (Exception e)
  646. {
  647. logger.error("Error indexing notifications:" + e.getMessage(), e);
  648. }
  649. finally
  650. {
  651. logger.error("Releasing indexing flag");
  652. this.indexingInitialized.set(false);
  653. }
  654. }
  655. else
  656. {
  657. logger.warn("-------------------: Allready running index all...");
  658. return false;
  659. }
  660. return true;
  661. }
  662. private void createTestIndex()
  663. {
  664. System.out.println("STARTING TEST");
  665. try
  666. {
  667. clearIndex();
  668. IndexWriter writer = getIndexWriter();
  669. for(int i=0; i<10000; i++)
  670. {
  671. // make a new, empty document
  672. Document doc = new Document();
  673. doc.add(new NumericField("publishDateTime", Field.Store.YES, true).setLongValue(23423423423L));
  674. doc.add(new NumericField("modificationDateTime", Field.Store.YES, true).setLongValue(23423423423L));
  675. doc.add(new Field("modified", DateTools.timeToString(23423423423L, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED));
  676. doc.add(new Field("contentVersionId", "324234234", Field.Store.YES, Field.Index.NOT_ANALYZED));
  677. doc.add(new Field("contentId", "324234234", Field.Store.YES, Field.Index.NOT_ANALYZED));
  678. doc.add(new Field("contentTypeDefinitionId", "344", Field.Store.YES, Field.Index.NOT_ANALYZED));
  679. doc.add(new Field("languageId", "33", Field.Store.YES, Field.Index.NOT_ANALYZED));
  680. doc.add(new Field("repositoryId", "22", Field.Store.YES, Field.Index.NOT_ANALYZED));
  681. doc.add(new Field("lastModifier", "Mattias Bogeblad", Field.Store.YES, Field.Index.NOT_ANALYZED));
  682. doc.add(new Field("stateId", "3", Field.Store.YES, Field.Index.NOT_ANALYZED));
  683. doc.add(new Field("isAsset", "false", Field.Store.YES, Field.Index.NOT_ANALYZED));
  684. doc.add(new Field("contents", new StringReader(i + " fwe foweif oiwejfoijweoifiweuhfi uehwiufh weiuhfiuwehfiew iufiuwehfi ewiufh iuwehfiuehwiufiweuhfiu ehwifhw eifew efiwehfiuwe" +
  685. "ff wehfiuehwiufiuwehfiuehw iufhwei uhfiehwiufweiuhf iwefihw eifiuwe ifhwe ifihew iufi weuhfiuwe" +
  686. "dfbsdjfsjdjfjksdf s f jdsjkfs dkjfh ksdfk sdkfhkds fksd " +
  687. "fjsd fsdhf uiweo p fiieowhf iehwiufiewhfiewfhw efn ewfowe ifioewf owehfowe")));
  688. doc.add(new Field("uid", "" + i, Field.Store.NO, Field.Index.NOT_ANALYZED));
  689. writer.addDocument(doc);
  690. if(i == 1000 || i == 2000 ||i == 3000 ||i == 4000 ||i == 5000 ||i == 6000 ||i == 7000 ||i == 8000 ||i == 9000)
  691. {
  692. //writer.optimize();
  693. //writer.optimize(true);
  694. logger.info("Sleeping...:" + getIndexInformation().get("numDoc"));
  695. Thread.sleep(5000);
  696. }
  697. }
  698. //writer.close(true);
  699. writer.commit();
  700. }
  701. catch (Exception e)
  702. {
  703. e.printStackTrace();
  704. }
  705. }
  706. /**
  707. * This method gets called when a new notification has come.
  708. * It then iterates through the listeners and notifies them.
  709. */
  710. public void addNotificationMessage(NotificationMessage notificationMessage)
  711. {
  712. if(notificationMessage.getClassName().equals(ContentImpl.class.getName()) ||
  713. notificationMessage.getClassName().equals(ContentVersionImpl.class.getName()) ||
  714. notificationMessage.getClassName().equals(SiteNodeImpl.class.getName()) ||
  715. notificationMessage.getClassName().equals(SiteNodeVersionImpl.class.getName()) ||
  716. notificationMessage.getClassName().equals(DigitalAssetImpl.class.getName()) ||
  717. notificationMessage.getClassName().equals(MediumDigitalAssetImpl.class.getName()))
  718. {
  719. if(qeuedMessages.size() == 1000)
  720. {
  721. logger.warn("qeuedMessages went over 1000 - seems wrong");
  722. Thread.dumpStack();
  723. }
  724. synchronized (qeuedMessages)
  725. {
  726. qeuedMessages.add(notificationMessage);
  727. }
  728. }
  729. else
  730. {
  731. logger.info("Skipping indexing:" + notificationMessage.getClassName());
  732. }
  733. }
  734. /**
  735. * This method gets called when a new NotificationMessage is available.
  736. * The writer just calls the transactionHistoryController which stores it.
  737. */
  738. public void notify(NotificationMessage notificationMessage)
  739. {
  740. try
  741. {
  742. if(logger.isInfoEnabled())
  743. logger.info("Indexing:" + notificationMessage.getName() + ":" + notificationMessage.getType() + ":" + notificationMessage.getObjectId() + ":" + notificationMessage.getObjectName());
  744. addNotificationMessage(notificationMessage);
  745. }
  746. catch(Exception e)
  747. {
  748. logger.error("Error notifying: " + e.getMessage());
  749. }
  750. }
  751. public void process() throws Exception
  752. {
  753. logger.info("Process inside LuceneController");
  754. notifyListeners(false, true);
  755. }
  756. public void notifyListeners(boolean forceVersionIndexing, boolean checkForIndexingJobs) throws IOException, Exception
  757. {
  758. if(!CmsPropertyHandler.getInternalSearchEngine().equalsIgnoreCase("lucene") || CmsPropertyHandler.getContextDiskPath().contains("@deploy.dir"))
  759. return;
  760. boolean initDoneLocally = false;
  761. boolean finishDoneLocally = false;
  762. logger.info("------------------------------->notifyListeners before check in " + CmsPropertyHandler.getContextRootPath() + "/" + deleteIndexOnStop.get() + "/" + stopIndexing.get());
  763. if(deleteIndexOnStop.get())
  764. {
  765. clearIndex();
  766. deleteIndexOnStop.set(false);
  767. stopIndexing.set(false);
  768. }
  769. else
  770. {
  771. stopIndexing.set(false);
  772. }
  773. if (!checkForIndexingJobs || indexingInitialized.compareAndSet(false, true))
  774. {
  775. if(checkForIndexingJobs)
  776. initDoneLocally = true;
  777. List<NotificationMessage> internalMessageList = new ArrayList<NotificationMessage>();
  778. synchronized (qeuedMessages)
  779. {
  780. //logger.error("internalMessageList: " + internalMessageList.size() + "/" + qeuedMessages.size());
  781. internalMessageList.addAll(qeuedMessages);
  782. //logger.error("internalMessageList: " + internalMessageList.size() + "/" + qeuedMessages.size());
  783. qeuedMessages.clear();
  784. //logger.error("internalMessageList: " + internalMessageList.size() + "/" + qeuedMessages.size());
  785. }
  786. //Should implement equals on NotificationMessage later
  787. List<NotificationMessage> baseEntitiesToIndexMessageList = new ArrayList<NotificationMessage>();
  788. List<String> existingSignatures = new ArrayList<String>();
  789. logger.info("Before AAAAA:" + internalMessageList.size() + ":" + existingSignatures.size());
  790. Iterator<NotificationMessage> cleanupInternalMessageListIterator = internalMessageList.iterator();
  791. while(cleanupInternalMessageListIterator.hasNext())
  792. {
  793. NotificationMessage notificationMessage = cleanupInternalMessageListIterator.next();
  794. logger.info("Indexing........:" + notificationMessage.getClassName());
  795. if(notificationMessage.getClassName().equals(ContentImpl.class.getName()) || notificationMessage.getClassName().equals(Content.class.getName()))
  796. {
  797. ContentVO contentVO = ContentController.getContentController().getContentVOWithId((Integer)notificationMessage.getObjectId());
  798. ContentTypeDefinitionVO ctdVO = null;
  799. try
  800. {
  801. ctdVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(contentVO.getContentTypeDefinitionId());
  802. }
  803. catch (SystemException sex)
  804. {
  805. logger.warn("Failed to get the content type definition for content with Id: " + contentVO.getContentId() + ". The content will not be indexed. Message: " + sex.getMessage());
  806. logger.info("Failed to get the content type definition for content with Id: " + contentVO.getContentId(), sex);
  807. }
  808. if(ctdVO != null && ctdVO.getName().equals("Meta info"))
  809. {
  810. SiteNodeVO siteNodeVO = SiteNodeController.getController().getSiteNodeVOWithMetaInfoContentId(contentVO.getContentId());
  811. NotificationMessage newNotificationMessage = new NotificationMessage("" + siteNodeVO.getName(), SiteNodeImpl.class.getName(), "SYSTEM", notificationMessage.getType(), siteNodeVO.getId(), "" + siteNodeVO.getName());
  812. String key = "" + newNotificationMessage.getClassName() + "_" + newNotificationMessage.getObjectId() + "_" + "_" + newNotificationMessage.getType();
  813. if(!existingSignatures.contains(key))
  814. {
  815. logger.info("++++++++++++++Got an META PAGE notification - just adding it AS A PAGE instead: " + newNotificationMessage.getObjectId());
  816. baseEntitiesToIndexMessageList.add(newNotificationMessage);
  817. existingSignatures.add(key);
  818. }
  819. else
  820. {
  821. logger.info("++++++++++++++Skipping Content notification - duplicate existed: " + notificationMessage.getObjectId());
  822. }
  823. }
  824. else
  825. {
  826. String key = "" + notificationMessage.getClassName() + "_" + notificationMessage.getObjectId() + "_" + "_" + notificationMessage.getType();
  827. if(!existingSignatures.contains(key))
  828. {
  829. logger.info("++++++++++++++Got an Content notification - just adding it: " + notificationMessage.getObjectId());
  830. baseEntitiesToIndexMessageList.add(notificationMessage);
  831. existingSignatures.add(key);
  832. }
  833. else
  834. {
  835. logger.info("++++++++++++++Skipping Content notification - duplicate existed: " + notificationMessage.getObjectId());
  836. }
  837. }
  838. }
  839. else if(notificationMessage.getClassName().equals(ContentVersionImpl.class.getName()) || notificationMessage.getClassName().equals(ContentVersion.class.getName()))
  840. {
  841. logger.info("++++++++++++++Got an ContentVersion notification - focus on content: " + notificationMessage.getObjectId());
  842. ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getContentVersionVOWithId((Integer)notificationMessage.getObjectId());
  843. ContentVO contentVO = ContentController.getContentController().getContentVOWithId(contentVersionVO.getContentId());
  844. if(contentVO.getContentTypeDefinitionId() != null)
  845. {
  846. ContentTypeDefinitionVO ctdVO = null;
  847. try
  848. {
  849. ctdVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(contentVO.getContentTypeDefinitionId());
  850. }
  851. catch (SystemException sex)
  852. {
  853. logger.warn("Failed to get the content type definition for content with Id: " + contentVO.getContentId() + ". The content version will not be indexed. Message: " + sex.getMessage());
  854. logger.info("Failed to get the content type definition for content with Id: " + contentVO.getContentId(), sex);
  855. }
  856. if(ctdVO != null && ctdVO.getName().equals("Meta info"))
  857. {
  858. SiteNodeVO siteNodeVO = SiteNodeController.getController().getSiteNodeVOWithMetaInfoContentId(contentVO.getContentId());
  859. if (siteNodeVO == null)
  860. {
  861. logger.warn("Got meta info notification but could not find a page for the Content-id. Content.id: " + contentVO.getContentId());
  862. }
  863. else
  864. {
  865. NotificationMessage newNotificationMessage = new NotificationMessage("" + siteNodeVO.getName(), SiteNodeImpl.class.getName(), "SYSTEM", notificationMessage.getType(), siteNodeVO.getId(), "" + siteNodeVO.getName());
  866. String key = "" + newNotificationMessage.getClassName() + "_" + newNotificationMessage.getObjectId() + "_" + newNotificationMessage.getType();
  867. if(!existingSignatures.contains(key))
  868. {
  869. logger.info("++++++++++++++Got an META PAGE notification - just adding it AS A PAGE instead: " + newNotificationMessage.getObjectId());
  870. baseEntitiesToIndexMessageList.add(newNotificationMessage);
  871. existingSignatures.add(key);
  872. }
  873. else
  874. {
  875. logger.info("++++++++++++++Skipping Content notification - duplicate existed: " + notificationMessage.getObjectId());
  876. }
  877. }
  878. }
  879. else
  880. {
  881. NotificationMessage newNotificationMessage = new NotificationMessage("" + contentVersionVO.getContentName(), ContentImpl.class.getName(), "SYSTEM", notificationMessage.getType(), contentVersionVO.getContentId(), "" + contentVersionVO.getContentName());
  882. String key = "" + newNotificationMessage.getClassName() + "_" + newNotificationMessage.getObjectId() + "_" + newNotificationMessage.getType();
  883. if(!existingSignatures.contains(key))
  884. {
  885. logger.info("++++++++++++++Got an Content notification - just adding it: " + newNotificationMessage.getObjectId());
  886. baseEntitiesToIndexMessageList.add(newNotificationMessage);
  887. existingSignatures.add(key);
  888. }
  889. else
  890. {
  891. logger.info("++++++++++++++Skipping Content notification - duplicate existed: " + notificationMessage.getObjectId());
  892. }
  893. }
  894. }
  895. }
  896. else if(notificationMessage.getClassName().equals(DigitalAssetImpl.class.getName()) ||
  897. notificationMessage.getClassName().equals(MediumDigitalAssetImpl.class.getName()) ||
  898. notificationMessage.getClassName().equals(DigitalAsset.class.getName()) ||
  899. notificationMessage.getClassName().equals(SiteNodeImpl.class.getName()) ||
  900. notificationMessage.getClassName().equals(SiteNode.class.getName()) ||
  901. notificationMessage.getClassName().equals(SiteNodeVersionImpl.class.getName()) ||
  902. notificationMessage.getClassName().equals(SiteNodeVersion.class.getName()))
  903. {
  904. logger.info("notificationMessage.getClassName():" + notificationMessage.getClassName());
  905. String key = "" + notificationMessage.getClassName() + "_" + notificationMessage.getObjectId() + "_" + "_" + notificationMessage.getType();
  906. if(notificationMessage.getClassName().equals(SiteNodeVersionImpl.class.getName()) || notificationMessage.getClassName().equals(SiteNodeVersion.class.getName()))
  907. {
  908. logger.info("PPPPPPPPPPPPPPPPPPPPPPPPPP:" + notificationMessage.getObjectId());
  909. SiteNodeVersionVO siteNodeVersionVO = SiteNodeVersionController.getController().getSiteNodeVersionVOWithId((Integer)notificationMessage.getObjectId());
  910. SiteNodeVO siteNodeVO = SiteNodeController.getController().getSiteNodeVOWithId(siteNodeVersionVO.getSiteNodeId());
  911. NotificationMessage newNotificationMessage = new NotificationMessage("" + siteNodeVO.getName(), SiteNodeImpl.class.getName(), "SYSTEM", notificationMessage.getType(), siteNodeVO.getId(), "" + siteNodeVO.getName());
  912. key = "" + newNotificationMessage.getClassName() + "_" + newNotificationMessage.getObjectId() + "_" + newNotificationMessage.getType();
  913. if(!existingSignatures.contains(key))
  914. {
  915. logger.info("++++++++++++++Got an SiteNodeVersionImpl notification - just adding it as SiteNodeImpl: " + newNotificationMessage.getClassName() + ":" + newNotificationMessage.getObjectId());
  916. baseEntitiesToIndexMessageList.add(newNotificationMessage);
  917. existingSignatures.add(key);
  918. }
  919. else
  920. {
  921. logger.info("++++++++++++++Skipping notification - duplicate existed: " + notificationMessage.getClassName() + ":" + notificationMessage.getObjectId());
  922. }
  923. }
  924. else if(notificationMessage.getClassName().equals(SiteNodeImpl.class.getName()) || notificationMessage.getClassName().equals(SiteNode.class.getName()))
  925. {
  926. if(!existingSignatures.contains(key))
  927. {
  928. logger.info("++++++++++++++Got an Page notification - just adding it: " + notificationMessage.getClassName() + ":" + notificationMessage.getObjectId());
  929. baseEntitiesToIndexMessageList.add(notificationMessage);
  930. existingSignatures.add(key);
  931. }
  932. else
  933. {
  934. logger.info("++++++++++++++Skipping notification - duplicate existed: " + notificationMessage.getClassName() + ":" + notificationMessage.getObjectId());
  935. }
  936. }
  937. else
  938. {
  939. NotificationMessage newNotificationMessage = new NotificationMessage("" + notificationMessage.getName(), DigitalAssetImpl.class.getName(), "SYSTEM", notificationMessage.getType(), notificationMessage.getObjectId(), "" + notificationMessage.getName());
  940. key = "" + newNotificationMessage.getClassName() + "_" + newNotificationMessage.getObjectId() + "_" + "_" + newNotificationMessage.getType();
  941. if(!existingSignatures.contains(key))
  942. {
  943. logger.info("++++++++++++++Got an Content notification - just adding it: " + notificationMessage.getClassName() + ":" + notificationMessage.getObjectId());
  944. baseEntitiesToIndexMessageList.add(newNotificationMessage);
  945. existingSignatures.add(key);
  946. }
  947. else
  948. {
  949. logger.info("++++++++++++++Skipping notification - duplicate existed: " + notificationMessage.getClassName() + ":" + notificationMessage.getObjectId());
  950. }
  951. }
  952. }
  953. }
  954. internalMessageList = baseEntitiesToIndexMessageList;
  955. logger.info("After in [" + CmsPropertyHandler.getContextRootPath() + "]:" + internalMessageList.size() + ":" + existingSignatures.size());
  956. try
  957. {
  958. logger.info("notifyListeners actually running");
  959. if(getIsIndexedLocked())
  960. {
  961. logger.warn("The index should not be locked as no indexing is registered to be carried out. Lets unlock it as it may be the result of a crash.");
  962. unlockIndex();
  963. }
  964. //logger.error("Starting indexin of " + qeuedMessages.size());
  965. Timer t = new Timer();
  966. IndexWriter writer = getIndexWriter();
  967. //t.printElapsedTime("Creating writer took");
  968. Database db = CastorDatabaseService.getDatabase();
  969. beginTransaction(db);
  970. try
  971. {
  972. int numberOfMessages = internalMessageList.size();
  973. Iterator internalMessageListIterator = internalMessageList.iterator();
  974. while(internalMessageListIterator.hasNext())
  975. {
  976. NotificationMessage notificationMessage = (NotificationMessage)internalMessageListIterator.next();
  977. try
  978. {
  979. if(logger.isInfoEnabled())
  980. logger.info("Starting indexin of " + notificationMessage);
  981. indexInformation(notificationMessage, writer, internalMessageList, forceVersionIndexing, db);
  982. internalMessageListIterator.remove();
  983. }
  984. catch (Exception e)
  985. {
  986. e.printStackTrace();
  987. }
  988. }
  989. //t.printElapsedTime("Indexing " + numberOfMessages + " documents took");
  990. //Map<String,String> commitUserData = new HashMap<String,String>();
  991. //internalMessageList.clear();
  992. //writer.commit(commitUserData);
  993. logger.info("##############lastCommitedContentVersionId before close:" + lastCommitedContentVersionId);
  994. if(lastCommitedContentVersionId > -1)
  995. {
  996. Integer previousLastCommittedContentVersionId = getLastCommitedContentVersionId();
  997. logger.info("##############previousLastCommittedContentVersionId before close:" + previousLastCommittedContentVersionId);
  998. if(previousLastCommittedContentVersionId < lastCommitedContentVersionId)
  999. {
  1000. try
  1001. {
  1002. logger.info("*************ADDING status doc " + lastCommitedContentVersionId + "**************");
  1003. setLastCommitedContentVersionId(writer, lastCommitedContentVersionId);
  1004. }
  1005. catch (Exception e)
  1006. {
  1007. logger.error("*************ERROR: ADDING status doc**************", e);
  1008. }
  1009. }
  1010. else
  1011. {
  1012. logger.warn("The content version was not a higher number than what was allready indexed - lets not add status....");
  1013. }
  1014. }
  1015. commitTransaction(db);
  1016. }
  1017. catch(Exception e)
  1018. {
  1019. logger.error("An error occurred so we should not complete the transaction:" + e.getMessage(), e);
  1020. rollbackTransaction(db);
  1021. }
  1022. finally
  1023. {
  1024. writer.commit();
  1025. //writer.close(true);
  1026. }
  1027. logger.info("OOOOOOOOOOOOOO:" + getLastCommitedContentVersionId());
  1028. }
  1029. catch (Exception e)
  1030. {
  1031. logger.error("Error indexing notifications:" + e.getMessage());
  1032. logger.warn("Error indexing notifications:" + e.getMessage(), e);
  1033. }
  1034. finally
  1035. {
  1036. logger.info("Releasing indexing flag");
  1037. try
  1038. {
  1039. if(internalMessageList.size() > 0)
  1040. {
  1041. synchronized (qeuedMessages)
  1042. {
  1043. logger.info("Returning internalMessageList:" + internalMessageList.size() + " to qeuedMessages as some failed.");
  1044. qeuedMessages.addAll(internalMessageList);
  1045. internalMessageList.clear();
  1046. }
  1047. }
  1048. }
  1049. catch (Exception e)
  1050. {
  1051. e.printStackTrace();
  1052. }
  1053. if(checkForIndexingJobs)
  1054. {
  1055. this.indexingInitialized.set(false);
  1056. finishDoneLocally = true;
  1057. }
  1058. }
  1059. if(initDoneLocally && !finishDoneLocally)
  1060. logger.error("EEEEEEEEEEEEEEERRRRRRRRRRRRRRROOOOOOOOOOOORRRRRRRR aaaaaaa");
  1061. logger.info("internalMessageList 1:" + internalMessageList.size() + " / " + qeuedMessages.size());
  1062. }
  1063. else
  1064. {
  1065. logger.info("------------------------------->Indexing job allready running... skipping in " + CmsPropertyHandler.getContextRootPath());
  1066. }
  1067. logger.info("queued messages 1:" + qeuedMessages.size());
  1068. }
  1069. public void index() throws Exception
  1070. {
  1071. if(!CmsPropertyHandler.getInternalSearchEngine().equalsIgnoreCase("lucene"))
  1072. return;
  1073. logger.info("Start index: " + CmsPropertyHandle

Large files files are truncated, but you can click here to view the full file