Skip to content
Browse files

Configuration

  • Loading branch information...
1 parent cfe17be commit 290905f0ffce11b3ed4bd3f9f0d7a43a374b7ca1 @janm janm committed Jul 13, 2012
View
5 maven/api/pom.xml
@@ -17,6 +17,11 @@
<artifactId>core</artifactId>
<version>0.1.RELEASE-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.cakesolutions.akkapatterns</groupId>
+ <artifactId>domain</artifactId>
+ <version>0.1.RELEASE-SNAPSHOT</version>
+ </dependency>
<dependency>
<groupId>cc.spray</groupId>
View
24 maven/api/src/main/scala/org/cakesolutions/akkapatterns/api/addressbook.scala
@@ -0,0 +1,24 @@
+package org.cakesolutions.akkapatterns.api
+
+import akka.actor.ActorSystem
+import cc.spray.Directives
+import org.cakesolutions.akkapatterns.core.application.GetAddresses
+import akka.pattern.ask
+import org.cakesolutions.akkapatterns.domain.Address
+
+/**
+ * @author janmachacek
+ */
+class AddressBookService(implicit val actorSystem: ActorSystem) extends Directives with Marshallers with DefaultTimeout {
+ def addressBook = actorSystem.actorFor("/user/application/addressBook")
+
+ val route =
+ path("addressbook") {
+ get {
+ completeWith(
+ (addressBook ? GetAddresses("Jan")).mapTo[List[Address]]
+ )
+ }
+ }
+
+}
View
6 maven/api/src/main/scala/org/cakesolutions/akkapatterns/api/boot.scala
@@ -3,6 +3,7 @@ package org.cakesolutions.akkapatterns.api
import akka.actor.{ActorRef, Props}
import cc.spray.{RootService, Route, HttpService, SprayCanRootService}
import org.cakesolutions.akkapatterns.core.Core
+import akka.util.Timeout
trait Api {
this: Core =>
@@ -21,3 +22,8 @@ trait Api {
)
}
+
+trait DefaultTimeout {
+ final implicit val timeout = Timeout(3000)
+
+}
View
3 maven/api/src/main/scala/org/cakesolutions/akkapatterns/api/home.scala
@@ -10,8 +10,7 @@ import org.cakesolutions.akkapatterns.core.application.{PoisonPill, GetImplement
case class SystemInfo(implementation: Implementation, host: String)
-class HomeService(implicit val actorSystem: ActorSystem) extends Directives with Marshallers {
- implicit val timeout = Timeout(3000)
+class HomeService(implicit val actorSystem: ActorSystem) extends Directives with Marshallers with DefaultTimeout {
def applicationActor = actorSystem.actorFor("/user/application")
View
8 maven/core/pom.xml
@@ -16,6 +16,14 @@
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor</artifactId>
</dependency>
+ <dependency>
+ <groupId>commons-dbcp</groupId>
+ <artifactId>commons-dbcp</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ </dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
View
14 maven/core/src/main/scala/org/cakesolutions/akkapatterns/core/application/addressbook.scala
@@ -1,12 +1,20 @@
package org.cakesolutions.akkapatterns.core.application
-import akka.actor.Actor
+import akka.actor.{Address, Actor}
+import org.cakesolutions.akkapatterns.core.Configured
+import javax.sql.DataSource
case class GetAddresses(person: String)
-class AddressBookActor extends Actor {
+class AddressBookActor extends Actor with Configured {
+
protected def receive = {
case GetAddresses(person) =>
- sender ! List(person + "@cakesolutions.net", person + "@gmail.com", person + "@hotmail.com")
+ val dataSource = configured[DataSource]
+ println(dataSource.toString)
+
+ sender ! Address("Robert Robinson Avenue", "Oxford") ::
+ Address("Houldsworth Mill", "Reddish") ::
+ Nil
}
}
View
19 maven/core/src/main/scala/org/cakesolutions/akkapatterns/core/configuration.scala
@@ -10,26 +10,25 @@ private object ConfigurationStore {
entries += ((key, value))
}
- def get[A] = {
- entries.values.find(_.isInstanceOf[A]) match {
- case Some(v) => v.asInstanceOf[A]
- case None => throw new Exception("Cannot find")
- }
- }
+ def get[A] =
+ entries.values.find(_.isInstanceOf[A]).asInstanceOf[Option[A]]
+
}
trait Configured {
def configured[A](implicit actorContext: ActorContext): A =
- ConfigurationStore.get[A]
+ ConfigurationStore.get[A].get
}
-trait Configurable {
-
- def configure[R](f: => R) = {
+trait Configuration {
+ def configure[R <: AnyRef](f: => R) = {
+ val a = f
+ ConfigurationStore.put(a.getClass.getName, a)
+ a
}
}
View
27 ...src/test/scala/org/cakesolutions/akkapatterns/core/application/AddressBookActorSpec.scala
@@ -0,0 +1,27 @@
+package org.cakesolutions.akkapatterns.core.application
+
+import akka.testkit.{TestActorRef, ImplicitSender, TestKit}
+import akka.actor.{Address, ActorSystem}
+import org.specs2.mutable.Specification
+import org.cakesolutions.akkapatterns.core.Core
+
+/**
+ * @author janmachacek
+ */
+class AddressBookActorSpec extends TestKit(ActorSystem()) with Specification with ImplicitSender with Core with SpecConfiguration {
+
+ val actor = TestActorRef[AddressBookActor]
+
+ "GetAddresses(p) replies with the person's addresses" in {
+ actor ! GetAddresses("Jan")
+
+ expectMsg(Address("Robert Robinson Avenue", "Oxford") ::
+ Address("Houldsworth Mill", "Reddish") ::
+ Nil)
+
+ success
+ }
+
+
+ implicit def actorSystem = system
+}
View
21 ...re/src/test/scala/org/cakesolutions/akkapatterns/core/application/SpecConfiguration.scala
@@ -0,0 +1,21 @@
+package org.cakesolutions.akkapatterns.core.application
+
+import org.cakesolutions.akkapatterns.core.Configuration
+import org.apache.commons.dbcp.BasicDataSource
+import org.hsqldb.jdbc.JDBCDriver
+
+/**
+ * @author janmachacek
+ */
+trait SpecConfiguration extends Configuration {
+
+ configure {
+ val ds = new BasicDataSource()
+ ds.setDriverClassName(classOf[JDBCDriver].getCanonicalName)
+ ds.setUsername("sa")
+ ds.setUrl("jdbc:hsqldb:mem:test")
+
+ ds
+ }
+
+}
View
3 maven/domain/src/main/scala/org/cakesolutions/akkapatterns/domain/Address.scala
@@ -0,0 +1,3 @@
+package org.cakesolutions.akkapatterns.domain
+
+case class Address(line1: String, line2: String)
View
20 maven/main/src/main/scala/org/cakesolutions/akkapatterns/main/Main.scala
@@ -1,20 +1,28 @@
package org.cakesolutions.akkapatterns.main
import akka.actor.ActorSystem
-import org.cakesolutions.akkapatterns.core.Core
+import org.cakesolutions.akkapatterns.core.{Configuration, Core}
import org.cakesolutions.akkapatterns.api.Api
import org.cakesolutions.akkapatterns.web.Web
+import javax.sql.ConnectionPoolDataSource
+import org.apache.commons.dbcp.BasicDataSource
+import org.hsqldb.jdbc.JDBCDriver
object Main extends App {
// -javaagent:/Users/janmachacek/.m2/repository/org/springframework/spring-instrument/3.1.1.RELEASE/spring-instrument-3.1.1.RELEASE.jar -Xmx512m -XX:MaxPermSize=256m
- class RuntimeConfiguration {
-
- }
-
implicit val system = ActorSystem("AkkaPatterns")
- class Application(val actorSystem: ActorSystem) extends Core with Api with Web
+ class Application(val actorSystem: ActorSystem) extends Core with Api with Web with Configuration {
+ configure {
+ val ds = new BasicDataSource()
+ ds.setDriverClassName(classOf[JDBCDriver].getCanonicalName)
+ ds.setUsername("sa")
+ ds.setUrl("jdbc:hsqldb:mem:test")
+
+ ds
+ }
+ }
new Application(system)
View
12 maven/parent/pom.xml
@@ -195,6 +195,18 @@
<version>1.0.2</version>
</dependency>
+ <!-- DBCP -->
+ <dependency>
+ <groupId>commons-dbcp</groupId>
+ <artifactId>commons-dbcp</artifactId>
+ <version>1.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>2.2.4</version>
+ </dependency>
+
<!-- Lift JSON -->
<dependency>
<groupId>net.liftweb</groupId>

0 comments on commit 290905f

Please sign in to comment.
Something went wrong with that request. Please try again.