30 package org.sleuthkit.autopsy.examples;
 
   32 import java.util.HashMap;
 
   33 import java.util.logging.Level;
 
   45 import org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
 
   56 class SampleFileIngestModule 
implements FileIngestModule {
 
   58     private static final HashMap<Long, Long> artifactCountsForIngestJobs = 
new HashMap<>();
 
   59     private static BlackboardAttribute.ATTRIBUTE_TYPE attrType = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COUNT;
 
   60     private final boolean skipKnownFiles;
 
   61     private IngestJobContext context = null;
 
   62     private static final IngestModuleReferenceCounter refCounter = 
new IngestModuleReferenceCounter();
 
   64     SampleFileIngestModule(SampleModuleIngestJobSettings settings) {
 
   65         this.skipKnownFiles = settings.skipKnownFiles();
 
   69     public void startUp(IngestJobContext context) 
throws IngestModuleException {
 
   70         this.context = context;
 
   71         refCounter.incrementAndGet(context.getJobId());
 
   75     public IngestModule.ProcessResult process(AbstractFile file) {
 
   78         if ((file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
 
   79                 || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)
 
   80                 || (file.isFile() == 
false)) {
 
   81             return IngestModule.ProcessResult.OK;
 
   85         if (skipKnownFiles && file.getKnown() == TskData.FileKnown.KNOWN) {
 
   86             return IngestModule.ProcessResult.OK;
 
   93             byte buffer[] = 
new byte[1024];
 
   94             int len = file.read(buffer, 0, 1024);
 
   96             for (
int i = 0; i < len; i++) {
 
   97                 if (buffer[i] == 0x00) {
 
  104             BlackboardAttribute attr = 
new BlackboardAttribute(attrType, SampleIngestModuleFactory.getModuleName(), count);
 
  109             BlackboardArtifact art = file.getGenInfoArtifact();
 
  110             art.addAttribute(attr);
 
  114             addToBlackboardPostCount(context.getJobId(), 1L);
 
  117             ModuleDataEvent 
event = 
new ModuleDataEvent(SampleIngestModuleFactory.getModuleName(), ARTIFACT_TYPE.TSK_GEN_INFO);
 
  118             IngestServices.getInstance().fireModuleDataEvent(event);
 
  120             return IngestModule.ProcessResult.OK;
 
  122         } 
catch (TskCoreException ex) {
 
  123             IngestServices ingestServices = IngestServices.getInstance();
 
  124             Logger logger = ingestServices.getLogger(SampleIngestModuleFactory.getModuleName());
 
  125             logger.log(Level.SEVERE, 
"Error processing file (id = " + file.getId() + 
")", ex);
 
  126             return IngestModule.ProcessResult.ERROR;
 
  131     public void shutDown() {
 
  134         reportBlackboardPostCount(context.getJobId());
 
  137     synchronized static void addToBlackboardPostCount(
long ingestJobId, 
long countToAdd) {
 
  138         Long fileCount = artifactCountsForIngestJobs.get(ingestJobId);
 
  141         if (fileCount == null) {
 
  143             artifactCountsForIngestJobs.put(ingestJobId, fileCount);
 
  146         fileCount += countToAdd;
 
  147         artifactCountsForIngestJobs.put(ingestJobId, fileCount);
 
  150     synchronized static void reportBlackboardPostCount(
long ingestJobId) {
 
  151         Long refCount = refCounter.decrementAndGet(ingestJobId);
 
  153             Long filesCount = artifactCountsForIngestJobs.remove(ingestJobId);
 
  154             String msgText = String.format(
"Posted %d times to the blackboard", filesCount);
 
  155             IngestMessage message = IngestMessage.createMessage(
 
  156                     IngestMessage.MessageType.INFO,
 
  157                     SampleIngestModuleFactory.getModuleName(),
 
  159             IngestServices.getInstance().postMessage(message);