-
Notifications
You must be signed in to change notification settings - Fork 25
/
AbstractConnectionTask.java
100 lines (86 loc) · 3.15 KB
/
AbstractConnectionTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.sshtools.client.tasks;
/*-
* #%L
* Client API
* %%
* Copyright (C) 2002 - 2024 JADAPTIVE Limited
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import java.util.Optional;
import java.util.function.Function;
import com.sshtools.client.SshClient;
import com.sshtools.common.ssh.SshConnection;
import com.sshtools.synergy.ssh.Connection;
public abstract class AbstractConnectionTask extends Task {
public static abstract class AbstractConnectionTaskBuilder<B extends AbstractConnectionTaskBuilder<B, T>, T extends AbstractConnectionTask> {
protected Optional<Function<Integer, SshClient>> clientSupplier = Optional.empty();
protected Optional<SshConnection> connection = Optional.empty();
/**
* Set a single {@link SshClient} to use.
*
* @param client client
* @return builder for chaining
*
*/
public B withClient(SshClient client) {
return withClients((idx) -> client);
}
/**
* Set a single {@link Connection} to use.
*
* @param connection connection
* @return builder for chaining
*
*/
@SuppressWarnings("unchecked")
public B withConnection(SshConnection connection) {
this.connection = Optional.of(connection);
return (B) this;
}
/**
* Set a {@link Function} that supplies
* {@link SshClient} instances. The function is provided with an
* <code>index</code>, with the control client being index <code>0</code>, and
* subsequent clients having index of <code>1</code> up to to whatever
* {@link #withChunks(int)} was given (or the default of <code>3</code>).
*
* @param clientSupplier supplier of clients
* @return builder for chaining
*/
@SuppressWarnings("unchecked")
public B withClients(Function<Integer, SshClient> clientSupplier) {
this.clientSupplier = Optional.of(clientSupplier);
return (B) this;
}
public abstract T build();
}
protected final Optional<Function<Integer, SshClient>> clientSupplier;
protected AbstractConnectionTask(AbstractConnectionTaskBuilder<?, ?> builder) {
super(builder.connection.orElse(builder.clientSupplier.orElseThrow(() -> new IllegalArgumentException("No connection or client supplied.")).apply(0).getConnection()));
this.clientSupplier = builder.clientSupplier;
}
@Deprecated(since = "3.1.0", forRemoval = true)
protected AbstractConnectionTask(SshClient ssh) {
super(ssh);
clientSupplier = Optional.empty();
}
@Deprecated(since = "3.1.0", forRemoval = true)
protected AbstractConnectionTask(SshConnection con) {
super(con);
clientSupplier = Optional.empty();
}
}