Make compatible with java5
This commit is contained in:
parent
9fcd31a4f4
commit
fc5606e034
2
pom.xml
2
pom.xml
@ -329,7 +329,7 @@
|
||||
<configuration>
|
||||
<signature>
|
||||
<groupId>org.codehaus.mojo.signature</groupId>
|
||||
<artifactId>java16</artifactId>
|
||||
<artifactId>java15</artifactId>
|
||||
<version>1.0</version>
|
||||
</signature>
|
||||
<ignores>
|
||||
|
@ -15,20 +15,7 @@
|
||||
*/
|
||||
package org.jboss.netty.util;
|
||||
|
||||
import org.jboss.netty.logging.InternalLogger;
|
||||
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
@ -40,148 +27,6 @@ import java.util.StringTokenizer;
|
||||
*/
|
||||
public final class NetUtil {
|
||||
|
||||
/**
|
||||
* The {@link InetAddress} representing the host machine
|
||||
* <p/>
|
||||
* We cache this because some machines take almost forever to return from
|
||||
* {@link InetAddress}.getLocalHost(). This may be due to incorrect
|
||||
* configuration of the hosts and DNS client configuration files.
|
||||
*/
|
||||
public static final InetAddress LOCALHOST;
|
||||
|
||||
/**
|
||||
* The loopback {@link NetworkInterface} on the current machine
|
||||
*/
|
||||
public static final NetworkInterface LOOPBACK_IF;
|
||||
|
||||
/**
|
||||
* The SOMAXCONN value of the current machine. If failed to get the value, 3072 is used as a
|
||||
* default value.
|
||||
*/
|
||||
public static final int SOMAXCONN;
|
||||
|
||||
/**
|
||||
* The logger being used by this class
|
||||
*/
|
||||
private static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(NetUtil.class);
|
||||
|
||||
static {
|
||||
//Start the process of discovering localhost
|
||||
InetAddress localhost;
|
||||
try {
|
||||
localhost = InetAddress.getLocalHost();
|
||||
validateHost(localhost);
|
||||
} catch (IOException e) {
|
||||
// The default local host names did not work. Try hard-coded IPv4 address.
|
||||
try {
|
||||
localhost = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
|
||||
validateHost(localhost);
|
||||
} catch (IOException e1) {
|
||||
// The hard-coded IPv4 address did not work. Try hard coded IPv6 address.
|
||||
try {
|
||||
localhost = InetAddress.getByAddress(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
|
||||
validateHost(localhost);
|
||||
} catch (IOException e2) {
|
||||
throw new Error("Failed to resolve localhost - incorrect network configuration?", e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOCALHOST = localhost;
|
||||
|
||||
//Prepare to get the local NetworkInterface
|
||||
NetworkInterface loopbackInterface;
|
||||
|
||||
try {
|
||||
//Automatically get the loopback interface
|
||||
loopbackInterface = NetworkInterface.getByInetAddress(LOCALHOST);
|
||||
} catch (SocketException e) {
|
||||
//No? Alright. There is a backup!
|
||||
loopbackInterface = null;
|
||||
}
|
||||
|
||||
//Check to see if a network interface was not found
|
||||
if (loopbackInterface == null) {
|
||||
try {
|
||||
//Start iterating over all network interfaces
|
||||
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
interfaces.hasMoreElements();) {
|
||||
//Get the "next" interface
|
||||
NetworkInterface networkInterface = interfaces.nextElement();
|
||||
|
||||
//Check to see if the interface is a loopback interface
|
||||
if (networkInterface.isLoopback()) {
|
||||
//Phew! The loopback interface was found.
|
||||
loopbackInterface = networkInterface;
|
||||
//No need to keep iterating
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
//Nope. Can't do anything else, sorry!
|
||||
logger.error("Failed to enumerate network interfaces", e);
|
||||
}
|
||||
}
|
||||
|
||||
//Set the loopback interface constant
|
||||
LOOPBACK_IF = loopbackInterface;
|
||||
|
||||
int somaxconn = 3072;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new FileReader("/proc/sys/net/core/somaxconn"));
|
||||
somaxconn = Integer.parseInt(in.readLine());
|
||||
} catch (Exception e) {
|
||||
// Failed to get SOMAXCONN
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
// Ignored.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SOMAXCONN = somaxconn;
|
||||
}
|
||||
|
||||
private static void validateHost(InetAddress host) throws IOException {
|
||||
ServerSocket ss = null;
|
||||
Socket s1 = null;
|
||||
Socket s2 = null;
|
||||
try {
|
||||
ss = new ServerSocket();
|
||||
ss.setReuseAddress(false);
|
||||
ss.bind(new InetSocketAddress(host, 0));
|
||||
s1 = new Socket(host, ss.getLocalPort());
|
||||
s2 = ss.accept();
|
||||
} finally {
|
||||
if (s2 != null) {
|
||||
try {
|
||||
s2.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
if (s1 != null) {
|
||||
try {
|
||||
s1.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
if (ss != null) {
|
||||
try {
|
||||
ss.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an byte[] based on an ipAddressString. No error handling is
|
||||
* performed here.
|
||||
@ -231,7 +76,7 @@ public final class NetUtil {
|
||||
if (":".equals(token)) {
|
||||
if (":".equals(prevToken)) {
|
||||
doubleColonIndex = hexStrings.size();
|
||||
} else if (!prevToken.isEmpty()) {
|
||||
} else if (prevToken.length() > 0) {
|
||||
hexStrings.add(prevToken);
|
||||
}
|
||||
} else if (".".equals(token)) {
|
||||
|
@ -249,16 +249,6 @@ public class NetUtilTest {
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testLocalhost() {
|
||||
assertNotNull(NetUtil.LOCALHOST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoopback() {
|
||||
assertNotNull(NetUtil.LOOPBACK_IF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsValidIpV4Address() {
|
||||
for (String host : validIpV4Hosts.keySet()) {
|
||||
|
Loading…
Reference in New Issue
Block a user