19 package org.sleuthkit.autopsy.casemodule;
21 import java.awt.Color;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.nio.file.Paths;
29 import java.util.concurrent.atomic.AtomicBoolean;
30 import java.util.logging.Level;
31 import javax.swing.JFrame;
32 import javax.swing.SwingWorker;
33 import org.openide.modules.InstalledFileLocator;
34 import org.openide.util.NbBundle;
35 import org.openide.windows.WindowManager;
43 @SuppressWarnings(
"PMD.SingularField")
44 class UnpackagePortableCaseProgressDialog extends javax.swing.JDialog implements PropertyChangeListener {
46 private UnpackageWorker worker;
51 @NbBundle.Messages({
"UnpackagePortableCaseProgressDialog.title.text=Unpackage Portable Case Progress",})
52 UnpackagePortableCaseProgressDialog() {
53 super((JFrame) WindowManager.getDefault().getMainWindow(),
54 Bundle.UnpackagePortableCaseProgressDialog_title_text(),
57 customizeComponents();
60 private void customizeComponents() {
61 cancelButton.setEnabled(
true);
62 okButton.setEnabled(
false);
63 progressBar.setIndeterminate(
true);
64 resultLabel.setText(
"");
73 void unpackageCase(String packagedCase, String outputFolder) {
75 worker =
new UnpackageWorker(packagedCase, outputFolder);
76 worker.addPropertyChangeListener(
this);
79 setLocationRelativeTo((JFrame) WindowManager.getDefault().getMainWindow());
80 this.setVisible(
true);
93 return worker.isSuccess();
97 @NbBundle.Messages({
"UnpackagePortableCaseProgressDialog.propertyChange.success=Successfully unpacked case",})
99 public void propertyChange(PropertyChangeEvent evt) {
101 if (
"state".equals(evt.getPropertyName())
102 && (SwingWorker.StateValue.DONE.equals(evt.getNewValue()))) {
106 cancelButton.setEnabled(
false);
107 okButton.setEnabled(
true);
109 if (worker.isSuccess()) {
110 progressBar.setIndeterminate(
false);
111 progressBar.setValue(progressBar.getMaximum());
112 resultLabel.setText(Bundle.UnpackagePortableCaseProgressDialog_propertyChange_success());
115 progressBar.setIndeterminate(
false);
116 progressBar.setValue(0);
117 resultLabel.setForeground(Color.red);
118 resultLabel.setText(worker.getDisplayError());
131 private final AtomicBoolean success =
new AtomicBoolean();
132 private String lastError =
"";
135 this.packagedCase = packagedCase;
136 this.outputFolder = outputFolder;
137 this.success.set(
false);
141 "UnpackageWorker.doInBackground.errorFinding7zip=Could not locate 7-Zip executable",
142 "UnpackageWorker.doInBackground.errorCompressingCase=Error unpackaging case",
143 "UnpackageWorker.doInBackground.canceled=Unpackaging canceled by user",})
148 File sevenZipExe = locate7ZipExecutable();
149 if (sevenZipExe == null) {
150 setDisplayError(Bundle.UnpackageWorker_doInBackground_errorFinding7zip());
151 throw new TskCoreException(
"Error finding 7-Zip executable");
154 String outputFolderSwitch =
"-o" + outputFolder;
155 ProcessBuilder procBuilder =
new ProcessBuilder();
157 sevenZipExe.getAbsolutePath(),
164 Process process = procBuilder.start();
166 while (process.isAlive()) {
167 if (this.isCancelled()) {
168 setDisplayError(Bundle.UnpackageWorker_doInBackground_canceled());
174 int exitCode = process.exitValue();
177 StringBuilder sb =
new StringBuilder();
178 try (BufferedReader br =
new BufferedReader(
new InputStreamReader(process.getErrorStream()))) {
180 while ((line = br.readLine()) != null) {
181 sb.append(line).append(System.getProperty(
"line.separator"));
185 setDisplayError(Bundle.UnpackageWorker_doInBackground_errorCompressingCase());
186 throw new TskCoreException(
"Error unpackaging case. 7-Zip output: " + sb.toString());
188 }
catch (IOException | InterruptedException ex) {
189 setDisplayError(Bundle.UnpackageWorker_doInBackground_errorCompressingCase());
190 throw new TskCoreException(
"Error unpackaging case", ex);
198 synchronized protected void done() {
199 if (this.isCancelled()) {
205 }
catch (Exception ex) {
206 Logger.
getLogger(UnpackagePortableCaseProgressDialog.class.getName()).log(Level.SEVERE,
"Error unpackaging portable case", ex);
216 lastError = errorStr;
229 return success.get();
242 String executableToFindName = Paths.get(
"7-Zip",
"7z.exe").toString();
243 File exeFile = InstalledFileLocator.getDefault().locate(executableToFindName, UnpackagePortableCaseProgressDialog.class.getPackage().getName(),
false);
244 if (null == exeFile) {
248 if (!exeFile.canExecute()) {
261 @SuppressWarnings(
"unchecked")
263 private
void initComponents() {
265 progressBar =
new javax.swing.JProgressBar();
266 cancelButton =
new javax.swing.JButton();
267 okButton =
new javax.swing.JButton();
268 resultLabel =
new javax.swing.JLabel();
270 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
272 org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
org.openide.util.NbBundle.getMessage(UnpackagePortableCaseProgressDialog.class,
"UnpackagePortableCaseProgressDialog.cancelButton.text"));
273 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
274 public void actionPerformed(java.awt.event.ActionEvent evt) {
275 cancelButtonActionPerformed(evt);
279 org.openide.awt.Mnemonics.setLocalizedText(okButton,
org.openide.util.NbBundle.getMessage(UnpackagePortableCaseProgressDialog.class,
"UnpackagePortableCaseProgressDialog.okButton.text"));
280 okButton.addActionListener(
new java.awt.event.ActionListener() {
281 public void actionPerformed(java.awt.event.ActionEvent evt) {
282 okButtonActionPerformed(evt);
286 org.openide.awt.Mnemonics.setLocalizedText(resultLabel,
org.openide.util.NbBundle.getMessage(UnpackagePortableCaseProgressDialog.class,
"UnpackagePortableCaseProgressDialog.resultLabel.text"));
288 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
289 getContentPane().setLayout(layout);
290 layout.setHorizontalGroup(
291 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
292 .addGroup(layout.createSequentialGroup()
294 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
295 .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
296 .addGroup(layout.createSequentialGroup()
297 .addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
298 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
299 .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
300 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
301 .addComponent(cancelButton)))
304 layout.setVerticalGroup(
305 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
306 .addGroup(layout.createSequentialGroup()
308 .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
309 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
310 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
311 .addComponent(cancelButton)
312 .addComponent(okButton)
313 .addComponent(resultLabel))
314 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
320 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
325 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
330 private javax.swing.JButton cancelButton;
331 private javax.swing.JButton okButton;
332 private javax.swing.JProgressBar progressBar;
333 private javax.swing.JLabel resultLabel;
synchronized String getDisplayError()
File locate7ZipExecutable()
final String packagedCase
synchronized void setDisplayError(String errorStr)
synchronized static Logger getLogger(String name)
final String outputFolder