From a197acb18b2c1092af8ceecdf34f877cacac6a58 Mon Sep 17 00:00:00 2001 From: Eugene Livis Date: Fri, 9 Oct 2015 13:15:39 -0400 Subject: [PATCH] Modified Case class to be TSK error observer --- .../sleuthkit/autopsy/casemodule/Case.java | 15 ++++++-- .../casemodule/IntervalErrorReportData.java | 34 ++++++++----------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java index fbbd7f424a..c82c2c114b 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java @@ -86,12 +86,12 @@ import org.sleuthkit.datamodel.TskException; * open at a time. Use getCurrentCase() to retrieve the object for the current * case. */ -public class Case { +public class Case implements SleuthkitCase.ErrorObserver { private static final String autopsyVer = Version.getVersion(); // current version of autopsy. Change it when the version is changed private static final String EVENT_CHANNEL_NAME = "%s-Case-Events"; private static String appName = null; - private IntervalErrorReportData tskErrorReporter = null; + volatile private IntervalErrorReportData tskErrorReporter = null; private static final int MIN_SECONDS_BETWEEN_ERROR_REPORTS = 60; // No less than 60 seconds between warnings for errors private static final int MAX_SANITIZED_NAME_LENGTH = 47; @@ -371,6 +371,17 @@ public class Case { Logger.setLogDirectory(PlatformUtil.getLogDirectory()); } } + + @Override + public void receiveError(String context, String errorMessage) { + /* NOTE: We are accessing currentCase.tskErrorReporter from two different threads. + * This is ok as long as we only read the value of currentCase.tskErrorReporter + * because currentCase.tskErrorReporter is declared as volatile. + */ + if (null != currentCase.tskErrorReporter) { + currentCase.tskErrorReporter.addProblems(context, errorMessage); + } + } AddImageProcess makeAddImageProcess(String timezone, boolean processUnallocSpace, boolean noFatOrphans) { return this.db.makeAddImageProcess(timezone, processUnallocSpace, noFatOrphans); diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/IntervalErrorReportData.java b/Core/src/org/sleuthkit/autopsy/casemodule/IntervalErrorReportData.java index eb71f8d8d8..52ea056333 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/IntervalErrorReportData.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/IntervalErrorReportData.java @@ -20,16 +20,15 @@ package org.sleuthkit.autopsy.casemodule; import org.openide.util.NbBundle; import org.sleuthkit.autopsy.coreutils.MessageNotifyUtil; -import org.sleuthkit.datamodel.SleuthkitCase; /** * This class enables capturing errors and batching them for reporting on a - * no-more-than-x number of seconds basis. When created, you specify what type - * of error it will be batching, and the minimum time between user - * notifications. When the time between notifications has expired, the next - * error encountered will cause a report to be shown to the user. + * no-more-than-x number of seconds basis. When created, you specify the minimum + * time between user notifications. When the time between notifications has + * expired, the next error encountered will cause a report to be shown to the + * user. */ -class IntervalErrorReportData implements SleuthkitCase.ErrorObserver { +class IntervalErrorReportData { private final Case currentCase; private long newProblems; @@ -39,7 +38,8 @@ class IntervalErrorReportData implements SleuthkitCase.ErrorObserver { private final String message; /** - * Create a new IntervalErrorReprotData instance. + * Create a new IntervalErrorReprotData instance and subscribe for TSK error + * notifications for the current case. * * @param currentCase Case for which TSK errors should be tracked * and displayed. @@ -48,21 +48,21 @@ class IntervalErrorReportData implements SleuthkitCase.ErrorObserver { * @param message The message that will be shown when warning * the user */ - public IntervalErrorReportData(Case currentCase, int secondsBetweenReports, String message) { + IntervalErrorReportData(Case currentCase, int secondsBetweenReports, String message) { this.newProblems = 0; this.totalProblems = 0; this.lastReportedDate = 0; // arm the first warning by choosing zero this.milliSecondsBetweenReports = secondsBetweenReports * 1000; // convert to milliseconds this.message = message; this.currentCase = currentCase; - this.currentCase.getSleuthkitCase().addErrorObserver(this); // it's ok to use "this" at the end of the constructor + this.currentCase.getSleuthkitCase().addErrorObserver(this.currentCase); } /** - * Shuts down this IntervalErrorReprotData instance. + * Un-subscribe from TSK error notifications for current case. */ - public void shutdown() { - this.currentCase.getSleuthkitCase().removeErrorObserver(this); + void shutdown() { + this.currentCase.getSleuthkitCase().removeErrorObserver(this.currentCase); } /** @@ -70,12 +70,11 @@ class IntervalErrorReportData implements SleuthkitCase.ErrorObserver { * (or if this is the first problem encountered), a warning will be shown to * the user. * - * @param newProblems the newProblems to set * @param context The context in which the error occurred. * @param errorMessage A description of the error that occurred. */ - private void addProblems(long newProblems, String context, String errorMessage) { - this.newProblems += newProblems; + void addProblems(String context, String errorMessage) { + this.newProblems += 1; this.totalProblems += newProblems; long currentTimeStamp = System.currentTimeMillis(); @@ -90,9 +89,4 @@ class IntervalErrorReportData implements SleuthkitCase.ErrorObserver { this.newProblems = 0; } } - - @Override - public void receiveError(String context, String errorMessage) { - addProblems(1, context, errorMessage); - } }