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.
This commit is contained in:
Tim McIver 2012-11-28 11:14:17 -05:00
parent 3824f90a0e
commit 0949fb13d1
3 changed files with 46 additions and 6 deletions

View File

@ -360,6 +360,7 @@ public class Case {
try { try {
this.xmlcm.close(); // close the xmlcm this.xmlcm.close(); // close the xmlcm
this.db.close(); this.db.close();
services.close();
} catch (Exception e) { } catch (Exception e) {
throw new CaseActionException("Error while trying to close the current case.", e); throw new CaseActionException("Error while trying to close the current case.", e);
} }

View File

@ -4,6 +4,9 @@
*/ */
package org.sleuthkit.autopsy.casemodule.services; package org.sleuthkit.autopsy.casemodule.services;
import java.io.Closeable;
import java.io.IOException;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.sleuthkit.datamodel.FsContent; import org.sleuthkit.datamodel.FsContent;
import org.sleuthkit.datamodel.SleuthkitCase; import org.sleuthkit.datamodel.SleuthkitCase;
@ -12,7 +15,7 @@ import org.sleuthkit.datamodel.TskCoreException;
/** /**
* Abstraction to facilitate access to files and directories. * Abstraction to facilitate access to files and directories.
*/ */
public class FileManager { public class FileManager implements Closeable {
private SleuthkitCase tskCase; private SleuthkitCase tskCase;
@ -26,6 +29,9 @@ public class FileManager {
* given fileName * given fileName
*/ */
public List<FsContent> findFiles(String fileName) throws TskCoreException { public List<FsContent> findFiles(String fileName) throws TskCoreException {
if (tskCase == null) {
throw new TskCoreException("Attemtped to use FileManager after it was closed.");
}
return tskCase.findFiles(fileName); return tskCase.findFiles(fileName);
} }
@ -36,6 +42,9 @@ public class FileManager {
* fileName and whose parent directory contains dirName. * fileName and whose parent directory contains dirName.
*/ */
public List<FsContent> findFiles(String fileName, String dirName) throws TskCoreException { public List<FsContent> findFiles(String fileName, String dirName) throws TskCoreException {
if (tskCase == null) {
throw new TskCoreException("Attemtped to use FileManager after it was closed.");
}
return tskCase.findFiles(fileName, dirName); return tskCase.findFiles(fileName, dirName);
} }
@ -46,6 +55,9 @@ public class FileManager {
* fileName and that were inside a directory described by parentFsContent. * fileName and that were inside a directory described by parentFsContent.
*/ */
public List<FsContent> findFiles(String fileName, FsContent parentFsContent) throws TskCoreException { public List<FsContent> findFiles(String fileName, FsContent parentFsContent) throws TskCoreException {
if (tskCase == null) {
throw new TskCoreException("Attemtped to use FileManager after it was closed.");
}
return findFiles(fileName, parentFsContent.getName()); return findFiles(fileName, parentFsContent.getName());
} }
@ -55,7 +67,15 @@ public class FileManager {
* @return a list of FsContent that have the given file path. * @return a list of FsContent that have the given file path.
*/ */
public List<FsContent> openFiles(String filePath) throws TskCoreException { public List<FsContent> openFiles(String filePath) throws TskCoreException {
if (tskCase == null) {
throw new TskCoreException("Attemtped to use FileManager after it was closed.");
}
return tskCase.openFiles(filePath); return tskCase.openFiles(filePath);
} }
@Override
public void close() throws IOException {
tskCase = null;
}
} }

View File

@ -4,15 +4,25 @@
*/ */
package org.sleuthkit.autopsy.casemodule.services; 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; import org.sleuthkit.datamodel.SleuthkitCase;
/** /**
* *
* @author mciver * @author mciver
*/ */
public class Services { public class Services implements Closeable {
private SleuthkitCase tskCase; private SleuthkitCase tskCase;
// NOTE: all new services added to Services class must be added to this list
// of services.
List<Closeable> services = new ArrayList<Closeable>();
// services
private FileManager fileManager; private FileManager fileManager;
public Services(SleuthkitCase tskCase) { public Services(SleuthkitCase tskCase) {
@ -22,7 +32,16 @@ public class Services {
public FileManager getFileManager() { public FileManager getFileManager() {
if (fileManager == null) { if (fileManager == null) {
fileManager = new FileManager(tskCase); fileManager = new FileManager(tskCase);
services.add(fileManager);
} }
return fileManager; return fileManager;
} }
@Override
public void close() throws IOException {
// close all services
for (Closeable service : services) {
service.close();
}
}
} }