added thread safety to Http fetches. Tried nio, but is more work... webid
authorHenry Story <henry.story@bblfish.net>
Sat, 03 Dec 2011 20:54:22 +0100
branchwebid
changeset 146 eb94418a1c66
parent 145 4865469e5aac
child 147 fedeae805046
added thread safety to Http fetches. Tried nio, but is more work...
project/build.scala
src/main/scala/WebCache.scala
--- a/project/build.scala	Sat Dec 03 18:44:47 2011 +0100
+++ b/project/build.scala	Sat Dec 03 20:54:22 2011 +0100
@@ -7,7 +7,9 @@
 //  val specs = "org.scala-tools.testing" %% "specs" % "1.6.9" % "test"
   val specs2 = "org.specs2" %% "specs2" % "1.6.1"
   val specs2_scalaz =  "org.specs2" %% "specs2-scalaz-core" % "6.0.1" % "test"
-  val dispatch_http = "net.databinder" %% "dispatch-http" % "0.8.5" 
+  val dispatch_version = "0.8.6"
+  val dispatch_http = "net.databinder" %% "dispatch-http" % dispatch_version 
+//  val dispatch_nio = "net.databinder" %% "dispatch-nio" % dispatch_version 
   val unfiltered_version = "0.5.2"
   val unfiltered_filter = "net.databinder" %% "unfiltered-filter" % unfiltered_version 
   val unfiltered_jetty = "net.databinder" %% "unfiltered-jetty" % unfiltered_version 
@@ -91,6 +93,7 @@
 //      libraryDependencies += unfiltered_spec,
       ivyXML := ivyUnfilteredSpec,
       libraryDependencies += dispatch_http,
+      libraryDependencies += dispatch_nio,
       libraryDependencies += unfiltered_filter,
       libraryDependencies += unfiltered_jetty,
       libraryDependencies += unfiltered_netty,
--- a/src/main/scala/WebCache.scala	Sat Dec 03 18:44:47 2011 +0100
+++ b/src/main/scala/WebCache.scala	Sat Dec 03 20:54:22 2011 +0100
@@ -26,9 +26,9 @@
 import com.hp.hpl.jena.rdf.model.Model
 import org.apache.http.MethodNotSupportedException
 import org.w3.readwriteweb.util._
-import scalaz._
-import Scalaz._
 import java.net.{ConnectException, URL}
+import scalaz.{Scalaz, Validation}
+
 
 /**
  * @author Henry Story
@@ -36,10 +36,11 @@
  *
  * The WebCache currently does not cache
  */
-class WebCache extends ResourceManager {
+class WebCache extends ResourceManager  {
   import dispatch._
+  import Scalaz._
 
-  val http = new Http
+  val http = new Http with thread.Safety
   
   def basePath = null //should be cache dir?
 
@@ -70,7 +71,8 @@
         }
       })
       try {
-         http(handler)
+         val future = http(handler)
+         future
       } catch {
         case e: ConnectException => e.fail
       }
@@ -83,4 +85,6 @@
 
     def createDirectory(model: Model) =  throw new MethodNotSupportedException("not implemented")
   }
+
+   override def finalize() { http.shutdown() }
 }