revisions based on feedback

This commit is contained in:
Greg DiCristofaro 2020-08-27 19:26:18 -04:00
parent 47d200d190
commit dd5f2b4e40
7 changed files with 53 additions and 115 deletions

View File

@ -34,15 +34,6 @@ abstract class BaseDataSourceSummaryPanel extends JPanel {
private final SwingWorkerSequentialExecutor executor = new SwingWorkerSequentialExecutor(); private final SwingWorkerSequentialExecutor executor = new SwingWorkerSequentialExecutor();
private DataSource dataSource; private DataSource dataSource;
/**
* The datasource currently used as the model in this panel.
*
* @return The datasource currently being used as the model in this panel.
*/
synchronized DataSource getDataSource() {
return dataSource;
}
/** /**
* Sets datasource to visualize in the panel. * Sets datasource to visualize in the panel.
* *
@ -52,6 +43,7 @@ abstract class BaseDataSourceSummaryPanel extends JPanel {
DataSource oldDataSource = this.dataSource; DataSource oldDataSource = this.dataSource;
this.dataSource = dataSource; this.dataSource = dataSource;
if (this.dataSource != oldDataSource) { if (this.dataSource != oldDataSource) {
this.executor.cancelRunning();
onNewDataSource(this.dataSource); onNewDataSource(this.dataSource);
} }
} }

View File

@ -159,7 +159,7 @@ public class DataSourceSummaryUserActivityPanel extends BaseDataSourceSummaryPan
// set results for tables to null. // set results for tables to null.
if (dataSource == null || !Case.isCaseOpen()) { if (dataSource == null || !Case.isCaseOpen()) {
this.dataFetchComponents.forEach((item) -> item.getResultHandler() this.dataFetchComponents.forEach((item) -> item.getResultHandler()
.accept(DataFetchResult.getLoadedResult(null))); .accept(DataFetchResult.getSuccessResult(null)));
} else { } else {
// set tables to display loading screen // set tables to display loading screen

View File

@ -19,52 +19,52 @@
package org.sleuthkit.autopsy.datasourcesummary.uiutils; package org.sleuthkit.autopsy.datasourcesummary.uiutils;
/** /**
* The intermediate or end result of a loading process. * The result of a loading process.
*/ */
public final class DataFetchResult<R> { public final class DataFetchResult<R> {
/** /**
* The state of loading in the result. * The type of result.
*/ */
public enum ProcessorState { public enum ResultType {
LOADED, LOAD_ERROR SUCCESS, ERROR
} }
/** /**
* Creates a DataLoadingResult of loaded data including the data. * Creates a DataFetchResult of loaded data including the data.
* *
* @param data The data. * @param data The data.
* *
* @return The loaded data result. * @return The loaded data result.
*/ */
public static <R> DataFetchResult<R> getLoadedResult(R data) { public static <R> DataFetchResult<R> getSuccessResult(R data) {
return new DataFetchResult<>(ProcessorState.LOADED, data, null); return new DataFetchResult<>(ResultType.SUCCESS, data, null);
} }
/** /**
* Returns a load error result. * Returns an error result.
* *
* @param e The exception (if any) present with the error. * @param e The exception (if any) present with the error.
* *
* @return * @return The error result.
*/ */
public static <R> DataFetchResult<R> getLoadErrorResult(DataFetcherException e) { public static <R> DataFetchResult<R> getErrorResult(Throwable e) {
return new DataFetchResult<>(ProcessorState.LOAD_ERROR, null, e); return new DataFetchResult<>(ResultType.ERROR, null, e);
} }
private final ProcessorState state; private final ResultType state;
private final R data; private final R data;
private final DataFetcherException exception; private final Throwable exception;
/** /**
* Main constructor for the DataLoadingResult. * Main constructor for the DataLoadingResult.
* *
* @param state The state of the result. * @param state The state of the result.
* @param data If the result is LOADED, the data related to this * @param data If the result is SUCCESS, the data related to this
* result. * result.
* @param exception If the result is LOAD_ERROR, the related exception. * @param exception If the result is ERROR, the related exception.
*/ */
private DataFetchResult(ProcessorState state, R data, DataFetcherException exception) { private DataFetchResult(ResultType state, R data, Throwable exception) {
this.state = state; this.state = state;
this.data = data; this.data = data;
this.exception = exception; this.exception = exception;
@ -73,21 +73,21 @@ public final class DataFetchResult<R> {
/** /**
* @return The current loading state. * @return The current loading state.
*/ */
public ProcessorState getState() { public ResultType getResultType() {
return state; return state;
} }
/** /**
* @return The data if the state is LOADED. * @return The data if the state is SUCCESS.
*/ */
public R getData() { public R getData() {
return data; return data;
} }
/** /**
* @return The exception if the state is LOAD_ERROR. * @return The exception if the state is ERROR.
*/ */
public DataFetcherException getException() { public Throwable getException() {
return exception; return exception;
} }
} }

View File

@ -25,39 +25,40 @@ import javax.swing.SwingWorker;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
/** /**
* A Swing worker that accepts an argument of a data processor and a result * A Swing worker that accepts an argument of a data fetcher and a result
* handler. * handler. If the data fetcher throws an InterruptedException, it is treated as
* a cancellation and not passed to the result handler.
*/ */
public class DataFetchWorker<A, R> extends SwingWorker<R, Void> { public class DataFetchWorker<A, R> extends SwingWorker<R, Void> {
/** /**
* Holds the functions necessary for a DataFetchWorker. Includes the * Holds the functions necessary for a DataFetchWorker. Includes the fetcher
* processor and result handler. The args are not included since they are * and result handler. The args are not included since they are likely
* likely dynamic. * dynamic.
*/ */
public static class DataFetchComponents<A1, R1> { public static class DataFetchComponents<A1, R1> {
private final DataFetcher<A1, R1> processor; private final DataFetcher<A1, R1> fetcher;
private final Consumer<DataFetchResult<R1>> resultHandler; private final Consumer<DataFetchResult<R1>> resultHandler;
/** /**
* Main constructor. * Main constructor.
* *
* @param processor The processor to be used as an argument for the * @param fetcher The fetcher to be used as an argument for the
* DataFetchWorker. * DataFetchWorker.
* @param resultHandler The result handler to be used as an argument for * @param resultHandler The result handler to be used as an argument for
* the DataFetchWorker. * the DataFetchWorker.
*/ */
public DataFetchComponents(DataFetcher<A1, R1> processor, Consumer<DataFetchResult<R1>> resultHandler) { public DataFetchComponents(DataFetcher<A1, R1> fetcher, Consumer<DataFetchResult<R1>> resultHandler) {
this.processor = processor; this.fetcher = fetcher;
this.resultHandler = resultHandler; this.resultHandler = resultHandler;
} }
/** /**
* @return The function that processes or fetches the data. * @return The function that fetches the data.
*/ */
public DataFetcher<A1, R1> getProcessor() { public DataFetcher<A1, R1> getFetcher() {
return processor; return fetcher;
} }
/** /**
@ -83,14 +84,16 @@ public class DataFetchWorker<A, R> extends SwingWorker<R, Void> {
* @param args The argument to be provided to the data processor. * @param args The argument to be provided to the data processor.
*/ */
public DataFetchWorker(DataFetchComponents<A, R> components, A args) { public DataFetchWorker(DataFetchComponents<A, R> components, A args) {
this(components.getProcessor(), components.getResultHandler(), args); this(components.getFetcher(), components.getResultHandler(), args);
} }
/** /**
* Main constructor for this swing worker. * Main constructor for this swing worker.
* *
* @param processor The function that will do the processing of the data * @param processor The function that will do the fetching of the data
* provided the given args. * provided the given args. InterruptedException's are
* treated as cancellations and are not passed to the
* result handler.
* @param resultHandler The ui function that will handle the result of the * @param resultHandler The ui function that will handle the result of the
* data processing. * data processing.
* @param args The args provided to the data processor. * @param args The args provided to the data processor.
@ -133,9 +136,8 @@ public class DataFetchWorker<A, R> extends SwingWorker<R, Void> {
// otherwise, there is an error to log // otherwise, there is an error to log
logger.log(Level.WARNING, "There was an error while fetching results.", ex); logger.log(Level.WARNING, "There was an error while fetching results.", ex);
if (inner instanceof DataFetcherException) { // and pass the result to the client
resultHandler.accept(DataFetchResult.getLoadErrorResult((DataFetcherException) inner)); resultHandler.accept(DataFetchResult.getErrorResult(inner));
}
return; return;
} }
@ -145,6 +147,6 @@ public class DataFetchWorker<A, R> extends SwingWorker<R, Void> {
} }
// if the data is loaded, send the data to the consumer. // if the data is loaded, send the data to the consumer.
resultHandler.accept(DataFetchResult.getLoadedResult(result)); resultHandler.accept(DataFetchResult.getSuccessResult(result));
} }
} }

View File

@ -21,25 +21,23 @@ package org.sleuthkit.autopsy.datasourcesummary.uiutils;
/** /**
* A function that accepts input of type I and outputs type O. This function is * A function that accepts input of type I and outputs type O. This function is
* meant to be utilized with DataFetchWorker and can therefore, throw an * meant to be utilized with DataFetchWorker and can therefore, throw an
* interrupted exception if the processing is cancelled or a * interrupted exception if the processing is cancelled or an Exception of on
* DataProcessorException in the event that the processing encountered an error. * another type in the event that the fetching encountered an error.
*/ */
@FunctionalInterface @FunctionalInterface
public interface DataFetcher<I, O> { public interface DataFetcher<I, O> {
/** /**
* A function that accepts an input argument and outputs a result. Since it * A function that accepts an input argument and outputs a result. Since it
* is meant to be used with the DataFetchWorker, it throws an interrupted * is meant to be used with the DataFetchWorker, it may throw an interrupted
* exception if the thread has been interrupted. It throws a data processing * exception if the thread has been interrupted. It throws another type of
* exception if there is an error during processing. * exception if there is an error during fetching.
* *
* @param input The input argument. * @param input The input argument.
* *
* @return The output result. * @return The output result.
* *
* @throws InterruptedException Thrown if the operation is cancelled. * @throws Exception
* @throws DataFetcherException Thrown if there is an issue processing the
* request.
*/ */
O runQuery(I input) throws InterruptedException, DataFetcherException; O runQuery(I input) throws Exception;
} }

View File

@ -1,47 +0,0 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2020 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.autopsy.datasourcesummary.uiutils;
/**
* An Exception that is thrown when there is an issue processing data in a
* DataProcessor.
*/
public class DataFetcherException extends Exception {
private static final long serialVersionUID = 1L;
/**
* Main constructor.
*
* @param string The error message.
*/
public DataFetcherException(String string) {
super(string);
}
/**
* Main constructor.
*
* @param string The error message.
* @param thrwbl The inner exception.
*/
public DataFetcherException(String string, Throwable thrwbl) {
super(string, thrwbl);
}
}

View File

@ -90,13 +90,6 @@ public class JTablePanel<T> extends JPanel {
this.visible = visible; this.visible = visible;
} }
/**
* @return The child JLabel component.
*/
JLabel getLabel() {
return label;
}
/** /**
* Sets the message to be displayed in the child jlabel. * Sets the message to be displayed in the child jlabel.
* *
@ -391,15 +384,15 @@ public class JTablePanel<T> extends JPanel {
return; return;
} }
switch (result.getState()) { switch (result.getResultType()) {
case LOADED: case SUCCESS:
if (result.getData() == null || result.getData().isEmpty()) { if (result.getData() == null || result.getData().isEmpty()) {
showMessage(noResultsMessage); showMessage(noResultsMessage);
} else { } else {
showResults(result.getData()); showResults(result.getData());
} }
break; break;
case LOAD_ERROR: case ERROR:
// if there is an error, log accordingly, set result list to // if there is an error, log accordingly, set result list to
// empty and display error message // empty and display error message
logger.log(Level.WARNING, "An exception was caused while results were loaded.", result.getException()); logger.log(Level.WARNING, "An exception was caused while results were loaded.", result.getException());