package play.api
import play.api.inject.Injector
import play.utils.{ Reflect, PlayIO }
import scala.collection.IndexedSeqLike
@deprecated("Use modules instead", since = "2.4.0")
trait Plugin {
def onStart() {}
def onStop() {}
def enabled: Boolean = true
}
private[play] object Plugin {
type Deprecated = Plugin
}
class Plugins(plugins: => IndexedSeq[Plugin.Deprecated]) extends IndexedSeqLike[Plugin.Deprecated, IndexedSeq[Plugin.Deprecated]] with IndexedSeq[Plugin.Deprecated] {
private lazy val thePlugins = plugins
def length = thePlugins.length
def apply(idx: Int) = thePlugins(idx)
}
object Plugins {
def loadPluginClassNames(env: Environment): Seq[String] = {
import scala.collection.JavaConverters._
val PluginDeclaration = """([0-9_]+):(.*)""".r
val pluginFiles = env.classLoader.getResources("play.plugins").asScala.toList
pluginFiles.distinct.map { plugins =>
PlayIO.readUrlAsString(plugins).split("\n").map(_.replaceAll("#.*$", "").trim).filterNot(_.isEmpty).map {
case PluginDeclaration(priority, className) => (priority.toInt, className)
}
}.flatten.sortBy(_._1).map(_._2)
}
def loadPlugins(classNames: Seq[String], env: Environment, injector: Injector): Seq[Plugin.Deprecated] = {
classNames.map { className =>
val clazz = Reflect.getClass[Plugin.Deprecated](className, env.classLoader)
injector.instanceOf(clazz)
}.filter(_.enabled)
}
def apply(env: Environment, injector: Injector): Plugins = {
val classNames = loadPluginClassNames(env)
new Plugins(loadPlugins(classNames, env, injector).toIndexedSeq)
}
def empty = new Plugins(IndexedSeq.empty)
}