From 0949fb13d12f93723ddcd55b2a185bcd5ec57e74 Mon Sep 17 00:00:00 2001 From: Tim McIver Date: Wed, 28 Nov 2012 11:14:17 -0500 Subject: [PATCH] Updated Services and FileManager to implement the java.io.Closeable interface and updated FileManager to throw TskCoreException when its API is used after it has been closed. Closing is propagated to all services when the Case object is closed. These changes are in support of AUT-613. --- .../sleuthkit/autopsy/casemodule/Case.java | 1 + .../casemodule/services/FileManager.java | 30 +++++++++++++++---- .../autopsy/casemodule/services/Services.java | 21 ++++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java index 429e85bf37..3bb87f96b6 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java @@ -360,6 +360,7 @@ public class Case { try { this.xmlcm.close(); // close the xmlcm this.db.close(); + services.close(); } catch (Exception e) { throw new CaseActionException("Error while trying to close the current case.", e); } diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/services/FileManager.java b/Core/src/org/sleuthkit/autopsy/casemodule/services/FileManager.java index 2fb61bd002..786370e9d2 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/services/FileManager.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/services/FileManager.java @@ -4,6 +4,9 @@ */ package org.sleuthkit.autopsy.casemodule.services; +import java.io.Closeable; +import java.io.IOException; +import java.util.Collections; import java.util.List; import org.sleuthkit.datamodel.FsContent; import org.sleuthkit.datamodel.SleuthkitCase; @@ -12,7 +15,7 @@ import org.sleuthkit.datamodel.TskCoreException; /** * Abstraction to facilitate access to files and directories. */ -public class FileManager { +public class FileManager implements Closeable { private SleuthkitCase tskCase; @@ -26,7 +29,10 @@ public class FileManager { * given fileName */ public List findFiles(String fileName) throws TskCoreException { - return tskCase.findFiles(fileName); + if (tskCase == null) { + throw new TskCoreException("Attemtped to use FileManager after it was closed."); + } + return tskCase.findFiles(fileName); } /** @@ -36,7 +42,10 @@ public class FileManager { * fileName and whose parent directory contains dirName. */ public List findFiles(String fileName, String dirName) throws TskCoreException { - return tskCase.findFiles(fileName, dirName); + if (tskCase == null) { + throw new TskCoreException("Attemtped to use FileManager after it was closed."); + } + return tskCase.findFiles(fileName, dirName); } /** @@ -46,7 +55,10 @@ public class FileManager { * fileName and that were inside a directory described by parentFsContent. */ public List findFiles(String fileName, FsContent parentFsContent) throws TskCoreException { - return findFiles(fileName, parentFsContent.getName()); + if (tskCase == null) { + throw new TskCoreException("Attemtped to use FileManager after it was closed."); + } + return findFiles(fileName, parentFsContent.getName()); } /** @@ -55,7 +67,15 @@ public class FileManager { * @return a list of FsContent that have the given file path. */ public List openFiles(String filePath) throws TskCoreException { - return tskCase.openFiles(filePath); + if (tskCase == null) { + throw new TskCoreException("Attemtped to use FileManager after it was closed."); + } + return tskCase.openFiles(filePath); } + + @Override + public void close() throws IOException { + tskCase = null; + } } diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/services/Services.java b/Core/src/org/sleuthkit/autopsy/casemodule/services/Services.java index af16268207..d1844b7c7f 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/services/Services.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/services/Services.java @@ -4,15 +4,25 @@ */ package org.sleuthkit.autopsy.casemodule.services; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.sleuthkit.datamodel.SleuthkitCase; /** * * @author mciver */ -public class Services { +public class Services implements Closeable { private SleuthkitCase tskCase; + + // NOTE: all new services added to Services class must be added to this list + // of services. + List services = new ArrayList(); + + // services private FileManager fileManager; public Services(SleuthkitCase tskCase) { @@ -22,7 +32,16 @@ public class Services { public FileManager getFileManager() { if (fileManager == null) { fileManager = new FileManager(tskCase); + services.add(fileManager); } return fileManager; } + + @Override + public void close() throws IOException { + // close all services + for (Closeable service : services) { + service.close(); + } + } }