All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.maven.docker.wait.TcpPortChecker Maven / Gradle / Ivy

There is a newer version: 0.45.0
Show newest version
package io.fabric8.maven.docker.wait;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Check whether a given TCP port is available
 */
public class TcpPortChecker implements WaitChecker {

    private static final int TCP_PING_TIMEOUT = 500;

    private final List ports;

    private final List pending;

    public TcpPortChecker(String host, List ports) {
        this.ports = ports;

        this.pending = new ArrayList<>();
        for (int port : ports) {
            this.pending.add(new InetSocketAddress(host, port));
        }

    }

    public List getPorts() {
        return ports;
    }

    @Override
    public boolean check() {
        Iterator iter = pending.iterator();

        while (iter.hasNext()) {
            InetSocketAddress address = iter.next();

            try {
                Socket s = new Socket();
                s.connect(address, TCP_PING_TIMEOUT);
                s.close();
                iter.remove();
            } catch (IOException e) {
                // Ports isn't opened, yet. So don't remove from queue.
                // Can happen and is part of the flow
            }
        }
        return pending.isEmpty();
    }

    @Override
    public void cleanUp() {
        // No cleanup required
    }

    @Override
    public String getLogLabel() {
        return "on tcp port '" + pending + "'";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy