31
31
public class NetworkInterface {
32
32
33
33
private final String name ;
34
+ private java .net .NetworkInterface networkInterface ;
34
35
private final Iterable <InetAddress > inetAddresses ;
35
- private boolean isLoopback ;
36
+ private Boolean isLoopback ;
36
37
37
38
public NetworkInterface (java .net .NetworkInterface networkInterface ) {
38
39
this (networkInterface .getName (), list (networkInterface .getInetAddresses ()));
39
- try {
40
- // Issue 1181 : determine whether this NetworkInterface instance is loopback
41
- // from java.net.NetworkInterface API
42
- this .isLoopback = networkInterface .isLoopback ();
43
- } catch (SocketException ex ) {
44
- Logger .getLogger (NetworkInterface .class .getName ()).log (Level .WARNING , null , ex );
45
- // If an SocketException is caught, determine whether this NetworkInterface
46
- // instance is loopback from computation from its inetAddresses
47
- this .isLoopback =
48
- isLoopBackFromINetAddresses (list (networkInterface .getInetAddresses ()));
49
- }
40
+ this .networkInterface = networkInterface ;
50
41
}
51
42
52
43
NetworkInterface (String name , Iterable <InetAddress > inetAddresses ) {
@@ -64,6 +55,22 @@ public boolean isIp4AddressBindingOnly() {
64
55
}
65
56
66
57
public boolean isLoopBack () {
58
+ if (isLoopback == null ) {
59
+ if (networkInterface != null ) {
60
+ try {
61
+ // Issue 1181 : determine whether this NetworkInterface instance is loopback
62
+ // from java.net.NetworkInterface API
63
+ isLoopback = networkInterface .isLoopback ();
64
+ } catch (SocketException ex ) {
65
+ Logger .getLogger (NetworkInterface .class .getName ()).log (Level .WARNING , null , ex );
66
+ }
67
+ }
68
+ // If a SocketException is caught, determine whether this NetworkInterface
69
+ // instance is loopback from computation from its inetAddresses
70
+ if (isLoopback == null ) {
71
+ isLoopback = isLoopBackFromINetAddresses (list (networkInterface .getInetAddresses ()));
72
+ }
73
+ }
67
74
return isLoopback ;
68
75
}
69
76
@@ -80,11 +87,11 @@ public InetAddress getIp4LoopbackOnly() {
80
87
// Most "normal" boxes don't have multiple addresses so we'll just refine this
81
88
// algorithm until it works.
82
89
// See NetworkUtilsTest#testOpenSuseBoxIssue1181
83
- InetAddress lastFound = null ;
84
90
// Issue 1181
85
- if (!isLoopback ) {
86
- return lastFound ;
91
+ if (!isLoopBack () ) {
92
+ return null ;
87
93
}
94
+ InetAddress lastFound = null ;
88
95
for (InetAddress inetAddress : inetAddresses ) {
89
96
if (inetAddress .isLoopbackAddress () && !isIpv6 (inetAddress )) {
90
97
lastFound = inetAddress ;
0 commit comments