3131
3232import java .io .IOException ;
3333import java .nio .charset .Charset ;
34+ import java .util .concurrent .ExecutorService ;
35+ import java .util .concurrent .Executors ;
36+ import java .util .concurrent .TimeUnit ;
3437
3538public class DartSensor implements Sensor {
3639
3740 private static final Logger LOGGER = LoggerFactory .getLogger (DartSensor .class );
41+ private static final int EXECUTOR_TIMEOUT = 10000 ;
3842
3943 @ Override
4044 public void describe (SensorDescriptor sensorDescriptor ) {
@@ -53,29 +57,46 @@ public void execute(SensorContext sensorContext) {
5357 FilePredicate dartAndMain = sensorContext .fileSystem ().predicates ().and (hasDart , isMain );
5458 FilePredicate dartAndTest = sensorContext .fileSystem ().predicates ().and (hasDart , isTest );
5559 final Charset charset = sensorContext .fileSystem ().encoding ();
60+
61+ final ExecutorService executorService = Executors .newWorkStealingPool ();
62+
5663 for (InputFile inf : sensorContext .fileSystem ().inputFiles (dartAndMain )){
57- // Visit source files
58- try {
59- final AntlrContext antlrContext = AntlrContext .fromInputFile (inf , charset );
60- ParseTreeItemVisitor visitor = new CustomTreeVisitor (new HighlighterVisitor (),
61- new SourceLinesVisitor (), new CyclomaticComplexityVisitor ());
62- visitor .fillContext (sensorContext , antlrContext );
63- } catch (IOException e ) {
64- LOGGER .warn ("Unexpected error while analyzing file " + inf .filename (), e );
65- }
64+
65+ executorService .execute (() -> {
66+ // Visit source files
67+ try {
68+ final AntlrContext antlrContext = AntlrContext .fromInputFile (inf , charset );
69+ ParseTreeItemVisitor visitor = new CustomTreeVisitor (new HighlighterVisitor (),
70+ new SourceLinesVisitor (), new CyclomaticComplexityVisitor ());
71+ visitor .fillContext (sensorContext , antlrContext );
72+ } catch (IOException e ) {
73+ LOGGER .warn ("Unexpected error while analyzing file " + inf .filename (), e );
74+ }
75+ });
6676
6777 }
6878
6979 for (InputFile inf : sensorContext .fileSystem ().inputFiles (dartAndTest )){
70- // Visit test files (for syntax highlighting only)
71- try {
72- final AntlrContext antlrContext = AntlrContext .fromInputFile (inf , charset );
73- ParseTreeItemVisitor visitor = new CustomTreeVisitor (new HighlighterVisitor ());
74- visitor .fillContext (sensorContext , antlrContext );
75- } catch (IOException e ) {
76- LOGGER .warn ("Unexpected error while analyzing file " + inf .filename (), e );
77- }
7880
81+ executorService .execute (() -> {
82+ // Visit test files (for syntax highlighting only)
83+ try {
84+ final AntlrContext antlrContext = AntlrContext .fromInputFile (inf , charset );
85+ ParseTreeItemVisitor visitor = new CustomTreeVisitor (new HighlighterVisitor ());
86+ visitor .fillContext (sensorContext , antlrContext );
87+ } catch (IOException e ) {
88+ LOGGER .warn ("Unexpected error while analyzing file " + inf .filename (), e );
89+ }
90+ });
91+
92+ }
93+
94+ try {
95+ executorService .shutdown ();
96+ executorService .awaitTermination (EXECUTOR_TIMEOUT , TimeUnit .SECONDS );
97+ executorService .shutdownNow ();
98+ } catch (final InterruptedException e ) {
99+ LOGGER .warn ("Unexpected error while running waiting for executor service to finish" , e );
79100 }
80101
81102 }
0 commit comments