Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DataFetchWorker.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 
22 import java.util.concurrent.ExecutionException;
23 import java.util.function.Consumer;
24 import javax.swing.SwingWorker;
26 
32 public class DataFetchWorker<A, R> extends SwingWorker<R, Void> {
33 
39  public static class DataFetchComponents<A1, R1> {
40 
42  private final Consumer<DataFetchResult<R1>> resultHandler;
43 
52  public DataFetchComponents(DataFetcher<A1, R1> fetcher, Consumer<DataFetchResult<R1>> resultHandler) {
53  this.fetcher = fetcher;
54  this.resultHandler = resultHandler;
55  }
56 
61  return fetcher;
62  }
63 
68  public Consumer<DataFetchResult<R1>> getResultHandler() {
69  return resultHandler;
70  }
71  }
72 
73  private static final Logger logger = Logger.getLogger(DataFetchWorker.class.getName());
74  private static final int MAX_INNER_EXCEPTION_DEPTH = 100;
75 
76  private final A args;
78  private final Consumer<DataFetchResult<R>> resultHandler;
79 
87  public DataFetchWorker(DataFetchComponents<A, R> components, A args) {
88  this(components.getFetcher(), components.getResultHandler(), args);
89  }
90 
103  DataFetcher<A, R> processor,
104  Consumer<DataFetchResult<R>> resultHandler,
105  A args) {
106 
107  this.args = args;
108  this.processor = processor;
109  this.resultHandler = resultHandler;
110  }
111 
112  @Override
113  protected R doInBackground() throws Exception {
114  return processor.runQuery(args);
115  }
116 
117  @Override
118  protected void done() {
119  // if cancelled, simply return
120  if (Thread.interrupted() || isCancelled()) {
121  return;
122  }
123 
124  R result = null;
125  try {
126  result = get();
127  } catch (InterruptedException ignored) {
128  // if cancelled, simply return
129  return;
130  } catch (ExecutionException ex) {
131  Throwable inner = ex.getCause();
132  for (int i = 0; i < MAX_INNER_EXCEPTION_DEPTH; i++) {
133  if (inner == null) {
134  break;
135  } else if (inner instanceof InterruptedException) {
136  // if cancelled during operation, simply return
137  return;
138  } else {
139  inner = inner.getCause();
140  }
141  }
142 
143  // and pass the result to the client
144  resultHandler.accept(DataFetchResult.getErrorResult(ex.getCause()));
145  return;
146  }
147 
148  // if cancelled, simply return
149  if (Thread.interrupted() || isCancelled()) {
150  return;
151  }
152 
153  // if the data is loaded, send the data to the consumer.
154  resultHandler.accept(DataFetchResult.getSuccessResult(result));
155  }
156 }
DataFetchComponents(DataFetcher< A1, R1 > fetcher, Consumer< DataFetchResult< R1 >> resultHandler)
static< R > DataFetchResult< R > getErrorResult(Throwable e)
DataFetchWorker(DataFetcher< A, R > processor, Consumer< DataFetchResult< R >> resultHandler, A args)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
DataFetchWorker(DataFetchComponents< A, R > components, A args)
static< R > DataFetchResult< R > getSuccessResult(R data)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.