19 package org.sleuthkit.autopsy.modules.hashdatabase;
21 import java.awt.Color;
22 import java.beans.PropertyChangeListener;
23 import java.beans.PropertyChangeEvent;
24 import java.util.HashSet;
26 import java.util.logging.Level;
27 import javax.swing.JFrame;
28 import javax.swing.SwingWorker;
29 import javax.swing.WindowConstants;
30 import java.util.concurrent.atomic.AtomicLong;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.concurrent.Executors;
34 import org.openide.util.NbBundle;
35 import org.openide.windows.WindowManager;
48 @SuppressWarnings(
"PMD.SingularField")
49 class ImportCentralRepoDbProgressDialog extends javax.swing.JDialog implements PropertyChangeListener {
51 private static final long serialVersionUID = 1L;
53 private CentralRepoImportWorker worker;
55 @NbBundle.Messages({
"ImportCentralRepoDbProgressDialog.title.text=Central Repository Import Progress",})
56 ImportCentralRepoDbProgressDialog() {
57 super((JFrame) WindowManager.getDefault().getMainWindow(),
58 Bundle.ImportCentralRepoDbProgressDialog_title_text(),
62 customizeComponents();
65 private void customizeComponents() {
67 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
69 bnOk.setEnabled(
false);
85 void importFile(String hashSetName, String version,
int orgId,
86 boolean searchDuringIngest,
boolean sendIngestMessages, HashDbManager.HashDb.KnownFilesType knownFilesType,
87 boolean readOnly, String importFileName) {
89 worker =
new CentralRepoImportWorker(hashSetName, version, orgId, searchDuringIngest, sendIngestMessages,
90 knownFilesType, readOnly, importFileName);
91 worker.addPropertyChangeListener(
this);
94 setLocationRelativeTo((JFrame) WindowManager.getDefault().getMainWindow());
95 this.setVisible(
true);
104 HashDbManager.HashDb getDatabase() {
105 if (worker != null) {
106 return worker.getDatabase();
117 @NbBundle.Messages({
"ImportCentralRepoDbProgressDialog.errorParsingFile.message=Error parsing hash set file"})
119 public void propertyChange(PropertyChangeEvent evt) {
121 if (
"progress".equals(evt.getPropertyName())) {
123 progressBar.setValue(worker.getProgress());
124 lbProgress.setText(getProgressString());
125 }
else if (
"state".equals(evt.getPropertyName())
126 && (SwingWorker.StateValue.DONE.equals(evt.getNewValue()))) {
130 bnCancel.setEnabled(
false);
131 bnOk.setEnabled(
true);
133 if (worker.getImportSuccess()) {
136 progressBar.setValue(progressBar.getMaximum());
137 lbProgress.setText(getProgressString());
140 progressBar.setValue(0);
141 lbProgress.setForeground(Color.red);
142 lbProgress.setText(Bundle.ImportCentralRepoDbProgressDialog_errorParsingFile_message());
147 @NbBundle.Messages({
"ImportCentralRepoDbProgressDialog.linesProcessed.message= hashes processed"})
148 private String getProgressString() {
149 return worker.getNumHashesProcessed() + Bundle.ImportCentralRepoDbProgressDialog_linesProcessed_message();
154 private final int HASH_IMPORT_THRESHOLD = 10000;
164 private final AtomicInteger referenceSetID =
new AtomicInteger();
165 private final AtomicLong hashCount =
new AtomicLong();
166 private final AtomicBoolean importSuccess =
new AtomicBoolean();
170 boolean readOnly, String importFileName) {
172 this.hashSetName = hashSetName;
173 this.version = version;
175 this.searchDuringIngest = searchDuringIngest;
176 this.sendIngestMessages = sendIngestMessages;
177 this.knownFilesType = knownFilesType;
178 this.readOnly = readOnly;
179 this.importFileName = importFileName;
180 this.hashCount.set(0);
181 this.importSuccess.set(
false);
182 this.referenceSetID.set(-1);
200 long getNumHashesProcessed() {
201 return hashCount.get();
210 boolean getImportSuccess() {
211 return importSuccess.get();
218 HashSetParser hashSetParser;
219 if (importFileName.toLowerCase().endsWith(
".idx") || importFileName.toLowerCase().endsWith(
".txt")) {
220 hashSetParser =
new IdxHashSetParser(importFileName);
221 }
else if (importFileName.toLowerCase().endsWith(
".hash")) {
222 hashSetParser =
new EncaseHashSetParser(importFileName);
223 }
else if (importFileName.toLowerCase().endsWith(
".kdb")) {
225 }
else if (importFileName.toLowerCase().endsWith(
".hsh")) {
229 throw new TskCoreException(
"Hash set to import is an unknown format : " + importFileName);
234 TskData.FileKnown knownStatus;
236 knownStatus = TskData.FileKnown.KNOWN;
238 knownStatus = TskData.FileKnown.BAD;
251 Set<EamGlobalFileInstance> globalInstances =
new HashSet<>();
253 while (!hashSetParser.doneReading()) {
258 String newHash = hashSetParser.getNextHash();
260 if (newHash != null) {
262 referenceSetID.get(),
267 globalInstances.add(eamGlobalFileInstance);
271 if (hashCount.incrementAndGet() % HASH_IMPORT_THRESHOLD == 0) {
273 globalInstances.clear();
275 int progress = (int) (hashCount.get() * 100 / hashSetParser.getExpectedHashCount());
276 if (progress < 100) {
277 this.setProgress(progress);
279 this.setProgress(99);
287 this.setProgress(100);
290 hashSetParser.close();
295 if (referenceSetID.get() >= 0) {
298 Executors.newSingleThreadExecutor().execute(
new Runnable() {
304 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error deleting incomplete hash set from central repository", ex2);
312 synchronized protected void done() {
316 deleteIncompleteSet();
324 referenceSetID.get(),
325 searchDuringIngest, sendIngestMessages, knownFilesType, readOnly);
326 importSuccess.set(
true);
327 }
catch (TskCoreException ex) {
328 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error adding imported hash set", ex);
330 }
catch (Exception ex) {
332 deleteIncompleteSet();
333 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error importing hash set", ex);
344 @SuppressWarnings(
"unchecked")
346 private
void initComponents() {
348 progressBar =
new javax.swing.JProgressBar();
349 lbProgress =
new javax.swing.JLabel();
350 bnOk =
new javax.swing.JButton();
351 bnCancel =
new javax.swing.JButton();
352 jLabel1 =
new javax.swing.JLabel();
354 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
356 org.openide.awt.Mnemonics.setLocalizedText(lbProgress,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.lbProgress.text"));
358 org.openide.awt.Mnemonics.setLocalizedText(bnOk,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.bnOk.text"));
359 bnOk.addActionListener(
new java.awt.event.ActionListener() {
360 public void actionPerformed(java.awt.event.ActionEvent evt) {
361 bnOkActionPerformed(evt);
365 org.openide.awt.Mnemonics.setLocalizedText(bnCancel,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.bnCancel.text"));
366 bnCancel.addActionListener(
new java.awt.event.ActionListener() {
367 public void actionPerformed(java.awt.event.ActionEvent evt) {
368 bnCancelActionPerformed(evt);
372 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.jLabel1.text"));
374 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
375 getContentPane().setLayout(layout);
376 layout.setHorizontalGroup(
377 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
378 .addGroup(layout.createSequentialGroup()
380 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
381 .addGroup(layout.createSequentialGroup()
382 .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
384 .addGroup(layout.createSequentialGroup()
385 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
386 .addComponent(jLabel1)
387 .addComponent(lbProgress))
388 .addGap(0, 0, Short.MAX_VALUE))))
389 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
390 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
392 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
393 .addComponent(bnCancel)
396 layout.setVerticalGroup(
397 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
398 .addGroup(layout.createSequentialGroup()
400 .addComponent(jLabel1)
401 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
402 .addComponent(lbProgress)
403 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
404 .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
405 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
406 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
407 .addComponent(bnCancel)
409 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
415 private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {
416 this.worker.cancel(
true);
420 private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {
426 private javax.swing.JButton bnCancel;
427 private javax.swing.JButton bnOk;
428 private javax.swing.JLabel jLabel1;
429 private javax.swing.JLabel lbProgress;
430 private javax.swing.JProgressBar progressBar;
void deleteReferenceSet(int referenceSetID)
int newReferenceSet(EamGlobalSet eamGlobalSet)
final boolean sendIngestMessages
void bulkInsertReferenceTypeEntries(Set< EamGlobalFileInstance > globalInstances, CorrelationAttributeInstance.Type contentType)
final HashDbManager.HashDb.KnownFilesType knownFilesType
static EamDb getInstance()
static synchronized HashDbManager getInstance()
CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId)
void deleteIncompleteSet()
final String importFileName
synchronized static Logger getLogger(String name)
static final int FILES_TYPE_ID
final boolean searchDuringIngest