View Javadoc
1   /**
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.decima.core.assessment;
28  
29  import gov.nist.secauto.decima.core.assessment.result.AssessmentResultBuilder;
30  import gov.nist.secauto.decima.core.document.Document;
31  
32  import org.apache.logging.log4j.LogManager;
33  import org.apache.logging.log4j.Logger;
34  
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Objects;
38  import java.util.Set;
39  import java.util.concurrent.Callable;
40  import java.util.concurrent.CompletionService;
41  import java.util.concurrent.ExecutionException;
42  import java.util.concurrent.Executor;
43  import java.util.concurrent.ExecutorCompletionService;
44  import java.util.concurrent.Future;
45  
46  public class ConcurrentAssessmentExecutor<DOC extends Document>
47      extends AbstractAssessmentExecutor<DOC> {
48    private static final Logger log = LogManager.getLogger(ConcurrentAssessmentExecutor.class);
49    private final Executor executor;
50  
51    /**
52     * Constructs a new AssessmentExecutor that is capable of executing multiple assessments
53     * Concurrently, using the provided Executor to execute the provided assessments.
54     * 
55     * @param executor
56     *          the executor to use to execute the assessment tasks
57     * @param assessments
58     *          the assessments to perform
59     */
60    public ConcurrentAssessmentExecutor(Executor executor, List<? extends Assessment<DOC>> assessments) {
61      super(assessments);
62      Objects.requireNonNull(executor, "executor");
63      this.executor = executor;
64    }
65  
66    public Executor getExecutor() {
67      return executor;
68    }
69  
70    @Override
71    protected final void executeInternal(DOC targetDocument, AssessmentResultBuilder builder) throws AssessmentException {
72      CompletionService<Void> completionService = new ExecutorCompletionService<>(executor);
73      Set<Future<Void>> futures = new HashSet<>();
74      for (Assessment<DOC> assessment : getExecutableAssessments(targetDocument)) {
75        log.info("Submitting assessment for execution: " + assessment.getName(true));
76        futures.add(completionService.submit(new AssessmentTask(assessment, targetDocument, builder)));
77      }
78  
79      try {
80        while (!futures.isEmpty()) {
81          Future<Void> future = completionService.take();
82          futures.remove(future);
83          future.get();
84        }
85      } catch (InterruptedException e) {
86        throw new AssessmentException("the assessment execution was interrupted", e);
87      } catch (ExecutionException e) {
88        if (e.getCause() instanceof AssessmentException) {
89          throw (AssessmentException) e.getCause();
90        }
91      } finally {
92        for (Future<Void> future : futures) {
93          future.cancel(true);
94        }
95      }
96    }
97  
98    private class AssessmentTask implements Callable<Void> {
99      private final Assessment<DOC> assessment;
100     private final DOC documentToAssess;
101     private final AssessmentResultBuilder builder;
102 
103     public AssessmentTask(Assessment<DOC> assessment, DOC documentToAssess, AssessmentResultBuilder builder) {
104       Objects.requireNonNull(assessment, "assessment");
105       Objects.requireNonNull(documentToAssess, "documentToAssess");
106       Objects.requireNonNull(builder, "builder");
107       this.assessment = assessment;
108       this.documentToAssess = documentToAssess;
109       this.builder = builder;
110     }
111 
112     @Override
113     public Void call() throws AssessmentException {
114       AssessmentExecutionHelper.executeAssessment(assessment, documentToAssess, builder);
115       return null;
116     }
117 
118   }
119 
120 }