Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement Plugin#enable which lets you disable a plugin if configurat…

…ion is missing or a user turned off the given plugin, db and db evolution plugins take advante of the new mechanism already by checking if db.xx configuration exists
  • Loading branch information...
commit 58286f1914b724502995e3ca14ec07a24217e208 1 parent d9f9c82
@pk11 pk11 authored
View
1  .gitignore
@@ -1,6 +1,7 @@
target/
logs/
repository/
+resources/
dist/
*.lock
*.komodoproject
View
5 framework/play/src/main/java/play/Plugin.java
@@ -16,5 +16,6 @@ public void onStart() {
*/
public void onStop() {
}
-
-}
+
+ public boolean enabled() { return true;}
+}
View
21 framework/play/src/main/scala/play/api/Application.scala
@@ -15,7 +15,7 @@ import scala.collection.JavaConverters._
* for example in case of unit testing, you can easily achieve it using:
*
* {{{
- * val application = Application(new File("."), this.getClass.getClassloader, None, Play.Mode.DEV)
+ * val application = Application(new File("."), this.getClass.getClassloader, None, Play.Mode.Dev)
* }}}
*
* It will create an application using the current classloader.
@@ -69,7 +69,7 @@ case class Application(path: File, classloader: ApplicationClassLoader, sources:
* The configuration used by this application.
* @see play.api.Configuration
*/
- val configuration = Configuration.fromFile(new File(path, "conf/application.conf"))
+ lazy val configuration = Configuration.fromFile(new File(path, "conf/application.conf"))
// Reconfigure logger
{
@@ -113,11 +113,13 @@ case class Application(path: File, classloader: ApplicationClassLoader, sources:
plugins.asInput.slurpString.split("\n").map(_.trim).filterNot(_.isEmpty).map {
case PluginDeclaration(priority, className) => {
try {
- Integer.parseInt(priority) -> classloader.loadClass(className).getConstructor(classOf[Application]).newInstance(this).asInstanceOf[Plugin]
+ val plugin = Integer.parseInt(priority) -> classloader.loadClass(className).getConstructor(classOf[Application]).newInstance(this).asInstanceOf[Plugin]
+ if (plugin._2.enabled) Some(plugin) else { Logger.warn("Plugin [" + className + "] is disabled"); None }
} catch {
case e: java.lang.NoSuchMethodException => {
try {
- Integer.parseInt(priority) -> classloader.loadClass(className).getConstructor(classOf[play.Application]).newInstance(new play.Application(this)).asInstanceOf[Plugin]
+ val plugin = Integer.parseInt(priority) -> classloader.loadClass(className).getConstructor(classOf[play.Application]).newInstance(new play.Application(this)).asInstanceOf[Plugin]
+ if (plugin._2.enabled) Some(plugin) else { Logger.warn("Plugin [" + className + "] is disabled"); None }
} catch {
case e => throw PlayException(
"Cannot load plugin",
@@ -132,7 +134,7 @@ case class Application(path: File, classloader: ApplicationClassLoader, sources:
}
}
}
- }.flatten.toList.sortBy(_._1).map(_._2)
+ }.flatten.toList.flatten.sortBy(_._1).map(_._2)
}
@@ -163,7 +165,12 @@ case class Application(path: File, classloader: ApplicationClassLoader, sources:
* @return The plugin instance used by this application.
* @throws Error if no plugins of type T are loaded by this application.
*/
- def plugin[T](pluginClass: Class[T]): T = plugins.find(_.getClass == pluginClass).get.asInstanceOf[T]
+ def plugin[T](pluginClass: Class[T]): T = plugins.find(_.getClass == pluginClass).getOrElse {
+ throw PlayException(
+ "you are trying to access Plugin[" + pluginClass.toString + "] which is likely disabled",
+ "",
+ None)
+ }.asInstanceOf[T]
/**
* Retrieve a file relatively to the application root path.
@@ -251,4 +258,4 @@ case class Application(path: File, classloader: ApplicationClassLoader, sources:
}.get))
}
-}
+}
View
4 framework/play/src/main/scala/play/api/Plugins.scala
@@ -28,5 +28,9 @@ trait Plugin {
*/
def onStop {}
+ /**
+ * Is the plugin enabled?
+ */
+ def enabled: Boolean = true
}
View
3  framework/play/src/main/scala/play/api/db/DB.scala
@@ -178,9 +178,12 @@ class DBPlugin(app: Application) extends Plugin {
}.getOrElse(Map.empty))
}
+ override def enabled = if (db.datasources.size > 0) true else false
+
/**
* Retrieve the underlying DB Api managing the datasources.
*/
+
def api = db
/**
View
1  framework/play/src/main/scala/play/api/db/evolutions/Evolutions.scala
@@ -385,6 +385,7 @@ class EvolutionsPlugin(app: Application) extends Plugin {
import Evolutions._
+ override def enabled = app.configuration.getSub("db").isDefined
/**
* Check the evolutions state.
*/
Please sign in to comment.
Something went wrong with that request. Please try again.