Skip to content

Commit

Permalink
Added ability to configure default write concern
Browse files Browse the repository at this point in the history
  • Loading branch information
jroper committed May 21, 2012
1 parent 74993d4 commit c2ad67f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/main/scala/play/modules/mongodb/jackson/MongoDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import org.codehaus.jackson.map.ObjectMapper
import net.vz.mongodb.jackson.internal.MongoJacksonMapperModule
import play.api.Application
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.mongodb.{Mongo, MongoURI, ServerAddress}
import net.vz.mongodb.jackson.{MongoCollection, JacksonDBCollection}
import java.util.Locale
import java.lang.reflect.ParameterizedType
import com.mongodb.{WriteConcern, Mongo, MongoURI, ServerAddress}

/**
* MongoDB Jackson Mapper module for play framework
Expand Down Expand Up @@ -179,11 +179,18 @@ class MongoDBPlugin(val app: Application) extends Plugin {
_.configure(defaultMapper)
} getOrElse defaultMapper

val defaultWriteConcern = app.configuration.getString("mongodb.defaultWriteConcern") map { value =>
WriteConcern.valueOf(value)
} filter {
_ != null
}

app.configuration.getString("mongodb.uri") match {
case Some(uri) => {
val mongoURI = new MongoURI(uri)
val mongo = new Mongo(mongoURI)
val db = mongo.getDB(mongoURI.getDatabase)
defaultWriteConcern.foreach { concern => db.setWriteConcern(concern) }
if (mongoURI.getUsername != null) {
if (!db.authenticate(mongoURI.getUsername, mongoURI.getPassword)) {
throw new IllegalArgumentException("MongoDB authentication failed for user: " + mongoURI.getUsername + " on database: "
Expand Down Expand Up @@ -223,6 +230,9 @@ class MongoDBPlugin(val app: Application) extends Plugin {
val dbName = app.configuration.getString("mongodb.database").getOrElse("play")
val db = mongo.getDB(dbName)

// Write concern
defaultWriteConcern.foreach { concern => db.setWriteConcern(concern) }

// Authenticate if necessary
val credentials = app.configuration.getString("mongodb.credentials")
if (credentials.isDefined) {
Expand All @@ -249,7 +259,10 @@ class MongoDBPlugin(val app: Application) extends Plugin {
val mapper = configurer map {
_.configure(globalMapper, name, entityType, keyType)
} getOrElse globalMapper
val coll = JacksonDBCollection.wrap(db.getCollection(name), entityType, keyType, mapper)

val mongoColl = db.getCollection(name)
val coll = JacksonDBCollection.wrap(mongoColl, entityType, keyType, mapper)

cache.putIfAbsent((name, entityType, keyType), coll)
coll
}
Expand Down
20 changes: 18 additions & 2 deletions src/test/scala/play/modules/mongodb/jackson/MongoDBSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import util.Random
import reflect.BeanProperty
import org.codehaus.jackson.annotate.JsonProperty
import org.codehaus.jackson.map.{DeserializationConfig, ObjectMapper}
import com.mongodb.{BasicDBObject, Mongo}
import net.vz.mongodb.jackson.{JacksonDBCollection, MongoCollection, Id}
import com.mongodb.{WriteConcern, BasicDBObject, Mongo}

class MongoDBSpec extends Specification {
case class MongoDBSpec() extends Specification {

"The MongoDB plugin" should {

Expand Down Expand Up @@ -142,6 +142,22 @@ class MongoDBSpec extends Specification {
coll.save(doc).getSavedId must_== "foo"
}
}

"use default write concern if not configured" in new Setup {
implicit val app = fakeApp(Map.empty)
running(app) {
val coll = MongoDB.collection(classOf[MockObject], classOf[String])
coll.getWriteConcern must_== coll.getDB.getMongo.getWriteConcern
}
}

"use configured write concern if configured" in new Setup {
implicit val app = fakeApp(Map("mongodb.defaultWriteConcern" -> "majority"))
running(app) {
val coll = MongoDB.collection(classOf[MockObject], classOf[String])
coll.getWriteConcern must_== WriteConcern.MAJORITY
}
}
}

trait Setup extends After {
Expand Down

0 comments on commit c2ad67f

Please sign in to comment.