Merge remote-tracking branch 'Netty/master' into fix-414

This commit is contained in:
Cruz Julian Bishop 2012-06-29 13:02:32 +10:00
commit 00ae0e8ad9
4 changed files with 120 additions and 21 deletions

View File

@ -165,6 +165,6 @@ public final class SpdyHttpHeaders {
* Sets the {@code "X-SPDY-Scheme"} header. * Sets the {@code "X-SPDY-Scheme"} header.
*/ */
public static void setScheme(HttpMessage message, String scheme) { public static void setScheme(HttpMessage message, String scheme) {
message.setHeader(Names.URL, scheme); message.setHeader(Names.SCHEME, scheme);
} }
} }

View File

@ -24,62 +24,101 @@ import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Enumeration; import java.util.Enumeration;
/**
* A class that holds a number of network-related constants.
*/
public final class NetworkConstants { public final class NetworkConstants {
/**
* The {@link InetAddress} representing the host machine
*
* 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; public static final InetAddress LOCALHOST;
/**
* The loopback {@link NetworkInterface} on the current machine
*/
public static final NetworkInterface LOOPBACK_IF; public static final NetworkInterface LOOPBACK_IF;
/**
* The logger being used by this class
*/
private static final InternalLogger logger = private static final InternalLogger logger =
InternalLoggerFactory.getInstance(NetworkConstants.class); InternalLoggerFactory.getInstance(NetworkConstants.class);
static { static {
// We cache this because some machine takes almost forever to return
// from InetAddress.getLocalHost(). I think it's due to the incorrect //Start the process of discovering localhost
// /etc/hosts or /etc/resolve.conf.
InetAddress localhost = null; InetAddress localhost = null;
try { try {
//Let's start by getting localhost automatically
localhost = InetAddress.getLocalHost(); localhost = InetAddress.getLocalHost();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
//No? That's okay.
try { try {
//Try to force an IPv4 localhost address
localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
} catch (UnknownHostException e1) { } catch (UnknownHostException e1) {
//No? Okay. You must be using IPv6
try { try {
//Try to force an IPv6 localhost address
localhost = InetAddress.getByAddress( localhost = InetAddress.getByAddress(
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }); new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 });
} catch (UnknownHostException e2) { } catch (UnknownHostException e2) {
logger.error("Failed to resolve localhost", e2); //No? Okay.
logger.error("Failed to resolve localhost - Incorrect network configuration?", e2);
} }
} }
} }
//Set the localhost constant
LOCALHOST = localhost; LOCALHOST = localhost;
NetworkInterface loopbackIf; //Prepare to get the local NetworkInterface
NetworkInterface loopbackInterface;
try { try {
loopbackIf = NetworkInterface.getByInetAddress(LOCALHOST); //Automatically get the loopback interface
loopbackInterface = NetworkInterface.getByInetAddress(LOCALHOST);
} catch (SocketException e) { } catch (SocketException e) {
loopbackIf = null; //No? Alright. There is a backup!
loopbackInterface = null;
} }
// If null is returned, iterate over all the available network interfaces. //Check to see if a network interface was not found
if (loopbackIf == null) { if (loopbackInterface == null) {
try { try {
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); //Start iterating over all network interfaces
e.hasMoreElements();) { for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface nif = e.nextElement(); interfaces.hasMoreElements();) {
if (nif.isLoopback()) { //Get the "next" interface
loopbackIf = nif; 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; break;
} }
} }
} catch (SocketException e) { } catch (SocketException e) {
//Nope. Can't do anything else, sorry!
logger.error("Failed to enumerate network interfaces", e); logger.error("Failed to enumerate network interfaces", e);
} }
} }
LOOPBACK_IF = loopbackIf; //Set the loopback interface constant
LOOPBACK_IF = loopbackInterface;
} }
/**
* A constructor to stop this class being constructed.
*/
private NetworkConstants() { private NetworkConstants() {
// Unused // Unused
} }

View File

