package com.yahoo.vespa.config.server.filedistribution;

import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.config.FileReference;
import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;
import com.yahoo.net.HostName;
import com.yahoo.vespa.config.Connection;
import com.yahoo.vespa.config.ConnectionPool;
import com.yahoo.vespa.config.JRTConnectionPool;
import com.yahoo.vespa.config.server.ConfigServerSpec;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Utilities related to file distribution on config servers.
 *
 * @author musum
 * @author gjoranv
 */
public class FileDistributionUtil {

    /**
     * Returns all files in the given directory, non-recursive.
     */
    public static Set<String> getFileReferencesOnDisk(File directory) {
        Set<String> fileReferencesOnDisk = new HashSet<>();
        File[] filesOnDisk = directory.listFiles();
        if (filesOnDisk != null)
            fileReferencesOnDisk.addAll(Arrays.stream(filesOnDisk).map(File::getName).collect(Collectors.toSet()));
        return fileReferencesOnDisk;
    }

    /**
     * Returns a connection pool with all config servers except this one, or an empty pool if there
     * is only one config server.
     */
    public static ConnectionPool createConnectionPool(ConfigserverConfig configserverConfig) {
        List<String> configServers = ConfigServerSpec.fromConfig(configserverConfig)
                .stream()
                .filter(spec -> !spec.getHostName().equals(HostName.getLocalhost()))
                .map(spec -> "tcp/" + spec.getHostName() + ":" + spec.getConfigServerPort())
                .collect(Collectors.toList());

        return configServers.size() > 0 ? new JRTConnectionPool(new ConfigSourceSet(configServers)) : emptyConnectionPool();
    }

    public static boolean fileReferenceExistsOnDisk(File downloadDirectory, FileReference applicationPackageReference) {
        return getFileReferencesOnDisk(downloadDirectory).contains(applicationPackageReference.value());
    }

    static ConnectionPool emptyConnectionPool() {
        return new EmptyConnectionPool();
    }

    private static class EmptyConnectionPool implements ConnectionPool {

        @Override
        public void close() {}

        @Override
        public void setError(Connection connection, int i) {}

        @Override
        public Connection getCurrent() { return null; }

        @Override
        public Connection setNewCurrentConnection() { return null; }

        @Override
        public int getSize() { return 0; }

        @Override
        public Supervisor getSupervisor() { return new Supervisor(new Transport()); }
    }

}

@@ -2356,16 +2356,55 @@
 onPool %7B
+%0A        private Supervisor supervisor;
 %0A%0A      
@@ -2444,16 +2444,195 @@
 lose() %7B
+%0A            synchronized (this) %7B%0A                if (supervisor != null) %7B%0A                    supervisor.transport().shutdown().join();%0A                %7D%0A            %7D%0A        
 %7D%0A%0A     
@@ -2997,48 +2997,221 @@
 () %7B
- return new Supervisor(new Transport());
+%0A            synchronized (this) %7B%0A                if (supervisor == null) %7B%0A                    supervisor = new Supervisor(new Transport());%0A                %7D%0A            %7D%0A            return supervisor;%0A       
  %7D%0A 

Create one Supervisor instead of a new one on every request.