summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ConversionTaskWorker.java
blob: fb76db880ff705958a8b904b4a6ac831d684b431 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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<Boolean, String> {
    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<String> 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();
    }
}