Skip to content

Commit

Permalink
#916 threaded rules validation
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyk committed Jul 28, 2018
1 parent 98d4052 commit be80eb7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
97 changes: 92 additions & 5 deletions qulice-maven-plugin/src/main/java/com/qulice/maven/CheckMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,24 @@
*/
package com.qulice.maven;

import com.jcabi.aspects.Tv;
import com.jcabi.log.Logger;
import com.qulice.spi.ResourceValidator;
import com.qulice.spi.ValidationException;
import com.qulice.spi.Validator;
import com.qulice.spi.Violation;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -53,6 +62,12 @@
requiresDependencyResolution = ResolutionScope.TEST)
public final class CheckMojo extends AbstractQuliceMojo {

/**
* Executors for validators.
*/
private final ExecutorService executors =
Executors.newFixedThreadPool(Tv.FIVE);

/**
* Provider of validators.
*/
Expand Down Expand Up @@ -85,17 +100,25 @@ public void setValidatorsProvider(final ValidatorsProvider prov) {
* @throws ValidationException If any of them fail
*/
private void run() throws ValidationException {
final Collection<Violation> results = new LinkedList<>();
final LinkedList<Violation> results = new LinkedList<>();
final MavenEnvironment env = this.env();
final Collection<File> files = env.files("*.*");
if (!files.isEmpty()) {
final Collection<ResourceValidator> validators =
this.provider.externalResource();
for (final ResourceValidator validator : validators) {
results.addAll(
validator.validate(CheckMojo.filter(env, files, validator))
);
final Collection<Future<Collection<Violation>>> futures =
this.submit(env, files, validators);
for (final Future<Collection<Violation>> future : futures) {
try {
results.addAll(future.get(Tv.TEN, TimeUnit.MINUTES));
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException(ex);
} catch (final ExecutionException | TimeoutException ex) {
throw new IllegalStateException(ex);
}
}
Collections.sort(results);
for (final Violation result : results) {
Logger.info(
this,
Expand Down Expand Up @@ -128,6 +151,29 @@ private void run() throws ValidationException {
}
}

/**
* Submit validators to executor.
* @param env Maven environment
* @param files List of files to validate
* @param validators Validators to use
* @return List of futures
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private Collection<Future<Collection<Violation>>> submit(
final MavenEnvironment env, final Collection<File> files,
final Collection<ResourceValidator> validators) {
final Collection<Future<Collection<Violation>>> futures =
new LinkedList<>();
for (final ResourceValidator validator : validators) {
futures.add(
this.executors.submit(
new ValidatorCallable(validator, env, files)
)
);
}
return futures;
}

/**
* Filter files based on excludes.
* @param env Maven environment
Expand All @@ -150,4 +196,45 @@ private static Collection<File> filter(final MavenEnvironment env,
}
return filtered;
}

/**
* Callable for validators.
*/
private static class ValidatorCallable
implements Callable<Collection<Violation>> {
/**
* Validator to use.
*/
private final ResourceValidator validator;

/**
* Maven environment.
*/
private final MavenEnvironment env;

/**
* List of files to validate.
*/
private final Collection<File> files;

/**
* Constructor.
* @param validator Validator to use
* @param env Maven environment
* @param files List of files to validate
*/
ValidatorCallable(final ResourceValidator validator,
final MavenEnvironment env, final Collection<File> files) {
this.validator = validator;
this.env = env;
this.files = files;
}

@Override
public Collection<Violation> call() {
return this.validator.validate(
CheckMojo.filter(this.env, this.files, this.validator)
);
}
}
}
7 changes: 6 additions & 1 deletion qulice-spi/src/main/java/com/qulice/spi/Violation.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
* Validation result.
* @since 0.17
*/
public interface Violation {
@SuppressWarnings("PMD.TooManyMethods")
public interface Violation extends Comparable<Violation> {

/**
* Name of the validator that generated this violation information.
Expand Down Expand Up @@ -143,6 +144,10 @@ public String message() {
return this.msg;
}

@Override
public int compareTo(final Violation other) {
return this.vldtr.compareToIgnoreCase(other.validator());
}
}

}

0 comments on commit be80eb7

Please sign in to comment.