Skip to content

Commit

Permalink
Extract the DataAccessingPlugin from BasePlugin, create some helper s…
Browse files Browse the repository at this point in the history
…tuff.
  • Loading branch information
ForNeVeR committed Feb 16, 2015
1 parent 9916f07 commit 72eb400
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/ru/org/codingteam/horta/core/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ object Core {
pluginDefinitions.map(t => t._2.name -> t._2.commands.map(cd => cd.name -> cd.level)).toMap

private def getStorages(pluginDefinitions: List[(ActorRef, PluginDefinition)]): Map[String, DAO] = {
pluginDefinitions.map(_._2).filter(_.dao.isDefined).map(definition => (definition.name, definition.dao.get)).toMap
pluginDefinitions.map(_._2).filter(_.repository.isDefined).map(definition => (definition.name, definition.repository.get)).toMap
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ru.org.codingteam.horta.database

import javax.sql.DataSource

import akka.actor.{ActorRef, Actor, ActorLogging}
import akka.actor.{Actor, ActorLogging, ActorSelection}
import akka.pattern.ask
import akka.util.Timeout
import com.googlecode.flyway.core.Flyway
Expand Down Expand Up @@ -78,7 +78,7 @@ object PersistentStore {

private case class Execute(plugin: String, action: (Repository, DBSession) => Any)

def execute[Repository, T: ClassTag](plugin: String, store: ActorRef)
def execute[Repository, T: ClassTag](plugin: String, store: ActorSelection)
(action: (Repository, DBSession) => T)
(implicit timeout: Timeout): Future[T] = {
val message = Execute(plugin, (r, s) => action(r.asInstanceOf[Repository], s))
Expand Down
13 changes: 4 additions & 9 deletions src/main/scala/ru/org/codingteam/horta/plugins/BasePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import akka.actor.{Actor, ActorLogging}
import ru.org.codingteam.horta.database.DAO

/**
* CommandPlugin class used as base for all command plugins.
* Common plugin functionality. Every plugin should be inherited from this class.
*/
abstract class BasePlugin extends Actor with ActorLogging {

def receive = {
case GetPluginDefinition => sender ! pluginDefinition
}

protected val core = context.actorSelection("/user/core")
protected val store = context.actorSelection("/user/core/store")

/**
* Plugin name.
Expand All @@ -32,15 +32,10 @@ abstract class BasePlugin extends Actor with ActorLogging {
*/
protected def commands: List[CommandDefinition] = List()

/**
* Plugin data access object. May be None if not present.
* @return data access object.
*/
protected def dao: Option[DAO] = None

/**
* A full plugin definition.
* @return plugin definition.
*/
private def pluginDefinition = PluginDefinition(name, notifications, commands, dao)
protected def pluginDefinition = PluginDefinition(name, notifications, commands, None)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.org.codingteam.horta.plugins

import akka.util.Timeout
import ru.org.codingteam.horta.database.PersistentStore
import scalikejdbc.DBSession

import scala.concurrent.Future

/**
* Trait for the plugins that can access the database.
*/
trait DataAccessingPlugin[Repository <: ru.org.codingteam.horta.database.Repository] extends BasePlugin {

/**
* A full plugin definition.
* @return plugin definition.
*/
override protected def pluginDefinition: PluginDefinition = super.pluginDefinition.copy(repository = Some(repository))

/**
* Repository used for data access in this plugin.
*/
protected abstract val repository: Repository

/**
* Execute repository action. Use with caution - do not mess with plugin data members within action; that is not
* context-safe.
*
* @param action an action.
* @tparam T type of action result.
* @return future that will be resolved after action execution.
*/
protected def withDatabase[T](action: (Repository, DBSession) => T)(implicit timeout: Timeout): Future[T] = {
PersistentStore.execute[Repository, T](name, store) { case (r, s) => action(r, s) }
}

private val store = context.actorSelection("/user/core/store")

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.org.codingteam.horta.plugins

import ru.org.codingteam.horta.database.DAO
import ru.org.codingteam.horta.database.{Repository}

/**
* Description of events.
Expand All @@ -17,9 +17,9 @@ case class Notifications(messages: Boolean,
* @param name plugin name.
* @param notifications description of events plugin want to be notified of.
* @param commands a list of commands supported by the plugin.
* @param dao plugin data access object if present.
* @param repository a repository for data access. Data access is disabled if repository isn't defined.
*/
case class PluginDefinition(name: String,
notifications: Notifications,
commands: List[CommandDefinition],
dao: Option[DAO])
repository: Option[Repository])

0 comments on commit 72eb400

Please sign in to comment.