PageRenderTime 55ms CodeModel.GetById 21ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

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