DEV Community

Query Filter
Query Filter

Posted on

gradle4

package bootstrap;

import sun.net.spi.nameservice.NameService;
import sun.net.spi.nameservice.NameServiceDescriptor;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class BootstrapServer {
    public static void main(String[] args) throws Exception {
        String forcedHost = System.getProperty("forced.hostname", "egtpsga56.nam.nsroot.net");

        // --- Install fake NameService to override hostname ---
        System.setProperty("sun.net.spi.nameservice.provider.1", "dns,fake");
        System.setProperty("sun.net.spi.nameservice.provider.2", "default");

        // Delegate to the real external main class
        com.citigroup.get.quantum.server.Server.main(args);
    }

    // --- NameService provider ---
    public static class FakeNameService implements NameService {
        private final String forcedHost;

        public FakeNameService(String forcedHost) {
            this.forcedHost = forcedHost;
        }

        @Override
        public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
            if (host.equalsIgnoreCase(InetAddress.getLocalHost().getHostName())) {
                return new InetAddress[]{InetAddress.getByName(forcedHost)};
            }
            return InetAddress.getAllByName(host);
        }

        @Override
        public String getHostByAddr(byte[] addr) throws UnknownHostException {
            return forcedHost;
        }
    }

    public static class FakeNameServiceDescriptor implements NameServiceDescriptor {
        @Override
        public NameService createNameService() {
            try {
                return new FakeNameService(System.getProperty("forced.hostname"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String getProviderName() { return "fake"; }

        @Override
        public String getType() { return "dns"; }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)