package org.openslx.dozmod.util; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import javax.swing.SwingWorker; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.helper.I18n; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.wizard.page.ImageOvfConversionPage; public class ConversionTaskWorker extends SwingWorker { private final static Logger LOGGER = LogManager.getLogger(ConversionTaskWorker.class); private File srcFile; private File destFile; private ImageOvfConversionPage page; private Process process = null; private String conversionCancelledText = I18n.HELPER.getString("OVFConversion.info.aborted.text"); private String ovfToolPath = "ovftool"; /** * Converts a given ovf and associated vmdk into an vmx and associated vmdk file * via the VMware OVF Tool Ovf images created with virtualBox are not supported. * * @param srcFile: Ovf source file * @param destFile: Desired destination file * @param page: Page that creates and starts the worker * @param ovfToolPath: Path to the OVF Tool on the system */ public ConversionTaskWorker(File srcFile, File destFile, ImageOvfConversionPage page, String ovfToolPath) { this.srcFile = srcFile; this.destFile = destFile; this.page = page; if (ovfToolPath != null) { this.ovfToolPath = ovfToolPath; } } @Override protected Boolean doInBackground() throws Exception { process = new ProcessBuilder(ovfToolPath, srcFile.getAbsolutePath(), destFile.getAbsolutePath()) .start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; LOGGER.info("Running OVFTool"); while ((line = br.readLine()) != null) { LOGGER.debug(line); publish(line); } return true; } @Override protected void process(List chunks) { // Don't update the gui progress bar and text if cancelled as this thread my run // longer and override our cancelled notifications. if (isCancelled()) return; String progressText = chunks.get(chunks.size() - 1); page.updateConversionProgressbarText(progressText); progressText = progressText.replaceAll("[^\\d]", ""); try { int progressInt = Integer.parseInt(progressText); if (progressInt < 100) page.updateConversionProgressbar(progressInt); } catch (NumberFormatException e) { // We are only interested in integers for the progressbar // should therefore be OK to discard the exceptions here. } } @Override protected void done() { try { get(); page.setPageComplete(true); page.conversionSuccessful = true; page.updateConversionProgressbar(100); } catch (InterruptedException e) { LOGGER.debug("Conversion execution interrupted", e); process.destroy(); page.updateConversionProgressbarText(conversionCancelledText); } catch (CancellationException e) { LOGGER.debug("Conversion execution Cancelled", e); process.destroy(); page.updateConversionProgressbarText(conversionCancelledText); } catch (ExecutionException e) { e.printStackTrace(); Gui.showMessageBox(page, I18n.HELPER.getString("OVFConversion.error.text"), MessageType.ERROR, LOGGER, e); page.btnStartConversion.setEnabled(false); page.btnStartConversion .setText(I18n.PAGE.getString("ImageOvfConversion.StartConversionButton.text")); } super.done(); } }