Permalink
Browse files

Add explicit return type for make_client to support override

Scala narrows the return type of an overridden method to the inferred type of the overriding method.  This prevents subclasses from further overriding according to the original type, HttpClient.  Specify HttpClient explicitly so that any subclass can override make_client.
  • Loading branch information...
1 parent 49f593f commit 3749a80ddcbe8b23d3ae7fdabfec49c80d11b4b7 @cyrus-saks cyrus-saks committed Oct 17, 2011
Showing with 30 additions and 5 deletions.
  1. +2 −5 http/src/main/scala/thread/thread.scala
  2. +28 −0 http/src/test/scala/HttpSpec.scala
@@ -1,9 +1,8 @@
package dispatch.thread
-import org.apache.http.{HttpHost,HttpRequest,HttpResponse}
-import org.apache.http.client.methods.HttpRequestBase
import dispatch._
import dispatch.futures._
+import org.apache.http.client.HttpClient
/** Http with a thread-safe client */
trait Safety { self: BlockingHttp =>
@@ -13,7 +12,7 @@ trait Safety { self: BlockingHttp =>
def maxConnectionsPerRoute = maxConnections
/** Shutdown connection manager if no longer in use. */
- override def make_client = new ThreadSafeHttpClient(
+ override def make_client: HttpClient = new ThreadSafeHttpClient(
credentials, maxConnections, maxConnectionsPerRoute)
}
@@ -36,8 +35,6 @@ class ThreadSafeHttpClient(
maxConnections: Int,
maxConnectionsPerRoute: Int
) extends ConfiguredHttpClient(credentials) {
- import org.apache.http.conn.scheme.{Scheme,SchemeRegistry,PlainSocketFactory}
- import org.apache.http.conn.ssl.SSLSocketFactory
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
override def createClientConnectionManager = {
val cm = new ThreadSafeClientConnManager()
@@ -1,3 +1,7 @@
+import org.apache.http.client.methods.HttpUriRequest
+import org.apache.http.client.{ResponseHandler, HttpClient}
+import org.apache.http.protocol.HttpContext
+import org.apache.http.{HttpRequest, HttpHost}
import org.specs._
object HttpSpec extends Specification {
@@ -27,6 +31,30 @@ object HttpSpec extends Specification {
"Backwards combined request get" should {
get_specs(/("test.text") <& :/("technically.us"))
}
+
+ "Http" should {
+ class SimpleDelegatingHttpClient(realClient: HttpClient) extends HttpClient {
+ def getParams = realClient.getParams
+ def getConnectionManager = realClient.getConnectionManager
+ def execute(request: HttpUriRequest) = realClient.execute(request)
+ def execute(request: HttpUriRequest, context: HttpContext) = realClient.execute(request, context)
+ def execute(target: HttpHost, request: HttpRequest) = realClient.execute(target, request)
+ def execute(target: HttpHost, request: HttpRequest, context: HttpContext) = realClient.execute(target, request, context)
+ def execute[T](request: HttpUriRequest, responseHandler: ResponseHandler[_ <: T]) = realClient.execute(request, responseHandler)
+ def execute[T](request: HttpUriRequest, responseHandler: ResponseHandler[_ <: T], context: HttpContext) = realClient.execute(request, responseHandler, context)
+ def execute[T](target: HttpHost, request: HttpRequest, responseHandler: ResponseHandler[_ <: T]) = realClient.execute(target, request, responseHandler)
+ def execute[T](target: HttpHost, request: HttpRequest, responseHandler: ResponseHandler[_ <: T], context: HttpContext) = realClient.execute(target, request, responseHandler, context)
+ }
+
+ "allow override" in {
+ val http = new Http with thread.Safety {
+ override def make_client: HttpClient = new SimpleDelegatingHttpClient(super.make_client)
+ }
+ http must notBeNull // i.e. this code should compile
+ http.shutdown()
+ }
+ }
+
val http = new Http
val httpfuture = new thread.Http
def get_specs(test: Request) = {

0 comments on commit 3749a80

Please sign in to comment.