Autopsy  4.16.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 
21 import java.util.concurrent.ExecutionException;
22 import java.util.function.Consumer;
23 import javax.swing.SwingWorker;
25 
31 public class DataFetchWorker<A, R> extends SwingWorker<R, Void> {
32 
38  public static class DataFetchComponents<A1, R1> {
39 
41  private final Consumer<DataFetchResult<R1>> resultHandler;
42 
51  public DataFetchComponents(DataFetcher<A1, R1> fetcher, Consumer<DataFetchResult<R1>> resultHandler) {
52  this.fetcher = fetcher;
53  this.resultHandler = resultHandler;
54  }
55 
60  return fetcher;
61  }
62 
67  public Consumer<DataFetchResult<R1>> getResultHandler() {
68  return resultHandler;
69  }
70  }
71 
72  private static final Logger logger = Logger.getLogger(DataFetchWorker.class.getName());
73  private static final int MAX_INNER_EXCEPTION_DEPTH = 100;
74 
75  private final A args;
77  private final Consumer<DataFetchResult<R>> resultHandler;
78 
86  public DataFetchWorker(DataFetchComponents<A, R> components, A args) {
87  this(components.getFetcher(), components.getResultHandler(), args);
88  }
89 
102  DataFetcher<A, R> processor,
103  Consumer<DataFetchResult<R>> resultHandler,
104  A args) {
105 
106  this.args = args;
107  this.processor = processor;
108  this.resultHandler = resultHandler;
109  }
110 
111  @Override
112  protected R doInBackground() throws Exception {
113  return processor.runQuery(args);
114  }
115 
116  @Override
117  protected void done() {
118  // if cancelled, simply return
119  if (Thread.interrupted() || isCancelled()) {
120  return;
121  }
122 
123  R result = null;
124  try {
125  result = get();
126  } catch (InterruptedException ignored) {
127  // if cancelled, simply return
128  return;
129  } catch (ExecutionException ex) {
130  Throwable inner = ex.getCause();
131  for (int i = 0; i < MAX_INNER_EXCEPTION_DEPTH; i++) {
132  if (inner == null) {
133  break;
134  } else if (inner instanceof InterruptedException) {
135  // if cancelled during operation, simply return
136  return;
137  } else {
138  inner = inner.getCause();
139  }
140  }
141 
142  // and pass the result to the client
143  resultHandler.accept(DataFetchResult.getErrorResult(inner));
144  return;
145  }
146 
147  // if cancelled, simply return
148  if (Thread.interrupted() || isCancelled()) {
149  return;
150  }
151 
152  // if the data is loaded, send the data to the consumer.
153  resultHandler.accept(DataFetchResult.getSuccessResult(result));
154  }
155 }
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-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.