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;
49 @SuppressWarnings(
"PMD.SingularField")
50 class ImportCentralRepoDbProgressDialog extends javax.swing.JDialog implements PropertyChangeListener {
52 private static final long serialVersionUID = 1L;
54 private CentralRepoImportWorker worker;
56 @NbBundle.Messages({
"ImportCentralRepoDbProgressDialog.title.text=Central Repository Import Progress",})
57 ImportCentralRepoDbProgressDialog() {
58 super((JFrame) WindowManager.getDefault().getMainWindow(),
59 Bundle.ImportCentralRepoDbProgressDialog_title_text(),
63 customizeComponents();
66 private void customizeComponents() {
68 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
70 bnOk.setEnabled(
false);
86 void importFile(String hashSetName, String version,
int orgId,
87 boolean searchDuringIngest,
boolean sendIngestMessages, HashDbManager.HashDb.KnownFilesType knownFilesType,
88 boolean readOnly, String importFileName) {
90 worker =
new CentralRepoImportWorker(hashSetName, version, orgId, searchDuringIngest, sendIngestMessages,
91 knownFilesType, readOnly, importFileName);
92 worker.addPropertyChangeListener(
this);
95 setLocationRelativeTo((JFrame) WindowManager.getDefault().getMainWindow());
96 this.setVisible(
true);
105 HashDbManager.HashDb getDatabase() {
106 if (worker != null) {
107 return worker.getDatabase();
118 @NbBundle.Messages({
"ImportCentralRepoDbProgressDialog.errorParsingFile.message=Error parsing hash set file"})
120 public void propertyChange(PropertyChangeEvent evt) {
122 if (
"progress".equals(evt.getPropertyName())) {
124 progressBar.setValue(worker.getProgress());
125 lbProgress.setText(getProgressString());
126 }
else if (
"state".equals(evt.getPropertyName())
127 && (SwingWorker.StateValue.DONE.equals(evt.getNewValue()))) {
131 bnCancel.setEnabled(
false);
132 bnOk.setEnabled(
true);
134 if (worker.getImportSuccess()) {
137 progressBar.setValue(progressBar.getMaximum());
138 lbProgress.setText(getProgressString());
141 progressBar.setValue(0);
142 lbProgress.setForeground(Color.red);
143 lbProgress.setText(Bundle.ImportCentralRepoDbProgressDialog_errorParsingFile_message());
148 @NbBundle.Messages({
"ImportCentralRepoDbProgressDialog.linesProcessed.message= hashes processed"})
149 private String getProgressString() {
150 return worker.getNumHashesProcessed() + Bundle.ImportCentralRepoDbProgressDialog_linesProcessed_message();
155 private final int HASH_IMPORT_THRESHOLD = 10000;
165 private final AtomicInteger referenceSetID =
new AtomicInteger();
166 private final AtomicLong hashCount =
new AtomicLong();
167 private final AtomicBoolean importSuccess =
new AtomicBoolean();
171 boolean readOnly, String importFileName) {
173 this.hashSetName = hashSetName;
174 this.version = version;
176 this.searchDuringIngest = searchDuringIngest;
177 this.sendIngestMessages = sendIngestMessages;
178 this.knownFilesType = knownFilesType;
179 this.readOnly = readOnly;
180 this.importFileName = importFileName;
181 this.hashCount.set(0);
182 this.importSuccess.set(
false);
183 this.referenceSetID.set(-1);
201 long getNumHashesProcessed() {
202 return hashCount.get();
211 boolean getImportSuccess() {
212 return importSuccess.get();
219 HashSetParser hashSetParser;
220 if (importFileName.toLowerCase().endsWith(
".idx") || importFileName.toLowerCase().endsWith(
".txt")) {
221 hashSetParser =
new IdxHashSetParser(importFileName);
222 }
else if (importFileName.toLowerCase().endsWith(
".hash")) {
223 hashSetParser =
new EncaseHashSetParser(importFileName);
224 }
else if (importFileName.toLowerCase().endsWith(
".kdb")) {
226 }
else if (importFileName.toLowerCase().endsWith(
".hsh")) {
230 throw new TskCoreException(
"Hash set to import is an unknown format : " + importFileName);
235 TskData.FileKnown knownStatus = knownFilesType.getFileKnown();
247 Set<CentralRepoFileInstance> globalInstances =
new HashSet<>();
249 while (!hashSetParser.doneReading()) {
254 HashEntry newHash = hashSetParser.getNextHashEntry();
256 if (newHash != null) {
258 referenceSetID.get(),
259 newHash.getMd5Hash(),
261 newHash.getComment());
263 globalInstances.add(eamGlobalFileInstance);
267 if (hashCount.incrementAndGet() % HASH_IMPORT_THRESHOLD == 0) {
269 globalInstances.clear();
271 int progress = (int) (hashCount.get() * 100 / hashSetParser.getExpectedHashCount());
272 if (progress < 100) {
273 this.setProgress(progress);
275 this.setProgress(99);
283 this.setProgress(100);
286 hashSetParser.close();
291 if (referenceSetID.get() >= 0) {
294 Executors.newSingleThreadExecutor().execute(
new Runnable() {
300 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error deleting incomplete hash set from central repository", ex2);
308 synchronized protected void done() {
312 deleteIncompleteSet();
320 referenceSetID.get(),
321 searchDuringIngest, sendIngestMessages, knownFilesType, readOnly);
322 importSuccess.set(
true);
323 }
catch (TskCoreException ex) {
324 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error adding imported hash set", ex);
326 }
catch (Exception ex) {
328 deleteIncompleteSet();
329 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error importing hash set", ex);
340 @SuppressWarnings(
"unchecked")
342 private
void initComponents() {
344 progressBar =
new javax.swing.JProgressBar();
345 lbProgress =
new javax.swing.JLabel();
346 bnOk =
new javax.swing.JButton();
347 bnCancel =
new javax.swing.JButton();
348 jLabel1 =
new javax.swing.JLabel();
350 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
352 org.openide.awt.Mnemonics.setLocalizedText(lbProgress,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.lbProgress.text"));
354 org.openide.awt.Mnemonics.setLocalizedText(bnOk,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.bnOk.text"));
355 bnOk.addActionListener(
new java.awt.event.ActionListener() {
356 public void actionPerformed(java.awt.event.ActionEvent evt) {
357 bnOkActionPerformed(evt);
361 org.openide.awt.Mnemonics.setLocalizedText(bnCancel,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.bnCancel.text"));
362 bnCancel.addActionListener(
new java.awt.event.ActionListener() {
363 public void actionPerformed(java.awt.event.ActionEvent evt) {
364 bnCancelActionPerformed(evt);
368 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.jLabel1.text"));
370 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
371 getContentPane().setLayout(layout);
372 layout.setHorizontalGroup(
373 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
374 .addGroup(layout.createSequentialGroup()
376 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
377 .addGroup(layout.createSequentialGroup()
378 .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
380 .addGroup(layout.createSequentialGroup()
381 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
382 .addComponent(jLabel1)
383 .addComponent(lbProgress))
384 .addGap(0, 0, Short.MAX_VALUE))))
385 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
386 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
388 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
389 .addComponent(bnCancel)
392 layout.setVerticalGroup(
393 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
394 .addGroup(layout.createSequentialGroup()
396 .addComponent(jLabel1)
397 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
398 .addComponent(lbProgress)
399 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
400 .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
401 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
402 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
403 .addComponent(bnCancel)
405 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
411 private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {
412 this.worker.cancel(
true);
416 private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {
422 private javax.swing.JButton bnCancel;
423 private javax.swing.JButton bnOk;
424 private javax.swing.JLabel jLabel1;
425 private javax.swing.JLabel lbProgress;
426 private javax.swing.JProgressBar progressBar;
final boolean sendIngestMessages
void bulkInsertReferenceTypeEntries(Set< CentralRepoFileInstance > globalInstances, CorrelationAttributeInstance.Type contentType)
void deleteReferenceSet(int referenceSetID)
final HashDbManager.HashDb.KnownFilesType knownFilesType
int newReferenceSet(CentralRepoFileSet eamGlobalSet)
static synchronized HashDbManager getInstance()
void deleteIncompleteSet()
final String importFileName
synchronized static Logger getLogger(String name)
CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId)
static CentralRepository getInstance()
static final int FILES_TYPE_ID
final boolean searchDuringIngest