HTTPCLIENT-1288: moved HttpExecutionAware implementation code to AbstractExecutionAwareRequest class

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1427723 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-01-02 10:12:59 +00:00
parent c02e1b3f75
commit fbb5561603
2 changed files with 175 additions and 141 deletions

View File

@ -0,0 +1,171 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.client.methods;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.http.HttpRequest;
import org.apache.http.client.utils.CloneUtils;
import org.apache.http.concurrent.Cancellable;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionReleaseTrigger;
import org.apache.http.message.AbstractHttpMessage;
@SuppressWarnings("deprecation")
public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
private final Lock abortLock;
private volatile boolean aborted;
private volatile Cancellable cancellable;
protected AbstractExecutionAwareRequest() {
super();
this.abortLock = new ReentrantLock();
}
@Deprecated
public void setConnectionRequest(final ClientConnectionRequest connRequest) {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.cancellable = new Cancellable() {
public boolean cancel() {
connRequest.abortRequest();
return true;
}
};
} finally {
this.abortLock.unlock();
}
}
@Deprecated
public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.cancellable = new Cancellable() {
public boolean cancel() {
try {
releaseTrigger.abortConnection();
return true;
} catch (IOException ex) {
return false;
}
}
};
} finally {
this.abortLock.unlock();
}
}
private void cancelExecution() {
if (this.cancellable != null) {
this.cancellable.cancel();
this.cancellable = null;
}
}
public void abort() {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.aborted = true;
cancelExecution();
} finally {
this.abortLock.unlock();
}
}
public boolean isAborted() {
return this.aborted;
}
/**
* @since 4.2
*/
public void setCancellable(final Cancellable cancellable) {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.cancellable = cancellable;
} finally {
this.abortLock.unlock();
}
}
@Override
public Object clone() throws CloneNotSupportedException {
AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone();
clone.headergroup = CloneUtils.cloneObject(this.headergroup);
clone.params = CloneUtils.cloneObject(this.params);
return clone;
}
/**
* @since 4.2
*/
public void completed() {
this.abortLock.lock();
try {
this.cancellable = null;
} finally {
this.abortLock.unlock();
}
}
/**
* Resets internal state of the request making it reusable.
*
* @since 4.2
*/
public void reset() {
this.abortLock.lock();
try {
cancelExecution();
this.aborted = false;
} finally {
this.abortLock.unlock();
}
}
}

View File

@ -27,20 +27,12 @@
package org.apache.http.client.methods;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.utils.CloneUtils;
import org.apache.http.concurrent.Cancellable;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionReleaseTrigger;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicRequestLine;
import org.apache.http.params.HttpProtocolParams;
@ -51,22 +43,13 @@ import org.apache.http.params.HttpProtocolParams;
*/
@SuppressWarnings("deprecation")
@NotThreadSafe
public abstract class HttpRequestBase extends AbstractHttpMessage
implements HttpUriRequest, Configurable, HttpExecutionAware, AbortableHttpRequest, Cloneable {
private Lock abortLock;
private volatile boolean aborted;
public abstract class HttpRequestBase extends AbstractExecutionAwareRequest
implements HttpUriRequest, Configurable {
private ProtocolVersion version;
private URI uri;
private Cancellable cancellable;
private RequestConfig config;
public HttpRequestBase() {
super();
this.abortLock = new ReentrantLock();
}
public abstract String getMethod();
/**
@ -104,9 +87,6 @@ public abstract class HttpRequestBase extends AbstractHttpMessage
return new BasicRequestLine(method, uritext, ver);
}
public void setURI(final URI uri) {
this.uri = uri;
}
public RequestConfig getConfig() {
return config;
@ -116,72 +96,8 @@ public abstract class HttpRequestBase extends AbstractHttpMessage
this.config = config;
}
@Deprecated
public void setConnectionRequest(final ClientConnectionRequest connRequest) {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.cancellable = new Cancellable() {
public boolean cancel() {
connRequest.abortRequest();
return true;
}
};
} finally {
this.abortLock.unlock();
}
}
@Deprecated
public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.cancellable = new Cancellable() {
public boolean cancel() {
try {
releaseTrigger.abortConnection();
return true;
} catch (IOException ex) {
return false;
}
}
};
} finally {
this.abortLock.unlock();
}
}
private void cancelExecution() {
if (this.cancellable != null) {
this.cancellable.cancel();
this.cancellable = null;
}
}
public void abort() {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.aborted = true;
cancelExecution();
} finally {
this.abortLock.unlock();
}
}
public boolean isAborted() {
return this.aborted;
public void setURI(final URI uri) {
this.uri = uri;
}
/**
@ -190,48 +106,6 @@ public abstract class HttpRequestBase extends AbstractHttpMessage
public void started() {
}
/**
* @since 4.2
*/
public void setCancellable(final Cancellable cancellable) {
if (this.aborted) {
return;
}
this.abortLock.lock();
try {
this.cancellable = cancellable;
} finally {
this.abortLock.unlock();
}
}
/**
* @since 4.2
*/
public void completed() {
this.abortLock.lock();
try {
this.cancellable = null;
} finally {
this.abortLock.unlock();
}
}
/**
* Resets internal state of the request making it reusable.
*
* @since 4.2
*/
public void reset() {
this.abortLock.lock();
try {
cancelExecution();
this.aborted = false;
} finally {
this.abortLock.unlock();
}
}
/**
* A convenience method to simplify migration from HttpClient 3.1 API. This method is
* equivalent to {@link #reset()}.
@ -242,17 +116,6 @@ public abstract class HttpRequestBase extends AbstractHttpMessage
reset();
}
@Override
public Object clone() throws CloneNotSupportedException {
HttpRequestBase clone = (HttpRequestBase) super.clone();
clone.abortLock = new ReentrantLock();
clone.aborted = false;
clone.cancellable = null;
clone.headergroup = CloneUtils.cloneObject(this.headergroup);
clone.params = CloneUtils.cloneObject(this.params);
return clone;
}
@Override
public String toString() {
return getMethod() + " " + getURI() + " " + getProtocolVersion();