Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
EamDbSettingsDialog.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.optionspanel;
20 
21 import java.awt.Color;
22 import java.awt.Cursor;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.logging.Level;
28 import javax.swing.JDialog;
29 import javax.swing.JFileChooser;
30 import javax.swing.JFrame;
31 import javax.swing.JOptionPane;
32 import javax.swing.JTextField;
33 import javax.swing.SwingUtilities;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.event.DocumentListener;
36 import javax.swing.filechooser.FileFilter;
37 import org.netbeans.spi.options.OptionsPanelController;
38 import org.openide.util.NbBundle.Messages;
39 import org.openide.windows.WindowManager;
48 
52 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
53 public class EamDbSettingsDialog extends JDialog {
54 
55  private static final Logger logger = Logger.getLogger(EamDbSettingsDialog.class.getName());
56  private static final String CENTRAL_REPO_DB_NAME = "central_repository";
57  private static final String CENTRAL_REPO_SQLITE_EXT = ".db";
58  private static final long serialVersionUID = 1L;
59  private final Collection<JTextField> textBoxes;
61 
66  private boolean configurationChanged = false;
67 
71  @Messages({"EamDbSettingsDialog.title.text=Central Repository Database Configuration",
72  "EamDbSettingsDialog.lbSingleUserSqLite.text=SQLite should only be used by one examiner at a time.",
73  "EamDbSettingsDialog.lbDatabaseType.text=Database Type :",
74  "EamDbSettingsDialog.fcDatabasePath.title=Select location for central_repository.db"})
75 
77  super((JFrame) WindowManager.getDefault().getMainWindow(),
78  Bundle.EamDbSettingsDialog_title_text(),
79  true);
80 
81  textBoxes = new ArrayList<>();
82  textBoxChangedListener = new TextBoxChangedListener();
83  dbSettingsPostgres = new PostgresEamDbSettings();
84  dbSettingsSqlite = new SqliteEamDbSettings();
85  selectedPlatform = EamDbPlatformEnum.getSelectedPlatform();
86  if (selectedPlatform == null || selectedPlatform.equals(EamDbPlatformEnum.DISABLED)) {
87  selectedPlatform = EamDbPlatformEnum.POSTGRESQL;
88  }
89 
90  initComponents();
91  fcDatabasePath.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
92  fcDatabasePath.setAcceptAllFileFilterUsed(false);
93  fcDatabasePath.setDialogTitle(Bundle.EamDbSettingsDialog_fcDatabasePath_title());
94  fcDatabasePath.setFileFilter(new FileFilter() {
95  @Override
96  public boolean accept(File pathname) {
97  if (pathname.isDirectory()) {
98  return true;
99  }
100  return pathname.getName().toLowerCase().equals((CENTRAL_REPO_DB_NAME + CENTRAL_REPO_SQLITE_EXT).toLowerCase());
101  }
102 
103  @Override
104  public String getDescription() {
105  return "Directories and central repository databases";
106  }
107  });
108  cbDatabaseType.setSelectedItem(selectedPlatform);
109  customizeComponents();
110  valid();
111  display();
112 
113  }
114 
120  @SuppressWarnings("unchecked")
121  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
122  private void initComponents() {
123 
124  bnGrpDatabasePlatforms = new javax.swing.ButtonGroup();
125  fcDatabasePath = new javax.swing.JFileChooser();
126  pnButtons = new javax.swing.JPanel();
127  bnCancel = new javax.swing.JButton();
128  bnOk = new javax.swing.JButton();
129  pnSQLiteSettings = new javax.swing.JPanel();
130  lbDatabasePath = new javax.swing.JLabel();
131  tfDatabasePath = new javax.swing.JTextField();
132  bnDatabasePathFileOpen = new javax.swing.JButton();
133  lbHostName = new javax.swing.JLabel();
134  tbDbHostname = new javax.swing.JTextField();
135  lbPort = new javax.swing.JLabel();
136  tbDbPort = new javax.swing.JTextField();
137  lbUserName = new javax.swing.JLabel();
138  tbDbUsername = new javax.swing.JTextField();
139  lbUserPassword = new javax.swing.JLabel();
140  jpDbPassword = new javax.swing.JPasswordField();
141  cbDatabaseType = new javax.swing.JComboBox<>();
142  lbSingleUserSqLite = new javax.swing.JLabel();
143  lbDatabaseType = new javax.swing.JLabel();
144  lbDatabaseDesc = new javax.swing.JLabel();
145  lbFullDbPath = new javax.swing.JLabel();
146  filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
147 
148  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
149 
150  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnCancel.text")); // NOI18N
151  bnCancel.addActionListener(new java.awt.event.ActionListener() {
152  public void actionPerformed(java.awt.event.ActionEvent evt) {
153  bnCancelActionPerformed(evt);
154  }
155  });
156 
157  org.openide.awt.Mnemonics.setLocalizedText(bnOk, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnOk.text")); // NOI18N
158  bnOk.addActionListener(new java.awt.event.ActionListener() {
159  public void actionPerformed(java.awt.event.ActionEvent evt) {
160  bnOkActionPerformed(evt);
161  }
162  });
163 
164  javax.swing.GroupLayout pnButtonsLayout = new javax.swing.GroupLayout(pnButtons);
165  pnButtons.setLayout(pnButtonsLayout);
166  pnButtonsLayout.setHorizontalGroup(
167  pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
168  .addGroup(pnButtonsLayout.createSequentialGroup()
169  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
170  .addComponent(bnOk)
171  .addGap(11, 11, 11)
172  .addComponent(bnCancel)
173  .addContainerGap())
174  );
175  pnButtonsLayout.setVerticalGroup(
176  pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
177  .addGroup(pnButtonsLayout.createSequentialGroup()
178  .addGap(0, 0, 0)
179  .addGroup(pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
180  .addComponent(bnOk)
181  .addComponent(bnCancel))
182  .addGap(0, 0, 0))
183  );
184 
185  pnSQLiteSettings.setBorder(javax.swing.BorderFactory.createEtchedBorder());
186 
187  org.openide.awt.Mnemonics.setLocalizedText(lbDatabasePath, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabasePath.text")); // NOI18N
188 
189  tfDatabasePath.setText(org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.tfDatabasePath.text")); // NOI18N
190  tfDatabasePath.setToolTipText(org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.tfDatabasePath.toolTipText")); // NOI18N
191 
192  org.openide.awt.Mnemonics.setLocalizedText(bnDatabasePathFileOpen, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnDatabasePathFileOpen.text")); // NOI18N
193  bnDatabasePathFileOpen.addActionListener(new java.awt.event.ActionListener() {
194  public void actionPerformed(java.awt.event.ActionEvent evt) {
195  bnDatabasePathFileOpenActionPerformed(evt);
196  }
197  });
198 
199  org.openide.awt.Mnemonics.setLocalizedText(lbHostName, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbHostName.text")); // NOI18N
200 
201  org.openide.awt.Mnemonics.setLocalizedText(lbPort, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbPort.text")); // NOI18N
202 
203  org.openide.awt.Mnemonics.setLocalizedText(lbUserName, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbUserName.text")); // NOI18N
204 
205  org.openide.awt.Mnemonics.setLocalizedText(lbUserPassword, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbUserPassword.text")); // NOI18N
206 
207  cbDatabaseType.setModel(new javax.swing.DefaultComboBoxModel<>(new EamDbPlatformEnum[]{EamDbPlatformEnum.POSTGRESQL, EamDbPlatformEnum.SQLITE}));
208  cbDatabaseType.addActionListener(new java.awt.event.ActionListener() {
209  public void actionPerformed(java.awt.event.ActionEvent evt) {
210  cbDatabaseTypeActionPerformed(evt);
211  }
212  });
213 
214  org.openide.awt.Mnemonics.setLocalizedText(lbSingleUserSqLite, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbSingleUserSqLite.text")); // NOI18N
215 
216  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseType, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabaseType.text")); // NOI18N
217 
218  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseDesc, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabaseDesc.text")); // NOI18N
219 
220  org.openide.awt.Mnemonics.setLocalizedText(lbFullDbPath, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbFullDbPath.text")); // NOI18N
221 
222  javax.swing.GroupLayout pnSQLiteSettingsLayout = new javax.swing.GroupLayout(pnSQLiteSettings);
223  pnSQLiteSettings.setLayout(pnSQLiteSettingsLayout);
224  pnSQLiteSettingsLayout.setHorizontalGroup(
225  pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
226  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
227  .addContainerGap()
228  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229  .addComponent(lbHostName)
230  .addComponent(lbPort)
231  .addComponent(lbUserName)
232  .addComponent(lbDatabaseType)
233  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
234  .addComponent(lbDatabasePath, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
235  .addComponent(lbUserPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
236  .addComponent(lbDatabaseDesc))
237  .addGap(10, 10, 10)
238  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
239  .addComponent(lbFullDbPath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
240  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
241  .addComponent(cbDatabaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
242  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
243  .addComponent(lbSingleUserSqLite, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
244  .addGap(9, 9, 9))
245  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
246  .addComponent(tfDatabasePath)
247  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
248  .addComponent(bnDatabasePathFileOpen)
249  .addGap(11, 11, 11))
250  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnSQLiteSettingsLayout.createSequentialGroup()
251  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
252  .addComponent(tbDbHostname, javax.swing.GroupLayout.Alignment.LEADING)
253  .addComponent(jpDbPassword, javax.swing.GroupLayout.Alignment.LEADING)
254  .addComponent(tbDbUsername)
255  .addComponent(tbDbPort, javax.swing.GroupLayout.Alignment.LEADING))
256  .addGap(10, 10, 10))))
257  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
258  .addGap(55, 55, 55)
259  .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
260  .addGap(0, 0, Short.MAX_VALUE))
261  );
262  pnSQLiteSettingsLayout.setVerticalGroup(
263  pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
264  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
265  .addGap(6, 6, 6)
266  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
267  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
268  .addComponent(cbDatabaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
269  .addComponent(lbSingleUserSqLite))
270  .addComponent(lbDatabaseType, javax.swing.GroupLayout.Alignment.TRAILING))
271  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
272  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
273  .addComponent(lbDatabasePath)
274  .addComponent(tfDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
275  .addComponent(bnDatabasePathFileOpen))
276  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
277  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
278  .addComponent(tbDbHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
279  .addComponent(lbHostName))
280  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
281  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
282  .addComponent(tbDbPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
283  .addComponent(lbPort))
284  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
285  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
286  .addComponent(tbDbUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
287  .addComponent(lbUserName))
288  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
289  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
290  .addComponent(jpDbPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
291  .addComponent(lbUserPassword))
292  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
293  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
294  .addComponent(lbFullDbPath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
295  .addComponent(lbDatabaseDesc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
296  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
297  .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
298  .addContainerGap())
299  );
300 
301  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
302  getContentPane().setLayout(layout);
303  layout.setHorizontalGroup(
304  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
305  .addGroup(layout.createSequentialGroup()
306  .addContainerGap()
307  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
308  .addComponent(pnButtons, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
309  .addComponent(pnSQLiteSettings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
310  .addContainerGap())
311  );
312  layout.setVerticalGroup(
313  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
314  .addGroup(layout.createSequentialGroup()
315  .addGap(10, 10, 10)
316  .addComponent(pnSQLiteSettings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
317  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
318  .addComponent(pnButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
319  .addGap(10, 10, 10))
320  );
321 
322  pack();
323  }// </editor-fold>//GEN-END:initComponents
324 
325  private void customizeComponents() {
326  setTextPrompts();
327  setTextBoxListeners();
328  switch (selectedPlatform) {
329  case SQLITE:
330  testingStatus = DatabaseTestResult.UNTESTED;
331  updatePostgresFields(false);
332  updateSqliteFields(true);
333  break;
334  default:
335  POSTGRESQL:
336  testingStatus = DatabaseTestResult.UNTESTED;
337  updatePostgresFields(true);
338  updateSqliteFields(false);
339  break;
340 
341  }
342  displayDatabaseSettings(selectedPlatform.equals(EamDbPlatformEnum.POSTGRESQL));
343  }
344 
345  private void display() {
346  this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
347  setVisible(true);
348  }
349 
350  @Messages({"EamDbSettingsDialog.chooserPath.failedToGetDbPathMsg=Selected database path is invalid. Try again."})
351  private void bnDatabasePathFileOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDatabasePathFileOpenActionPerformed
352  fcDatabasePath.setSelectedFile(new File(dbSettingsSqlite.getDbDirectory()));
353  if (fcDatabasePath.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
354  File databaseFile = fcDatabasePath.getSelectedFile();
355  if (databaseFile.isFile()) {
356  databaseFile = fcDatabasePath.getCurrentDirectory();
357  }
358  try {
359  tfDatabasePath.setText(databaseFile.getCanonicalPath());
360  valid();
361  } catch (IOException ex) {
362  logger.log(Level.SEVERE, "Failed to get path of selected database file", ex); // NON-NLS
363  }
364  }
365  }//GEN-LAST:event_bnDatabasePathFileOpenActionPerformed
366 
367  private void testDbSettings() {
368  switch (selectedPlatform) {
369  case POSTGRESQL:
370  if (dbSettingsPostgres.verifyConnection()) {
371  if (dbSettingsPostgres.verifyDatabaseExists()) {
372  if( dbSettingsPostgres.verifyDatabaseSchema()) {
373  testingStatus = DatabaseTestResult.TESTEDOK;
374  } else {
375  testingStatus = DatabaseTestResult.SCHEMA_INVALID;
376  }
377  } else {
378  testingStatus = DatabaseTestResult.DB_DOES_NOT_EXIST;
379  }
380  } else {
381  testingStatus = DatabaseTestResult.CONNECTION_FAILED;
382  }
383  break;
384  case SQLITE:
385  if (dbSettingsSqlite.dbFileExists()){
386  if(dbSettingsSqlite.verifyConnection()) {
387  if (dbSettingsSqlite.verifyDatabaseSchema()) {
388  testingStatus = DatabaseTestResult.TESTEDOK;
389  } else {
390  testingStatus = DatabaseTestResult.SCHEMA_INVALID;
391  }
392  } else {
393  testingStatus = DatabaseTestResult.SCHEMA_INVALID;
394  }
395  } else {
396  testingStatus = DatabaseTestResult.DB_DOES_NOT_EXIST;
397  }
398  break;
399  }
400 
401  valid();
402  }
403 
404  @Messages({"EamDbSettingsDialog.okButton.createDbError.title=Unable to Create Database",
405  "EamDbSettingsDialog.okButton.createSQLiteDbError.message=Unable to create SQLite Database, please ensure location exists and you have write permissions and try again.",
406  "EamDbSettingsDialog.okButton.createPostgresDbError.message=Unable to create Postgres Database, please ensure address, port, and login credentials are correct for Postgres server and try again."})
407  private void createDb() {
408  boolean result = false;
409  boolean dbCreated = true;
410  switch (selectedPlatform) {
411  case POSTGRESQL:
412  if (!dbSettingsPostgres.verifyDatabaseExists()) {
413  dbCreated = dbSettingsPostgres.createDatabase();
414  }
415  if (dbCreated) {
416  result = dbSettingsPostgres.initializeDatabaseSchema()
417  && dbSettingsPostgres.insertDefaultDatabaseContent();
418  }
419  if (!result) {
420  // Remove the incomplete database
421  if(dbCreated){
422  dbSettingsPostgres.deleteDatabase();
423  }
424 
425  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
426  Bundle.EamDbSettingsDialog_okButton_createPostgresDbError_message(),
427  Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
428  JOptionPane.WARNING_MESSAGE);
429  logger.severe("Unable to initialize database schema or insert contents into central repository.");
430  return;
431  }
432  break;
433  case SQLITE:
434  if (!dbSettingsSqlite.dbDirectoryExists()) {
435  dbCreated = dbSettingsSqlite.createDbDirectory();
436  }
437  if (dbCreated) {
438  result = dbSettingsSqlite.initializeDatabaseSchema()
439  && dbSettingsSqlite.insertDefaultDatabaseContent();
440  }
441  if (!result) {
442  if(dbCreated){
443  dbSettingsSqlite.deleteDatabase();
444  }
445 
446  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
447  Bundle.EamDbSettingsDialog_okButton_createSQLiteDbError_message(),
448  Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
449  JOptionPane.WARNING_MESSAGE);
450  logger.severe("Unable to initialize database schema or insert contents into central repository.");
451  return;
452  }
453  break;
454  }
455  testingStatus = DatabaseTestResult.TESTEDOK;
456  valid();
457  }
463  boolean wasConfigurationChanged() {
464  return configurationChanged;
465  }
466 
467  @Messages({"EamDbSettingsDialog.okButton.errorTitle.text=Restart Required.",
468  "EamDbSettingsDialog.okButton.errorMsg.text=Please restart Autopsy to begin using the new database platform.",
469  "EamDbSettingsDialog.okButton.connectionErrorMsg.text=Failed to connect to central repository database.",
470  "EamDbSettingsDialog.okButton.corruptDatabaseExists.title=Error Loading Database",
471  "EamDbSettingsDialog.okButton.corruptDatabaseExists.message=Database exists but is not the right format. Manually delete it or choose a different path (if applicable).",
472  "EamDbSettingsDialog.okButton.createDbDialog.title=Database Does Not Exist",
473  "EamDbSettingsDialog.okButton.createDbDialog.message=Database does not exist, would you like to create it?",
474  "EamDbSettingsDialog.okButton.databaseConnectionFailed.title=Database Connection Failed",
475  "EamDbSettingsDialog.okButton.databaseConnectionFailed.message=Unable to connect to database please check your settings and try again."})
476  private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOkActionPerformed
477  setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
478  testDbSettings();
479  if (testingStatus == DatabaseTestResult.CONNECTION_FAILED) {
480  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
481  Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_message(),
482  Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_title(),
483  JOptionPane.WARNING_MESSAGE);
484  } else if (testingStatus == DatabaseTestResult.SCHEMA_INVALID){
485  // There's an existing database or file, but it's not in our format.
486  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
487  Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_message(),
488  Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_title(),
489  JOptionPane.WARNING_MESSAGE);
490  } else if (testingStatus == DatabaseTestResult.DB_DOES_NOT_EXIST) {
491  //database doesn't exist do you want to create
492  if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(),
493  Bundle.EamDbSettingsDialog_okButton_createDbDialog_message(),
494  Bundle.EamDbSettingsDialog_okButton_createDbDialog_title(),
495  JOptionPane.YES_NO_OPTION)) {
496  createDb();
497  }
498  }
499 
500  if (testingStatus != DatabaseTestResult.TESTEDOK) {
501  setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
502  return;
503  }
504 
513  try {
514  EamDb previousDbManager = EamDb.getInstance();
515  if (null != previousDbManager) {
516  // NOTE: do not set/save the seleted platform before calling this.
518  }
519  } catch (EamDbException ex) {
520  logger.log(Level.SEVERE, "Failed to close database connections in previously selected platform.", ex); // NON-NLS
521  SwingUtilities.invokeLater(() -> {
522  JOptionPane.showMessageDialog(this,
523  Bundle.EamDbSettingsDialog_okButton_errorMsg_text(),
524  Bundle.EamDbSettingsDialog_okButton_errorTitle_text(),
525  JOptionPane.WARNING_MESSAGE);
526  });
527  }
528 
529  // Even if we fail to close the existing connections, make sure that we
530  // save the new connection settings, so an Autopsy restart will correctly
531  // start with the new settings.
532  EamDbPlatformEnum.setSelectedPlatform(selectedPlatform.name());
534 
535  switch (selectedPlatform) {
536  case POSTGRESQL:
537  // save the new PostgreSQL settings
538  dbSettingsPostgres.saveSettings();
539  // Load those newly saved settings into the postgres db manager instance
540  // in case we are still using the same instance.
541  try {
543  configurationChanged = true;
544  } catch (EamDbException ex) {
545  logger.log(Level.SEVERE, Bundle.EamDbSettingsDialog_okButton_connectionErrorMsg_text(), ex); //NON-NLS
546  setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
547  return;
548  }
549 
550  break;
551  case SQLITE:
552  // save the new SQLite settings
553  dbSettingsSqlite.saveSettings();
554  // Load those newly saved settings into the sqlite db manager instance
555  // in case we are still using the same instance.
556  try {
558  configurationChanged = true;
559  } catch (EamDbException ex) {
560  logger.log(Level.SEVERE, Bundle.EamDbSettingsDialog_okButton_connectionErrorMsg_text(), ex); //NON-NLS
561  setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
562  return;
563  }
564  break;
565  case DISABLED:
566  break;
567  }
568 
569  setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
570  dispose();
571  }//GEN-LAST:event_bnOkActionPerformed
572 
573  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
574  dispose();
575  }//GEN-LAST:event_bnCancelActionPerformed
576 
577 
578  private void cbDatabaseTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDatabaseTypeActionPerformed
579  selectedPlatform = (EamDbPlatformEnum) cbDatabaseType.getSelectedItem();
580  customizeComponents();
581  }//GEN-LAST:event_cbDatabaseTypeActionPerformed
582 
583  private void updateFullDbPath(){
584  lbFullDbPath.setText(tfDatabasePath.getText() + File.separator + CENTRAL_REPO_DB_NAME + CENTRAL_REPO_SQLITE_EXT);
585  }
586 
587  private void displayDatabaseSettings(boolean isPostgres) {
588  lbDatabasePath.setVisible(!isPostgres);
589  tfDatabasePath.setVisible(!isPostgres);
590  lbDatabaseDesc.setVisible(!isPostgres);
591  lbFullDbPath.setVisible(!isPostgres);
592  lbSingleUserSqLite.setVisible(!isPostgres);
593  bnDatabasePathFileOpen.setVisible(!isPostgres);
594  lbHostName.setVisible(isPostgres);
595  tbDbHostname.setVisible(isPostgres);
596  lbPort.setVisible(isPostgres);
597  tbDbPort.setVisible(isPostgres);
598  lbUserName.setVisible(isPostgres);
599  tbDbUsername.setVisible(isPostgres);
600  lbUserPassword.setVisible(isPostgres);
601  jpDbPassword.setVisible(isPostgres);
602  }
603 
607  @Messages({"EamDbSettingsDialog.textPrompt.hostnameOrIP=Hostname or IP Address",
608  "EamDbSettingsDialog.textPrompt.port=Port Number",
609  "EamDbSettingsDialog.textPrompt.dbName=Database Name",
610  "EamDbSettingsDialog.textPrompt.user=Database User",
611  "EamDbSettingsDialog.textPrompt.password=Database User's Password"})
612  private void setTextPrompts() {
613  Collection<TextPrompt> textPrompts = new ArrayList<>();
614  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_hostnameOrIP(), tbDbHostname));
615  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_port(), tbDbPort));
616  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_user(), tbDbUsername));
617  configureTextPrompts(textPrompts);
618  }
619 
620  private void updatePostgresFields(boolean enabled) {
621  tbDbHostname.setText(enabled ? dbSettingsPostgres.getHost() : "");
622  tbDbHostname.setEnabled(enabled);
623  tbDbPort.setText(enabled ? Integer.toString(dbSettingsPostgres.getPort()) : "");
624  tbDbPort.setEnabled(enabled);
625  tbDbUsername.setText(enabled ? dbSettingsPostgres.getUserName() : "");
626  tbDbUsername.setEnabled(enabled);
627  jpDbPassword.setText(enabled ? dbSettingsPostgres.getPassword() : "");
628  jpDbPassword.setEnabled(enabled);
629  }
630 
637  private void updateSqliteFields(boolean enabled) {
638  tfDatabasePath.setText(enabled ? dbSettingsSqlite.getDbDirectory() : "");
639  tfDatabasePath.setEnabled(enabled);
640  bnDatabasePathFileOpen.setEnabled(enabled);
641  }
642 
646  private void setTextBoxListeners() {
647  textBoxes.add(tfDatabasePath);
648  textBoxes.add(tbDbHostname);
649  textBoxes.add(tbDbPort);
650  // textBoxes.add(tbDbName);
651  textBoxes.add(tbDbUsername);
652  textBoxes.add(jpDbPassword);
653  addDocumentListeners(textBoxes, textBoxChangedListener);
654  }
655 
662  private static void configureTextPrompts(Collection<TextPrompt> textPrompts) {
663  float alpha = 0.9f; // Mostly opaque
664  for (TextPrompt textPrompt : textPrompts) {
665  textPrompt.setForeground(Color.LIGHT_GRAY);
666  textPrompt.changeAlpha(alpha);
667  }
668  }
669 
676  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
677  textFields.forEach((textField) -> {
678  textField.getDocument().addDocumentListener(listener);
679  });
680  }
681 
688  @Messages({"EamDbSettingsDialog.validation.incompleteFields=Fill in all values for the selected database."})
689  private boolean databaseFieldsArePopulated() {
690  boolean result = true;
691  switch (selectedPlatform) {
692  case POSTGRESQL:
693  result = !tbDbHostname.getText().trim().isEmpty()
694  && !tbDbPort.getText().trim().isEmpty()
695  // && !tbDbName.getText().trim().isEmpty()
696  && !tbDbUsername.getText().trim().isEmpty()
697  && 0 < jpDbPassword.getPassword().length;
698 
699  break;
700 
701  case SQLITE:
702  result = !tfDatabasePath.getText().trim().isEmpty();
703  break;
704  }
705 
706  if (!result) {
707  }
708 
709  return result;
710  }
711 
717  private boolean checkFields() {
718  return databaseFieldsArePopulated()
719  && databaseSettingsAreValid();
720  }
721 
727  private boolean databaseSettingsAreValid() {
728  boolean result = true;
729  StringBuilder guidanceText = new StringBuilder();
730 
731  switch (selectedPlatform) {
732  case POSTGRESQL:
733  try {
734  dbSettingsPostgres.setHost(tbDbHostname.getText().trim());
735  } catch (EamDbException ex) {
736  guidanceText.append(ex.getMessage());
737  result = false;
738  }
739 
740  try {
741  dbSettingsPostgres.setPort(Integer.valueOf(tbDbPort.getText().trim()));
742  } catch (NumberFormatException | EamDbException ex) {
743  guidanceText.append(ex.getMessage());
744  result = false;
745  }
746 
747  try {
748  dbSettingsPostgres.setDbName(CENTRAL_REPO_DB_NAME);
749  } catch (EamDbException ex) {
750  guidanceText.append(ex.getMessage());
751  result = false;
752  }
753 
754  try {
755  dbSettingsPostgres.setUserName(tbDbUsername.getText().trim());
756  } catch (EamDbException ex) {
757  guidanceText.append(ex.getMessage());
758  result = false;
759  }
760 
761  try {
762  dbSettingsPostgres.setPassword(new String(jpDbPassword.getPassword()));
763  } catch (EamDbException ex) {
764  guidanceText.append(ex.getMessage());
765  result = false;
766  }
767  break;
768  case SQLITE:
769  try {
770  File databasePath = new File(tfDatabasePath.getText());
771  dbSettingsSqlite.setDbName(CENTRAL_REPO_DB_NAME + CENTRAL_REPO_SQLITE_EXT);
772  dbSettingsSqlite.setDbDirectory(databasePath.getPath());
773  } catch (EamDbException ex) {
774  guidanceText.append(ex.getMessage());
775  result = false;
776  }
777  break;
778  }
779 
780  return result;
781  }
782 
788  private boolean valid() {
789  return enableOkButton(checkFields());
790  }
791 
798  @Messages({"EamDbSettingsDialog.validation.finished=Click OK to save your database settings and return to the Options. Or select a different database type."})
799  private boolean enableOkButton(boolean isValidInput) {
800  if (isValidInput) {
801  bnOk.setEnabled(true);
802  } else {
803  bnOk.setEnabled(false);
804  }
805  return true;
806 
807  }
808 
813  private class TextBoxChangedListener implements DocumentListener {
814 
815  @Override
816  public void changedUpdate(DocumentEvent e) {
817  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
818  testingStatus = DatabaseTestResult.UNTESTED;
819  updateFullDbPath();
820  valid();
821  }
822 
823  @Override
824  public void insertUpdate(DocumentEvent e) {
825  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
826  testingStatus = DatabaseTestResult.UNTESTED;
827  updateFullDbPath();
828  valid();
829  }
830 
831  @Override
832  public void removeUpdate(DocumentEvent e) {
833  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
834  testingStatus = DatabaseTestResult.UNTESTED;
835  updateFullDbPath();
836  valid();
837 
838  }
839  }
840 
841  private enum DatabaseTestResult {
847  }
848 
849  // Variables declaration - do not modify//GEN-BEGIN:variables
850  private javax.swing.JButton bnCancel;
851  private javax.swing.JButton bnDatabasePathFileOpen;
852  private javax.swing.ButtonGroup bnGrpDatabasePlatforms;
853  private javax.swing.JButton bnOk;
854  private javax.swing.JComboBox<EamDbPlatformEnum> cbDatabaseType;
855  private javax.swing.JFileChooser fcDatabasePath;
856  private javax.swing.Box.Filler filler1;
857  private javax.swing.JPasswordField jpDbPassword;
858  private javax.swing.JLabel lbDatabaseDesc;
859  private javax.swing.JLabel lbDatabasePath;
860  private javax.swing.JLabel lbDatabaseType;
861  private javax.swing.JLabel lbFullDbPath;
862  private javax.swing.JLabel lbHostName;
863  private javax.swing.JLabel lbPort;
864  private javax.swing.JLabel lbSingleUserSqLite;
865  private javax.swing.JLabel lbUserName;
866  private javax.swing.JLabel lbUserPassword;
867  private javax.swing.JPanel pnButtons;
868  private javax.swing.JPanel pnSQLiteSettings;
869  private javax.swing.JTextField tbDbHostname;
870  private javax.swing.JTextField tbDbPort;
871  private javax.swing.JTextField tbDbUsername;
872  private javax.swing.JTextField tfDatabasePath;
873  // End of variables declaration//GEN-END:variables
874 }
static void addDocumentListeners(Collection< JTextField > textFields, TextBoxChangedListener listener)
static void configureTextPrompts(Collection< TextPrompt > textPrompts)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.