@ -20,48 +20,97 @@ import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
/**
* A custom implementation of a lock that does not allow reentry
*/
public final class NonReentrantLock extends AbstractQueuedSynchronizer public final class NonReentrantLock extends AbstractQueuedSynchronizer
implements Lock { implements Lock {
/**
* The serial version unique ID
*/
private static final long serialVersionUID = -833780837233068610L; private static final long serialVersionUID = -833780837233068610L;
/**
* The {@link Thread} that owns this {@link NonReentrantLock}
*/
private Thread owner; private Thread owner;
/**
* Locks this {@link NonReentrantLock}
*/
@Override @Override
public void lock() { public void lock() {
acquire(1); acquire(1);
} }
/**
* Locks this {@link NonReentrantLock}, but allow interruption
*
* @throws InterruptedException The lock was interrupted
*/
@Override @Override
public void lockInterruptibly() throws InterruptedException { public void lockInterruptibly() throws InterruptedException {
acquireInterruptibly(1); acquireInterruptibly(1);
} }
/**
* Try to lock this {@link NonReentrantLock}
*
* @return True if locking was successful, otherwise false
*/
@Override @Override
public boolean tryLock() { public boolean tryLock() {
return tryAcquire(1); return tryAcquire(1);
} }
/**
* Tries to lock this {@link NonReentrantLock} over a period of time
*
* @param time The maximum number of time units to attempt to get a lock for.
* @param unit The {@link TimeUnit} associated with the time parameter
* @return True if the lock was successful, otherwise false
* @throws InterruptedException The locking attempt was interrupted
*/
@Override @Override
public boolean tryLock(long time, TimeUnit unit) public boolean tryLock(long time, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
return tryAcquireNanos(1, unit.toNanos(time)); return tryAcquireNanos(1, unit.toNanos(time));
} }
/**
* Unlocks this {@link NonReentrantLock}
*/
@Override @Override
public void unlock() { public void unlock() {
release(1); release(1);
} }
/**
* Checks to see if this {@link NonReentrantLock} is held by the current {@link Thread}
*
* @return True if held by the current thread, otherwise false
*/
public boolean isHeldByCurrentThread() { public boolean isHeldByCurrentThread() {
return isHeldExclusively(); return isHeldExclusively();
} }
/**
* Creates a new {@link Condition}
*
* @return The condition object
*/
@Override @Override
public Condition newCondition() { public Condition newCondition() {
return new ConditionObject(); return new ConditionObject();
} }
/**
* Try to acquire a lock
*
* @param acquires A number that is sent by acquiring methods
* @return True if a lock is acquired, otherwise false
*/
@Override @Override
protected boolean tryAcquire(int acquires) { protected boolean tryAcquire(int acquires) {
if (compareAndSetState(0, 1)) { if (compareAndSetState(0, 1)) {
@ -71,6 +120,12 @@ public final class NonReentrantLock extends AbstractQueuedSynchronizer
return false; return false;
} }
/**
* Tries to release the lock
*
* @param releases A number that is passed by the release methods
* @return True if a release is granted, otherwise false
*/
@Override @Override
protected boolean tryRelease(int releases) { protected boolean tryRelease(int releases) {
if (Thread.currentThread() != owner) { if (Thread.currentThread() != owner) {
@ -81,6 +136,11 @@ public final class NonReentrantLock extends AbstractQueuedSynchronizer
return true; return true;
} }
/**
* Checks to see if this {@link NonReentrantLock} is held exclusively by the current {@link Thread}
*
* @return True if held exclusively, otherwise false
*/
@Override @Override
protected boolean isHeldExclusively() { protected boolean isHeldExclusively() {
return getState() != 0 && owner == Thread.currentThread(); return getState() != 0 && owner == Thread.currentThread();

View File

@ -216,7 +216,7 @@
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>1.6.4</version> <version>1.6.6</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -226,7 +226,7 @@
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-enforcer-plugin</artifactId> <artifactId>maven-enforcer-plugin</artifactId>
<version>1.0.1</version> <version>1.1</version>
<executions> <executions>
<execution> <execution>
<id>enforce-tools</id> <id>enforce-tools</id>
@ -266,7 +266,7 @@
be used even when compiling with java 1.7+ --> be used even when compiling with java 1.7+ -->
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId> <artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.7</version> <version>1.8</version>
<configuration> <configuration>
<signature> <signature>
<groupId>org.codehaus.mojo.signature</groupId> <groupId>org.codehaus.mojo.signature</groupId>
@ -345,7 +345,7 @@
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-release-plugin</artifactId> <artifactId>maven-release-plugin</artifactId>
<version>2.3.1</version> <version>2.3.2</version>
<configuration> <configuration>
<releaseProfiles>release,full</releaseProfiles> <releaseProfiles>release,full</releaseProfiles>
</configuration> </configuration>