<!-- Official MongoDB scala driver -->
<!-- For issues with the driver see: https://jira.mongodb.org/browse/SCALA -->
- <dependency>
- <groupId>com.mongodb.casbah</groupId>
- <artifactId>casbah-core_2.9.1</artifactId>
- <version>2.1.5-1</version>
- </dependency>
+ <!--<dependency>-->
+ <!--<groupId>com.mongodb.casbah</groupId>-->
+ <!--<artifactId>casbah-core_2.9.1</artifactId>-->
+ <!--<version>2.1.5-1</version>-->
+ <!--</dependency>-->
</dependencies>
</project>
--- /dev/null
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.store
+package mongodb
+
+import confmodel.MongoDBCollectionModel
+import com.mongodb.{WriteConcern, BasicDBObject}
+import gr.grnet.aquarium.util.Loggable
+
+/**
+ *
+ * @author Christos KK Loverdos <loverdos@gmail.com>.
+ */
+class MongoDBCollection(private[mongodb] val owner: MongoDBConnection, confModel: MongoDBCollectionModel) extends MessageStore with Loggable {
+ def name = confModel.name
+
+ private[mongodb] lazy val (_mongoDB, _mongoCollection) = {
+ val db = owner._mongo.getDB(confModel.mongoDBName)
+ val coll = db.getCollection(confModel.mongoCollectionName)
+ (db, coll)
+ }
+
+ def storeString(message: String) = {
+ val obj = new BasicDBObject("event", message)
+ val writeResult = _mongoCollection.insert(obj, WriteConcern.valueOf(confModel.writeConcern))
+ logger.debug("Wrote message %s and got result %s".format(message, writeResult))
+ null
+ }
+}
\ No newline at end of file
package gr.grnet.aquarium.store
package mongodb
-import confmodel.MongoDBConfigurationModel
-import com.mongodb.casbah.MongoConnection
-import com.mongodb.{WriteConcern, ServerAddress}
+import confmodel.MongoDBConnectionModel
import gr.grnet.aquarium.util.Loggable
+import scala.collection.JavaConversions._
+import com.mongodb.{Mongo, WriteConcern, ServerAddress}
/**
*
* @author Christos KK Loverdos <loverdos@gmail.com>.
*/
-class MongoDBConnection(val confModel: MongoDBConfigurationModel) extends Loggable {
+class MongoDBConnection(val confModel: MongoDBConnectionModel) extends Loggable {
- private[mongodb] lazy val _mongoConnection = {
+ private[mongodb] lazy val _mongo = {
val hosts = confModel.hosts
- val serverAddresses = hosts.map(sacm => new ServerAddress(sacm.host, sacm.port))
- val mongo = MongoConnection(serverAddresses)
- logger.info("Created MongoDB connection %s for hosts %s".format(mongo, confModel.hosts.map(h => "%s:%s".format(h.host, h.port)).mkString(", ")))
-
- if(confModel.slaveOK) {
- mongo.slaveOk()
- logger.info("Set slaveOK for MongoDB connection %s".format(mongo))
+ val mongo = if(hosts.size == 1) {
+ val sacm = hosts.head
+ new Mongo(sacm.host, sacm.port)
+ } else {
+ val serverAddresses = hosts.map(sacm => new ServerAddress(sacm.host, sacm.port))
+ new Mongo(serverAddresses)
}
- val writeConcern = WriteConcern.valueOf(confModel.writeConcern)
- mongo.setWriteConcern(writeConcern)
- logger.info("Set WriteConcern %s for MongoDB connection %s".format(confModel.writeConcern, mongo))
mongo
}
+
+
+ private lazy val _collections = confModel.collections.map(new MongoDBCollection(this, _))
+ def collections: List[MongoDBCollection] = _collections
+
+ def findCollection(name: String): Option[MongoDBCollection] = collections.find(_.name == name)
}
object MongoDBConnection {
object DBNames {
val test = "test"
+ val aquarium = "aquarium"
}
object CollectionNames {
- val test = "test"
- }
+ val test = "test"
+ val events = "events"
+ }
}
\ No newline at end of file
*/
class MongoDBStore(connection: MongoDBConnection) extends MessageStore {
def storeString(message: String) = {
- val mongo = connection._mongoConnection
+ val mongo = connection._mongo
// FIXME: implement
NoVal
--- /dev/null
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.store.mongodb
+package confmodel
+
+/**
+ *
+ * @author Christos KK Loverdos <loverdos@gmail.com>.
+ */
+case class MongoDBCollectionModel(name: String, mongoDBName: String, mongoCollectionName: String, writeConcern: String)
\ No newline at end of file
package gr.grnet.aquarium.store.mongodb
package confmodel
-import com.mongodb.ServerAddress
////////////////////////////////////////////////////////////////////////////
// The WriteConcerns are as follows:
*
* @author Christos KK Loverdos <loverdos@gmail.com>.
*/
-case class MongoDBConfigurationModel(
+case class MongoDBConnectionModel(
hosts: List[ServerAddressConfigurationModel],
- slaveOK: Boolean,
- writeConcern: String)
\ No newline at end of file
+ collections: List[MongoDBCollectionModel])
\ No newline at end of file
* or implied, of GRNET S.A.
*/
-package gr.grnet.aquarium.store.mongodb.confmodel
+package gr.grnet.aquarium.store.mongodb
+package confmodel
/**
*
prepareXStreamAlias[RabbitMQConsumerModel](xs)
// MongoDB
- prepareXStreamAlias[MongoDBConfigurationModel](xs)
+ prepareXStreamAlias[MongoDBConnectionModel](xs)
+ prepareXStreamAlias[MongoDBCollectionModel](xs)
prepareXStreamAlias[ServerAddressConfigurationModel](xs)
xs.alias("List", classOf[::[_]])
-<MongoDBConfigurationModel>
- <slaveOK>true</slaveOK>
- <writeConcern>SAFE</writeConcern>
+<MongoDBConnectionModel>
<hosts class="List">
<ServerAddressConfigurationModel>
<host>aquarium.dev.grnet.gr</host>
<port>27017</port>
</ServerAddressConfigurationModel>
</hosts>
-</MongoDBConfigurationModel>
+
+ <collections class="List">
+ <MongoDBCollectionModel>
+ <name>events</name>
+
+ <mongoDBName>aquarium</mongoDBName>
+ <mongoCollectionName>events</mongoCollectionName>
+ <writeConcern>SAFE</writeConcern>
+ </MongoDBCollectionModel>
+ </collections>
+</MongoDBConnectionModel>
-<MongoDBConfigurationModel>
- <slaveOK>true</slaveOK>
- <writeConcern>SAFE</writeConcern>
+<MongoDBConnectionModel>
<hosts class="List">
<ServerAddressConfigurationModel>
<host>localhost</host>
<port>27017</port>
</ServerAddressConfigurationModel>
</hosts>
-</MongoDBConfigurationModel>
+
+ <collections class="List">
+ <MongoDBCollectionModel>
+ <name>events</name>
+
+ <mongoDBName>aquarium</mongoDBName>
+ <mongoCollectionName>events</mongoCollectionName>
+ <writeConcern>SAFE</writeConcern>
+ </MongoDBCollectionModel>
+ </collections>
+</MongoDBConnectionModel>
package store
package mongodb
-import confmodel.{ServerAddressConfigurationModel, MongoDBConfigurationModel}
+import confmodel.{ServerAddressConfigurationModel, MongoDBConnectionModel}
import org.junit.Test
import org.junit.Assert._
import org.junit.Assume.assumeTrue
import com.ckkloverdos.resource.DefaultResourceContext
import MongoDBConnection.{RCFolders, PropFiles, DBNames, CollectionNames}
import gr.grnet.aquarium.util.xstream.XStreamHelpers
-import com.mongodb.casbah.commons.MongoDBObject
import com.ckkloverdos.sys.SysProp
import util.Loggable
+import com.mongodb.{BasicDBObject, DBObject}
+import com.ckkloverdos.maybe.Failed
/**
*
val xs = XStreamHelpers.newXStream
lazy val MongoDBPropFile = {
- val filename = SysProp(PropertyNames.MongoDBConfFile).value.getOr(PropFiles.local_message_store)
+ val filename = SysProp(PropertyNames.MongoDBConfFile).value.getOr(PropFiles.aquarium_message_store)
logger.debug("Using mongodb configuration from %s".format(filename))
filename
}
- private def _getTestConf: String = {
- val address1 = ServerAddressConfigurationModel("aquarium.dev.grnet.gr", 27017)
- val model = new MongoDBConfigurationModel(List(address1), true, "SAFE")
- val xml = xs.toXML(model)
- xml
- }
+// private def _getTestConf: String = {
+// val address1 = ServerAddressConfigurationModel("aquarium.dev.grnet.gr", 27017)
+// val model = new MongoDBConnectionModel(List(address1), true, )
+// val xml = xs.toXML(model)
+// xml
+// }
@Test
def testConfigurationExists: Unit = {
def testConnection: Unit = {
assumeTrue(LogicTestsAssumptions.EnableMongoDBTests)
+ assertTrue(mongodbRC.getLocalResource(MongoDBPropFile).isJust)
for {
confResource <- mongodbRC.getLocalResource(MongoDBPropFile)
} {
- val xs = XStreamHelpers.newXStream
- logger.debug("Reading mongodb configuration from %s".format(confResource.url))
- logger.debug("mongodb configuration is:\n%s".format(confResource.stringContent.getOr("")))
- val maybeModel = XStreamHelpers.parseType[MongoDBConfigurationModel](confResource, xs)
+ val maybeModel = XStreamHelpers.parseType[MongoDBConnectionModel](confResource, xs)
+ maybeModel match {
+ case Failed(e, m) => throw e
+ case _ =>
+ }
assertTrue(maybeModel.isJust)
- val obj = MongoDBObject("1" -> "one", "2" -> "two")
- logger.debug("Inserting %s into mongodb".format(obj))
+
for(model <- maybeModel) {
+ logger.debug("Reading mongodb configuration from %s".format(confResource.url))
+ logger.debug("mongodb configuration is:\n%s".format(confResource.stringContent.getOr("")))
+ val obj = new BasicDBObject("1", 1)
+ logger.debug("Inserting %s into mongodb".format(obj))
val mongo = new MongoDBConnection(model)
- val db = mongo._mongoConnection(DBNames.test)
- val collection = db.apply(CollectionNames.test)
- collection.insert(obj)
+ val store: Option[MessageStore] = mongo.findCollection("events")
+
+ store match {
+ case Some(store) =>
+ store.storeString("{a: 1}")
+ case None =>
+ logger.warn("No store found")
+ }
}
}
}