From b43aba991f9844f3f1523016918514536f0e9265 Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Mon, 29 Jan 2024 09:32:12 +0100 Subject: [PATCH] Using CACHED_ADDRESSES correctly --- src/org/jgroups/util/Util.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/org/jgroups/util/Util.java b/src/org/jgroups/util/Util.java index bcbeb98752..dd10acdb5f 100644 --- a/src/org/jgroups/util/Util.java +++ b/src/org/jgroups/util/Util.java @@ -123,7 +123,7 @@ public enum AddressScope {GLOBAL,SITE_LOCAL,LINK_LOCAL,LOOPBACK,NON_LOOPBACK} try { CACHED_INTERFACES=getAllAvailableInterfaces(); - CACHED_ADDRESSES=getAllAvailableAddresses(null); + CACHED_ADDRESSES=getAllAvailableAddresses(); } catch(SocketException e) { throw new RuntimeException(e); @@ -4644,7 +4644,7 @@ public static StackType getIpStackType() { * if the type cannot be detected */ private static StackType _getIpStackType() { - Collection all_addresses=getAllAvailableAddresses(null); + Collection all_addresses=getAllAvailableAddresses(); for(InetAddress addr: all_addresses) { if(addr instanceof Inet4Address) ipv4_stack_available=true; @@ -4668,7 +4668,7 @@ else if(addr instanceof Inet6Address) public static boolean isStackAvailable(boolean ipv4) { - Collection all_addrs=getAllAvailableAddresses(null); + Collection all_addrs=getAllAvailableAddresses(); for(InetAddress addr : all_addrs) if(ipv4 && addr instanceof Inet4Address || (!ipv4 && addr instanceof Inet6Address)) return true; @@ -4711,10 +4711,9 @@ public static Collection getAllAvailableAddresses(Predicate getAllAvailableAddresses() { - if (CACHED_ADDRESSES != null) { + if(CACHED_ADDRESSES != null) return CACHED_ADDRESSES; - } - Set addresses = new HashSet<>(); + Set addresses=new HashSet<>(); try { List interfaces=getAllAvailableInterfaces(); for(NetworkInterface intf: interfaces) { @@ -4726,15 +4725,14 @@ private static synchronized Collection getAllAvailableAddresses() { catch(SocketException e) { } // immutable list - CACHED_ADDRESSES = List.copyOf(addresses); - return CACHED_ADDRESSES; + return CACHED_ADDRESSES=List.copyOf(addresses); } public static void checkIfValidAddress(InetAddress bind_addr,String prot_name) throws Exception { // N.B. bind_addr.isAnyLocalAddress() is not OK if (bind_addr.isLoopbackAddress()) return; - Collection addrs=getAllAvailableAddresses(null); + Collection addrs=getAllAvailableAddresses(); for(InetAddress addr : addrs) { if(addr.equals(bind_addr)) return;