Browse Source

Updated play-scala to 2.2.0-RC2

Christopher Hunt 12 years ago
parent
commit
c9e2c79350

+ 35 - 41
play-scala/app/controllers/Application.scala

@@ -34,7 +34,7 @@ object Application extends Controller {
   // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
   // queries we're going to make or what other threads are running in parallel etc. Nevertheless, the check is
   // adequate in order to throttle the acceptance of requests to the size of the pool.
-  def isDbAvailable: Boolean = (tpe.getQueue.size() < maxConnections * MaxQueriesPerRequest)
+  def isDbAvailable: Boolean = tpe.getQueue.size() < maxConnections * MaxQueriesPerRequest
 
 
   def json() = Action {
@@ -42,57 +42,51 @@ object Application extends Controller {
   }
 
   def db(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
-    Action {
-      Async {
-        val random = ThreadLocalRandom.current()
-
-        val worlds = Future.sequence((for {
-          _ <- 1 to queries
-        } yield Future(World.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
-          ).toList)
-
-        worlds map {
-          w => Ok(Json.toJson(w))
-        }
+    Action.async {
+      val random = ThreadLocalRandom.current()
+
+      val worlds = Future.sequence((for {
+        _ <- 1 to queries
+      } yield Future(World.findById(random.nextInt(TestDatabaseRows) + 1))(dbEc)
+        ).toList)
+
+      worlds map {
+        w => Ok(Json.toJson(w))
       }
     }
   }
 
   def fortunes() = PredicatedAction(isDbAvailable, ServiceUnavailable) {
-    Action {
-      Async {
-        Future(Fortune.getAll())(dbEc).map { fs =>
-          val fortunes =  Fortune(anorm.NotAssigned, "Additional fortune added at request time.") +: fs
-          Ok(views.html.fortune(fortunes))
-        }
+    Action.async {
+      Future(Fortune.getAll())(dbEc).map { fs =>
+        val fortunes =  Fortune(anorm.NotAssigned, "Additional fortune added at request time.") +: fs
+        Ok(views.html.fortune(fortunes))
       }
     }
   }
 
   def update(queries: Int) = PredicatedAction(isDbAvailable, ServiceUnavailable) {
-    Action {
-      Async {
-        val random = ThreadLocalRandom.current()
-
-        val boundsCheckedQueries = queries match {
-          case q if q > 500 => 500
-          case q if q <   1 => 1
-          case _ => queries
-        }
-
-        val worlds = Future.sequence((for {
-          _ <- 1 to boundsCheckedQueries
-        } yield Future {
-            val world = World.findById(random.nextInt(TestDatabaseRows) + 1)
-            val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
-            World.updateRandom(updatedWorld)
-            updatedWorld
-          }(dbEc)
-        ).toList)
+    Action.async {
+      val random = ThreadLocalRandom.current()
+
+      val boundsCheckedQueries = queries match {
+        case q if q > 500 => 500
+        case q if q <   1 => 1
+        case _ => queries
+      }
 
-        worlds.map {
-          w => Ok(Json.toJson(w)).withHeaders("Server" -> "Netty")
-        }
+      val worlds = Future.sequence((for {
+        _ <- 1 to boundsCheckedQueries
+      } yield Future {
+          val world = World.findById(random.nextInt(TestDatabaseRows) + 1)
+          val updatedWorld = world.copy(randomNumber = random.nextInt(TestDatabaseRows) + 1)
+          World.updateRandom(updatedWorld)
+          updatedWorld
+        }(dbEc)
+      ).toList)
+
+      worlds.map {
+        w => Ok(Json.toJson(w)).withHeaders("Server" -> "Netty")
       }
     }
   }

+ 1 - 0
play-scala/app/models/Fortune.scala

@@ -4,6 +4,7 @@ import play.api.db._
 import play.api.Play.current
 import anorm._
 import anorm.SqlParser._
+import scala.language.postfixOps
 
 case class Fortune(id: Pk[Long], message: String)
 

+ 4 - 3
play-scala/app/utils/PredicatedAction.scala

@@ -1,15 +1,16 @@
 package utils
 
 import play.api.mvc._
+import scala.concurrent.{Promise, Future}
 
 /**
  * A predicated action is one where a condition must be satisfied in order to proceed with the request. If the
  * condition is not satisfied then a supplied status result is yielded.
  */
 class PredicatedActionBuilder {
-  def apply[A](p: => Boolean, failed: => Result)(action: Action[A]): Action[A] = new Action[A] {
-    def apply(request: Request[A]): Result = {
-      if (p) action(request) else failed
+  def apply[A](p: => Boolean, failed: => SimpleResult)(action: Action[A]): Action[A] = new Action[A] {
+    def apply(request: Request[A]): Future[SimpleResult] = {
+      if (p) action(request) else Promise.successful(failed).future
     }
 
     lazy val parser = action.parser

+ 7 - 0
play-scala/build.sbt

@@ -0,0 +1,7 @@
+name := "play-scala"
+
+version := "1.0-SNAPSHOT"
+
+libraryDependencies ++= Seq(jdbc, anorm, "mysql" % "mysql-connector-java" % "5.1.22")
+
+playScalaSettings

+ 0 - 20
play-scala/conf/application.conf

@@ -64,23 +64,3 @@ logger.play=ERROR
 
 # Logger provided to your application:
 logger.application=ERROR
-
-play {
-  akka {
-    event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
-    loglevel = WARNING
-    actor {
-      default-dispatcher = {
-        fork-join-executor {
-          parallelism-factor = 1.0
-          parallelism-max = 50
-        }
-      }
-      application = {
-        fork-join-executor {
-          parallelism-max = 300
-        }
-      }
-    }
-  }
-}

+ 0 - 19
play-scala/project/Build.scala

@@ -1,19 +0,0 @@
-import sbt._
-import Keys._
-import play.Project._
-
-object ApplicationBuild extends Build {
-
-  val appName         = "play-scala"
-  val appVersion      = "1.0-SNAPSHOT"
-
-  val appDependencies = Seq(
-    jdbc,
-    anorm,
-    "mysql" % "mysql-connector-java" % "5.1.22"
-  )
-
-  val main = play.Project(appName, appVersion, appDependencies).settings(
-  )
-
-}

+ 1 - 1
play-scala/project/build.properties

@@ -1 +1 @@
-sbt.version=0.12.3
+sbt.version=0.13.0

+ 1 - 1
play-scala/project/plugins.sbt

@@ -5,4 +5,4 @@ logLevel := Level.Warn
 resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
 
 // Use the Play sbt plugin for Play projects
-addSbtPlugin("play" % "sbt-plugin" % "2.1.2-RC1")
+addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0-RC2")