public CLI(URL jenkins, ExecutorService exec) throws IOException, InterruptedException { String url = jenkins.toExternalForm(); if (!url.endsWith("/")) url += '/'; ownsPool = exec == null; pool = exec != null ? exec : Executors.newCachedThreadPool(); int clip = getCliTcpPort(url); if (clip >= 0) { // connect via CLI port String host = new URL(url).getHost(); LOGGER.fine("Trying to connect directly via TCP/IP to port " + clip + " of " + host); Socket s = new Socket(host, clip); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF("Protocol:CLI-connect"); channel = new Channel( "CLI connection to " + jenkins, pool, new BufferedInputStream(new SocketInputStream(s)), new BufferedOutputStream(new SocketOutputStream(s))); } else { // connect via HTTP LOGGER.fine("Trying to connect to " + url + " via HTTP"); url += "cli"; jenkins = new URL(url); FullDuplexHttpStream con = new FullDuplexHttpStream(jenkins); channel = new Channel( "Chunked connection to " + jenkins, pool, con.getInputStream(), con.getOutputStream()); new PingThread(channel, 30 * 1000) { protected void onDead() { // noop. the point of ping is to keep the connection alive // as most HTTP servers have a rather short read time out } }.start(); } // execute the command entryPoint = (CliEntryPoint) channel.waitForRemoteProperty(CliEntryPoint.class.getName()); if (entryPoint.protocolVersion() != CliEntryPoint.VERSION) throw new IOException(Messages.CLI_VersionMismatch()); }