|
24 | 24 | import java.net.UnknownHostException; |
25 | 25 | import java.util.Map; |
26 | 26 | import java.util.concurrent.ConcurrentHashMap; |
27 | | -import java.util.function.Function; |
28 | 27 |
|
| 28 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
29 | 29 | import lombok.AllArgsConstructor; |
30 | 30 | import lombok.EqualsAndHashCode; |
31 | 31 | import lombok.NonNull; |
|
50 | 50 | @Value |
51 | 51 | @EqualsAndHashCode(of = "fullName") |
52 | 52 | @AllArgsConstructor(access = PRIVATE) |
| 53 | +@SuppressWarnings({ |
| 54 | + "PMD.AvoidThrowingRawExceptionTypes", |
| 55 | + "PMD.AvoidLiteralsInIfCondition" |
| 56 | +}) |
| 57 | +@SuppressFBWarnings("RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED") |
53 | 58 | public class NodeDescriptor implements Serializable { |
54 | 59 |
|
55 | 60 | private static final long serialVersionUID = 7324588959922091097L; |
56 | 61 |
|
57 | 62 | private static final Map<String, NodeDescriptor> NODE_DESCRIPTOR_CACHE; |
58 | 63 |
|
59 | | - private static final Function<String, NodeDescriptor> COMPUTE_NODE_DESCRIPTOR; |
| 64 | + private static final InetAddress LOCALHOST; |
| 65 | + |
| 66 | + private static final InetAddress LOOPBACK; |
| 67 | + |
| 68 | + private static final String HOST_NAME; |
60 | 69 |
|
61 | 70 | static { |
62 | | - val loopback = InetAddress.getLoopbackAddress(); |
| 71 | + try { |
| 72 | + LOCALHOST = InetAddress.getLocalHost(); |
| 73 | + } catch (UnknownHostException ex) { |
| 74 | + throw new RuntimeException(ex); |
| 75 | + } |
| 76 | + LOOPBACK = InetAddress.getLoopbackAddress(); |
| 77 | + HOST_NAME = LOCALHOST.getHostName(); |
63 | 78 | NODE_DESCRIPTOR_CACHE = new ConcurrentHashMap<>(); |
64 | | - COMPUTE_NODE_DESCRIPTOR = str -> { |
65 | | - val tokens = str.split("@", 2); |
66 | | - val shortName = tokens[0]; |
67 | | - val fullName = tokens.length == 2 |
68 | | - ? str |
69 | | - : shortName + '@' + loopback.getHostName(); |
70 | | - |
71 | | - val address = tokens.length == 2 |
72 | | - ? getByName(tokens[1]) |
73 | | - : loopback; |
74 | | - |
75 | | - return new NodeDescriptor(shortName, fullName, address); |
76 | | - }; |
77 | 79 | } |
78 | 80 |
|
79 | 81 | /** |
80 | 82 | * Parses node descriptor from string. |
81 | 83 | * <p> |
82 | | - * A string could be short like <b>popa_node</b> and full, like <b>[email protected]</b>. |
| 84 | + * A string could be short like <b>popa_node</b> and long, like <b>[email protected]</b>. |
83 | 85 | * <p> |
84 | 86 | * Node descriptors are caching. |
85 | 87 | * |
86 | 88 | * @param node node name |
87 | 89 | * |
88 | 90 | * @return parsed {@link NodeDescriptor} instance |
89 | 91 | */ |
90 | | - @SneakyThrows |
91 | 92 | public static NodeDescriptor from (@NonNull String node) { |
92 | 93 | val cached = NODE_DESCRIPTOR_CACHE.get(node); |
93 | 94 | if (cached != null) { |
94 | 95 | return cached; |
95 | 96 | } |
96 | | - return of(node) |
| 97 | + val descriptor = getFromCacheOrCreateNew(node); |
| 98 | + NODE_DESCRIPTOR_CACHE.putIfAbsent(node, descriptor); |
| 99 | + NODE_DESCRIPTOR_CACHE.putIfAbsent(descriptor.getFullName(), descriptor); |
| 100 | + NODE_DESCRIPTOR_CACHE.putIfAbsent(descriptor.getNodeName(), descriptor); |
| 101 | + return descriptor; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Parses node descriptor from string. |
| 106 | + * <p> |
| 107 | + * A string could be short like <b>popa_node</b> and long, like <b>[email protected]</b>. |
| 108 | + * <p> |
| 109 | + * Node descriptors are caching. |
| 110 | + * |
| 111 | + * @param node node name |
| 112 | + * |
| 113 | + * @param isShortName indicates is this node name short or long |
| 114 | + * |
| 115 | + * @return parsed {@link NodeDescriptor} instance |
| 116 | + */ |
| 117 | + |
| 118 | + public static NodeDescriptor from (@NonNull String node, boolean isShortName) { |
| 119 | + val cached = NODE_DESCRIPTOR_CACHE.get(node); |
| 120 | + if (cached != null) { |
| 121 | + return cached; |
| 122 | + } |
| 123 | + val descriptor = getFromCacheOrCreateNew(node, isShortName); |
| 124 | + NODE_DESCRIPTOR_CACHE.putIfAbsent(node, descriptor); |
| 125 | + NODE_DESCRIPTOR_CACHE.putIfAbsent(descriptor.getFullName(), descriptor); |
| 126 | + NODE_DESCRIPTOR_CACHE.putIfAbsent(descriptor.getNodeName(), descriptor); |
| 127 | + return descriptor; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Checks, if this node cached or not. |
| 132 | + * |
| 133 | + * @param node node name |
| 134 | + * |
| 135 | + * @return {@code true}, if node descriptor was cached, or |
| 136 | + * {@code false} if there was no such node |
| 137 | + */ |
| 138 | + public static boolean wasCached (@NonNull String node) { |
| 139 | + val descriptor = getFromCacheOrCreateNew(node); |
| 140 | + return wasCached(descriptor); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Checks, if this node cached or not. |
| 145 | + * |
| 146 | + * @param descriptor node descriptor |
| 147 | + * |
| 148 | + * @return {@code true}, if node descriptor was cached, or |
| 149 | + * {@code false} if there was no such node |
| 150 | + */ |
| 151 | + public static boolean wasCached (@NonNull NodeDescriptor descriptor) { |
| 152 | + return NODE_DESCRIPTOR_CACHE.containsKey(descriptor.getNodeName()) || |
| 153 | + NODE_DESCRIPTOR_CACHE.containsKey(descriptor.getFullName()); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Removes a node from cached values. |
| 158 | + * |
| 159 | + * @param node node name |
| 160 | + * |
| 161 | + * @return {@code true}, if node descriptor was removed, or |
| 162 | + * {@code false} if there was no such node |
| 163 | + */ |
| 164 | + public static boolean removeFromCache (@NonNull String node) { |
| 165 | + val descriptor = getFromCacheOrCreateNew(node); |
| 166 | + return removeFromCache(descriptor); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Removes a node from cached values. |
| 171 | + * |
| 172 | + * @param descriptor node descriptor |
| 173 | + * |
| 174 | + * @return {@code true}, if node descriptor was removed, or |
| 175 | + * {@code false} if there was no such node |
| 176 | + */ |
| 177 | + public static boolean removeFromCache (@NonNull NodeDescriptor descriptor) { |
| 178 | + val wasRemoved = NODE_DESCRIPTOR_CACHE.remove(descriptor.getNodeName()) != null; |
| 179 | + return NODE_DESCRIPTOR_CACHE.remove(descriptor.getFullName()) != null || wasRemoved; |
| 180 | + } |
| 181 | + |
| 182 | + private static NodeDescriptor getFromCacheOrCreateNew (@NonNull String node) { |
| 183 | + val atIndex = node.indexOf('@'); |
| 184 | + val isShortName = atIndex < 0 || node.indexOf('.', atIndex) < 0; |
| 185 | + return getFromCacheOrCreateNew(node, isShortName); |
| 186 | + } |
| 187 | + |
| 188 | + private static NodeDescriptor getFromCacheOrCreateNew (@NonNull String node, boolean isShortName) { |
| 189 | + String trimmedNode = of(node) |
97 | 190 | .map(String::trim) |
98 | 191 | .filter(it -> !it.isEmpty()) |
99 | | - .map(it -> NODE_DESCRIPTOR_CACHE.computeIfAbsent(it, COMPUTE_NODE_DESCRIPTOR)) |
100 | | - .orElseThrow(() -> new IllegalArgumentException("Invalid node descriptor string")); |
| 192 | + .orElseThrow(() -> new IllegalArgumentException("Invalid node descriptor string '" + node + "'")); |
| 193 | + |
| 194 | + val cached = NODE_DESCRIPTOR_CACHE.get(trimmedNode); |
| 195 | + return cached == null |
| 196 | + ? parse(trimmedNode, isShortName) |
| 197 | + : cached; |
101 | 198 | } |
102 | 199 |
|
103 | | - @SuppressWarnings("PMD.PreserveStackTrace") |
104 | | - private static InetAddress getByName (@NonNull String hostname) { |
105 | | - try { |
106 | | - return InetAddress.getByName(hostname); |
107 | | - } catch (UnknownHostException ex) { |
108 | | - try { |
109 | | - return InetAddress.getByName(hostname + ".local"); |
110 | | - } catch (UnknownHostException ex1) { |
111 | | - throw new IllegalArgumentException("Wrapped", ex); |
| 200 | + @SneakyThrows |
| 201 | + private static NodeDescriptor parse (@NonNull String node, boolean isShortName) { |
| 202 | + val tokens = node.split("@", 2); |
| 203 | + |
| 204 | + val nodeName = tokens[0]; |
| 205 | + if (nodeName.isEmpty()) { |
| 206 | + throw new IllegalArgumentException(); |
| 207 | + } |
| 208 | + |
| 209 | + String hostName; |
| 210 | + InetAddress address; |
| 211 | + if (tokens.length == 2) { |
| 212 | + hostName = tokens[1]; |
| 213 | + if (isShortName && hostName.contains(".")) { |
| 214 | + val message = String.format("Using host name '%s' in short node name is illegal, " + |
| 215 | + "because it contains dots ('.') and suits only for long name nodes", |
| 216 | + hostName); |
| 217 | + throw new IllegalArgumentException(message); |
112 | 218 | } |
| 219 | + address = InetAddress.getByName(hostName); |
| 220 | + } else if (isShortName) { |
| 221 | + val dotIndex = HOST_NAME.indexOf('.'); |
| 222 | + hostName = dotIndex > 0 |
| 223 | + ? HOST_NAME.substring(dotIndex) |
| 224 | + : HOST_NAME; |
| 225 | + |
| 226 | + address = LOOPBACK; |
| 227 | + } else { |
| 228 | + hostName = HOST_NAME; |
| 229 | + address = LOCALHOST; |
113 | 230 | } |
| 231 | + |
| 232 | + val fullName = nodeName + '@' + hostName; |
| 233 | + return new NodeDescriptor(nodeName, hostName, fullName, address, isShortName); |
114 | 234 | } |
115 | 235 |
|
116 | | - String shortName; |
| 236 | + String nodeName; |
| 237 | + |
| 238 | + String hostName; |
117 | 239 |
|
118 | 240 | String fullName; |
119 | 241 |
|
120 | 242 | InetAddress address; |
| 243 | + |
| 244 | + boolean shortName; |
| 245 | + |
| 246 | + /** |
| 247 | + * Tells is this {@link NodeDescriptor} instance for short-named node or not. |
| 248 | + * |
| 249 | + * @return {@code true} if node name is short, {@code false} otherwise |
| 250 | + */ |
| 251 | + public boolean isShortName () { |
| 252 | + return shortName; |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Tells is this {@link NodeDescriptor} instance for long-named node or not. |
| 257 | + * |
| 258 | + * @return {@code true} if node name is long, {@code false} otherwise |
| 259 | + */ |
| 260 | + public boolean isLongName () { |
| 261 | + return !shortName; |
| 262 | + } |
121 | 263 | } |
0 commit comments