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 org.openide.util.NbBundle;
34 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 = knownFilesType.getFileKnown();
246 Set<CentralRepoFileInstance> globalInstances =
new HashSet<>();
248 while (!hashSetParser.doneReading()) {
253 HashEntry newHash = hashSetParser.getNextHashEntry();
255 if (newHash != null) {
257 referenceSetID.get(),
258 newHash.getMd5Hash(),
260 newHash.getComment());
262 globalInstances.add(eamGlobalFileInstance);
266 if (hashCount.incrementAndGet() % HASH_IMPORT_THRESHOLD == 0) {
268 globalInstances.clear();
270 int progress = (int) (hashCount.get() * 100 / hashSetParser.getExpectedHashCount());
271 if (progress < 100) {
272 this.setProgress(progress);
274 this.setProgress(99);
282 this.setProgress(100);
285 hashSetParser.close();
290 if (referenceSetID.get() >= 0) {
297 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error deleting incomplete hash set from central repository", ex2);
304 synchronized protected void done() {
308 deleteIncompleteSet();
316 referenceSetID.get(),
317 searchDuringIngest, sendIngestMessages, knownFilesType, readOnly);
318 importSuccess.set(
true);
319 }
catch (TskCoreException ex) {
320 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error adding imported hash set", ex);
322 }
catch (Exception ex) {
324 deleteIncompleteSet();
325 Logger.
getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE,
"Error importing hash set", ex);
336 @SuppressWarnings(
"unchecked")
338 private
void initComponents() {
340 progressBar =
new javax.swing.JProgressBar();
341 lbProgress =
new javax.swing.JLabel();
342 bnOk =
new javax.swing.JButton();
343 bnCancel =
new javax.swing.JButton();
344 jLabel1 =
new javax.swing.JLabel();
346 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
348 org.openide.awt.Mnemonics.setLocalizedText(lbProgress,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.lbProgress.text"));
350 org.openide.awt.Mnemonics.setLocalizedText(bnOk,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.bnOk.text"));
351 bnOk.addActionListener(
new java.awt.event.ActionListener() {
352 public void actionPerformed(java.awt.event.ActionEvent evt) {
353 bnOkActionPerformed(evt);
357 org.openide.awt.Mnemonics.setLocalizedText(bnCancel,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.bnCancel.text"));
358 bnCancel.addActionListener(
new java.awt.event.ActionListener() {
359 public void actionPerformed(java.awt.event.ActionEvent evt) {
360 bnCancelActionPerformed(evt);
364 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(ImportCentralRepoDbProgressDialog.class,
"ImportCentralRepoDbProgressDialog.jLabel1.text"));
366 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
367 getContentPane().setLayout(layout);
368 layout.setHorizontalGroup(
369 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
370 .addGroup(layout.createSequentialGroup()
372 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
373 .addGroup(layout.createSequentialGroup()
374 .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
376 .addGroup(layout.createSequentialGroup()
377 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
378 .addComponent(jLabel1)
379 .addComponent(lbProgress))
380 .addGap(0, 0, Short.MAX_VALUE))))
381 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
382 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
384 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
385 .addComponent(bnCancel)
388 layout.setVerticalGroup(
389 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
390 .addGroup(layout.createSequentialGroup()
392 .addComponent(jLabel1)
393 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
394 .addComponent(lbProgress)
395 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
396 .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
397 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
398 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
399 .addComponent(bnCancel)
401 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
407 private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {
408 this.worker.cancel(
true);
412 private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {
418 private javax.swing.JButton bnCancel;
419 private javax.swing.JButton bnOk;
420 private javax.swing.JLabel jLabel1;
421 private javax.swing.JLabel lbProgress;
422 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