|
19 | 19 | import com.rabbitmq.client.Connection; |
20 | 20 | import com.rabbitmq.client.ConnectionFactory; |
21 | 21 | import com.rabbitmq.client.RecoveryDelayHandler; |
| 22 | +import com.rabbitmq.client.SocketConfigurator; |
| 23 | +import com.rabbitmq.client.SocketConfigurators; |
| 24 | +import com.rabbitmq.client.SslEngineConfigurator; |
| 25 | +import com.rabbitmq.client.SslEngineConfigurators; |
22 | 26 | import com.rabbitmq.client.impl.recovery.AutorecoveringConnection; |
23 | | - |
24 | 27 | import java.net.URISyntaxException; |
25 | 28 | import java.security.KeyManagementException; |
26 | 29 | import java.security.NoSuchAlgorithmException; |
27 | | -import java.util.concurrent.*; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.Future; |
| 35 | +import java.util.concurrent.ThreadLocalRandom; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.TimeoutException; |
28 | 38 | import java.util.regex.Matcher; |
29 | 39 | import java.util.regex.Pattern; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import javax.net.ssl.SNIHostName; |
| 42 | +import javax.net.ssl.SNIServerName; |
| 43 | +import javax.net.ssl.SSLParameters; |
| 44 | +import javax.net.ssl.SSLSocket; |
| 45 | +import org.slf4j.Logger; |
| 46 | +import org.slf4j.LoggerFactory; |
30 | 47 |
|
31 | 48 | abstract class Utils { |
32 | 49 |
|
33 | | - private static final ConnectionFactory CF = new ConnectionFactory(); |
34 | | - |
35 | | - static boolean isRecoverable(Connection connection) { |
36 | | - return connection instanceof AutorecoveringConnection; |
37 | | - } |
38 | | - |
39 | | - static synchronized Address extract(String uri) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException { |
40 | | - CF.setUri(uri); |
41 | | - return new Address(CF.getHost(), CF.getPort()); |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * @param argument |
46 | | - * @return |
47 | | - * @since 2.11.0 |
48 | | - */ |
49 | | - static RecoveryDelayHandler getRecoveryDelayHandler(String argument) { |
50 | | - if (argument == null || argument.trim().isEmpty()) { |
51 | | - return null; |
52 | | - } |
53 | | - argument = argument.trim(); |
54 | | - Pattern pattern = Pattern.compile("(\\d+)(-(\\d+))?"); |
55 | | - Matcher matcher = pattern.matcher(argument); |
56 | | - if (!matcher.matches()) { |
57 | | - throw new IllegalArgumentException("Incorrect argument for connection recovery interval. Must be e.g. 30 or 30-60."); |
58 | | - } |
59 | | - |
60 | | - RecoveryDelayHandler handler; |
61 | | - final long delay = Long.parseLong(matcher.group(1)) * 1000; |
62 | | - if (matcher.group(2) == null) { |
63 | | - handler = recoveryAttempts -> delay; |
64 | | - } else { |
65 | | - final long maxInput = Long.parseLong(matcher.group(2).replace("-", "")) * 1000; |
66 | | - if (maxInput <= delay) { |
67 | | - throw new IllegalArgumentException("Wrong interval min-max values: " + argument); |
68 | | - } |
69 | | - final long maxDelay = maxInput + 1000; |
70 | | - handler = recoveryAttempts -> ThreadLocalRandom.current().nextLong(delay, maxDelay); |
71 | | - } |
72 | | - return handler; |
73 | | - } |
74 | | - |
75 | | - static final Future<?> NO_OP_FUTURE = new Future<Object>() { |
| 50 | + static final Future<?> NO_OP_FUTURE = |
| 51 | + new Future<Object>() { |
76 | 52 |
|
77 | 53 | @Override |
78 | 54 | public boolean cancel(boolean mayInterruptIfRunning) { |
79 | | - return true; |
| 55 | + return true; |
80 | 56 | } |
81 | 57 |
|
82 | 58 | @Override |
83 | 59 | public boolean isCancelled() { |
84 | | - return false; |
| 60 | + return false; |
85 | 61 | } |
86 | 62 |
|
87 | 63 | @Override |
88 | 64 | public boolean isDone() { |
89 | | - return true; |
| 65 | + return true; |
90 | 66 | } |
91 | 67 |
|
92 | 68 | @Override |
93 | 69 | public Object get() throws InterruptedException, ExecutionException { |
94 | | - return null; |
| 70 | + return null; |
95 | 71 | } |
96 | 72 |
|
97 | 73 | @Override |
98 | | - public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
99 | | - return null; |
| 74 | + public Object get(long timeout, TimeUnit unit) |
| 75 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 76 | + return null; |
100 | 77 | } |
101 | | - }; |
| 78 | + }; |
| 79 | + private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); |
| 80 | + private static final ConnectionFactory CF = new ConnectionFactory(); |
| 81 | + |
| 82 | + static boolean isRecoverable(Connection connection) { |
| 83 | + return connection instanceof AutorecoveringConnection; |
| 84 | + } |
| 85 | + |
| 86 | + static synchronized Address extract(String uri) |
| 87 | + throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException { |
| 88 | + CF.setUri(uri); |
| 89 | + return new Address(CF.getHost(), CF.getPort()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param argument |
| 94 | + * @return |
| 95 | + * @since 2.11.0 |
| 96 | + */ |
| 97 | + static RecoveryDelayHandler getRecoveryDelayHandler(String argument) { |
| 98 | + if (argument == null || argument.trim().isEmpty()) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + argument = argument.trim(); |
| 102 | + Pattern pattern = Pattern.compile("(\\d+)(-(\\d+))?"); |
| 103 | + Matcher matcher = pattern.matcher(argument); |
| 104 | + if (!matcher.matches()) { |
| 105 | + throw new IllegalArgumentException( |
| 106 | + "Incorrect argument for connection recovery interval. Must be e.g. 30 or 30-60."); |
| 107 | + } |
| 108 | + |
| 109 | + RecoveryDelayHandler handler; |
| 110 | + final long delay = Long.parseLong(matcher.group(1)) * 1000; |
| 111 | + if (matcher.group(2) == null) { |
| 112 | + handler = recoveryAttempts -> delay; |
| 113 | + } else { |
| 114 | + final long maxInput = Long.parseLong(matcher.group(2).replace("-", "")) * 1000; |
| 115 | + if (maxInput <= delay) { |
| 116 | + throw new IllegalArgumentException("Wrong interval min-max values: " + argument); |
| 117 | + } |
| 118 | + final long maxDelay = maxInput + 1000; |
| 119 | + handler = recoveryAttempts -> ThreadLocalRandom.current().nextLong(delay, maxDelay); |
| 120 | + } |
| 121 | + return handler; |
| 122 | + } |
| 123 | + |
| 124 | + static List<SNIServerName> sniServerNames(String argumentValue) { |
| 125 | + if (argumentValue != null && !argumentValue.trim().isEmpty()) { |
| 126 | + return Arrays.stream(argumentValue.split(",")) |
| 127 | + .map(s -> s.trim()) |
| 128 | + .map(s -> new SNIHostName(s)) |
| 129 | + .collect(Collectors.toList()); |
| 130 | + } else { |
| 131 | + return Collections.emptyList(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + static SocketConfigurator socketConfigurator(CommandLineProxy cmd) { |
| 136 | + List<SNIServerName> serverNames = sniServerNames(strArg(cmd, "sni", null)); |
| 137 | + if (serverNames.isEmpty()) { |
| 138 | + return SocketConfigurators.defaultConfigurator(); |
| 139 | + } else { |
| 140 | + SocketConfigurator socketConfigurator = |
| 141 | + socket -> { |
| 142 | + if (socket instanceof SSLSocket) { |
| 143 | + SSLSocket sslSocket = (SSLSocket) socket; |
| 144 | + SSLParameters sslParameters = |
| 145 | + sslSocket.getSSLParameters() == null |
| 146 | + ? new SSLParameters() |
| 147 | + : sslSocket.getSSLParameters(); |
| 148 | + sslParameters.setServerNames(serverNames); |
| 149 | + sslSocket.setSSLParameters(sslParameters); |
| 150 | + } else { |
| 151 | + LOGGER.warn("SNI parameter set on a non-TLS connection"); |
| 152 | + } |
| 153 | + }; |
| 154 | + return SocketConfigurators.defaultConfigurator().andThen(socketConfigurator); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + static SslEngineConfigurator sslEngineConfigurator(CommandLineProxy cmd) { |
| 159 | + List<SNIServerName> serverNames = sniServerNames(strArg(cmd, "sni", null)); |
| 160 | + if (serverNames.isEmpty()) { |
| 161 | + return SslEngineConfigurators.DEFAULT; |
| 162 | + } else { |
| 163 | + SslEngineConfigurator sslEngineConfigurator = |
| 164 | + sslEngine -> { |
| 165 | + SSLParameters sslParameters = |
| 166 | + sslEngine.getSSLParameters() == null |
| 167 | + ? new SSLParameters() |
| 168 | + : sslEngine.getSSLParameters(); |
| 169 | + sslParameters.setServerNames(serverNames); |
| 170 | + sslEngine.setSSLParameters(sslParameters); |
| 171 | + }; |
| 172 | + return SslEngineConfigurators.defaultConfigurator().andThen(sslEngineConfigurator); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + static String strArg(CommandLineProxy cmd, String opt, String def) { |
| 177 | + return cmd.getOptionValue(opt, def); |
| 178 | + } |
102 | 179 |
|
| 180 | + static String strArg(CommandLineProxy cmd, char opt, String def) { |
| 181 | + return cmd.getOptionValue(opt, def); |
| 182 | + } |
103 | 183 | } |
0 commit comments