001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package org.anarres.qemu.exec.util;
006
007import java.net.InetSocketAddress;
008import java.util.UUID;
009import javax.annotation.CheckForNull;
010import javax.annotation.Nonnull;
011import org.anarres.qemu.exec.QEmuChardevOption;
012import org.anarres.qemu.exec.QEmuCommandLine;
013import org.anarres.qemu.exec.QEmuIdOption;
014import org.anarres.qemu.exec.QEmuMonitorOption;
015import org.anarres.qemu.exec.host.chardev.TcpCharDevice;
016import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
017
018/**
019 *
020 * @author shevek
021 */
022public class QEmuCommandLineUtils {
023
024    @CheckForNull
025    public static UUID getUuid(@Nonnull QEmuCommandLine commandLine) {
026        QEmuIdOption idOption = commandLine.getOption(QEmuIdOption.class);
027        if (idOption == null)
028            return null;
029        return idOption.getUuid();
030    }
031
032    @CheckForNull
033    public static InetSocketAddress getMonitorAddress(@Nonnull QEmuCommandLine commandLine) {
034        QEmuMonitorOption monitorOption = commandLine.getOption(QEmuMonitorOption.class);
035        if (monitorOption == null)
036            return null;
037        String monitorChardev = monitorOption.chardev;
038        for (QEmuChardevOption chardevOption : commandLine.getOptions(QEmuChardevOption.class)) {
039            if (chardevOption.id.equals(monitorChardev)) {
040                if (chardevOption.device instanceof TcpCharDevice) {
041                    TcpCharDevice tcpDevice = (TcpCharDevice) chardevOption.device;
042                    return tcpDevice.getAddress();
043                }
044            }
045        }
046        return null;
047    }
048
049    /** TODO: Write a thread-based implementation of this method if anyone complains. */
050    @IgnoreJRERequirement
051    public static void redirectIO(@Nonnull ProcessBuilder builder) {
052        builder.inheritIO();
053    }
054
055    private QEmuCommandLineUtils() {
056    }
057}