Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SwingWorkerSequentialExecutor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.Future;
27 import java.util.stream.Collectors;
28 import javax.swing.SwingWorker;
29 
38 
39  private final ExecutorService executorService = Executors.newFixedThreadPool(1);
40  private List<? extends SwingWorker<?, ?>> workers = Collections.emptyList();
41  private List<Future<?>> futures = Collections.emptyList();
42 
49  public synchronized void submit(List<? extends SwingWorker<?, ?>> submittedWorkers) {
50  // cancel currently running operations
51  cancelRunning();
52 
53  // if no workers, there is nothing to run
54  if (submittedWorkers == null) {
55  return;
56  }
57 
58  this.workers = new ArrayList<>(submittedWorkers);
59 
60  // start running the workers and capture the futures if there is a need to cancel them.
61  this.futures = this.workers.stream()
62  .map((w) -> executorService.submit(w))
63  .collect(Collectors.toList());
64  }
65 
69  public synchronized void cancelRunning() {
70  futures.forEach((f) -> f.cancel(true));
71  workers = Collections.emptyList();
72  futures = Collections.emptyList();
73  }
74 
80  public synchronized boolean isRunning() {
81  // borrowed from this stack overflow answer:
82  // https://stackoverflow.com/a/33845730
83 
84  for (Future<?> future : futures) {
85  if (!future.isDone()) {
86  return true;
87  }
88  }
89 
90  return false;
91  }
92 }
synchronized void submit(List<?extends SwingWorker<?,?>> submittedWorkers)

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.