PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/searchcode/app/jobs/PopulateSpellingCorrectorJob.java

https://github.com/boyter/searchcode-server
Java | 81 lines | 65 code | 14 blank | 2 comment | 4 complexity | ee3c1f5be478afc80c7b6b0cab29cf36 MD5 | raw file
  1. package com.searchcode.app.jobs;
  2. import com.searchcode.app.config.Values;
  3. import com.searchcode.app.service.Singleton;
  4. import com.searchcode.app.util.LoggerWrapper;
  5. import com.searchcode.app.util.Properties;
  6. import org.apache.commons.io.FilenameUtils;
  7. import org.quartz.DisallowConcurrentExecution;
  8. import org.quartz.Job;
  9. import org.quartz.JobExecutionContext;
  10. import org.quartz.PersistJobDataAfterExecution;
  11. import java.io.IOException;
  12. import java.nio.file.*;
  13. import java.nio.file.attribute.BasicFileAttributes;
  14. import java.util.EnumSet;
  15. import java.util.List;
  16. @PersistJobDataAfterExecution
  17. @DisallowConcurrentExecution
  18. public class PopulateSpellingCorrectorJob implements Job {
  19. private final LoggerWrapper logger;
  20. public int MAXFILELINEDEPTH = Singleton.getHelpers().tryParseInt(Properties.getProperties().getProperty(Values.MAXFILELINEDEPTH, Values.DEFAULTMAXFILELINEDEPTH), Values.DEFAULTMAXFILELINEDEPTH);
  21. public PopulateSpellingCorrectorJob() {
  22. this.logger = Singleton.getLogger();
  23. }
  24. public void execute(JobExecutionContext context) {
  25. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  26. Path path = Paths.get(Properties.getProperties().getProperty(Values.REPOSITORYLOCATION, Values.DEFAULTREPOSITORYLOCATION));
  27. this.logger.info(String.format("4f5b6cb6::starting populatespellingcorrector in path %s", path.toString()));
  28. try {
  29. Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
  30. @Override
  31. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  32. try {
  33. // Convert Path file to unix style that way everything is easier to reason about
  34. String fileParent = FilenameUtils.separatorsToUnix(file.getParent().toString());
  35. String fileToString = FilenameUtils.separatorsToUnix(file.toString());
  36. String fileName = file.getFileName().toString();
  37. if (Singleton.getHelpers().ignoreFiles(fileParent)) {
  38. return FileVisitResult.CONTINUE;
  39. }
  40. List<String> codeLines;
  41. try {
  42. codeLines = Singleton.getHelpers().readFileLinesGuessEncoding(fileToString, MAXFILELINEDEPTH);
  43. } catch (IOException ex) {
  44. return FileVisitResult.CONTINUE;
  45. }
  46. if (Singleton.getSearchCodeLib().isMinified(codeLines, fileName)) {
  47. return FileVisitResult.CONTINUE;
  48. }
  49. if (codeLines.isEmpty()) {
  50. return FileVisitResult.CONTINUE;
  51. }
  52. if (Singleton.getSearchCodeLib().isBinary(codeLines, fileName).isBinary()) {
  53. return FileVisitResult.CONTINUE;
  54. }
  55. Singleton.getSearchCodeLib().addToSpellingCorrector(String.join(" ", codeLines));
  56. } catch (Exception ex) {
  57. Singleton.getLogger().severe(String.format("a173f0e6::error in class %s exception %s", ex.getClass(), ex.getMessage()));
  58. }
  59. // Continue at all costs
  60. return FileVisitResult.CONTINUE;
  61. }
  62. });
  63. } catch (IOException ex) {
  64. this.logger.severe(String.format("55d4cf9a::error in class %s exception %s", ex.getClass(), ex.getMessage()));
  65. }
  66. }
  67. }