forked from scalikejdbc/scalikejdbc
/
PlayPlugin.scala
125 lines (102 loc) · 4.57 KB
/
PlayPlugin.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2012 Kazuhiro Sera
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package scalikejdbc
import play.api._
/**
* The Play plugin to use ScalikeJDBC
*/
class PlayPlugin(implicit app: Application) extends Plugin
with FixtureSupport {
import PlayPlugin._
// Play DB configuration
private[this] lazy val playDbConfig = app.configuration.getConfig("db").getOrElse(Configuration.empty)
// ScalikeJDBC global configuration
private[this] lazy val globalConfig = app.configuration.getConfig("scalikejdbc.global").getOrElse(Configuration.empty)
private[this] lazy val playConfig = app.configuration.getConfig("scalikejdbc.play").getOrElse(Configuration.empty)
private[this] val loggingSQLAndTime = "loggingSQLAndTime"
private[this] var closeAllOnStop = true
override def onStart(): Unit = {
playDbConfig.subKeys map {
name =>
def load(name: String): (String, String, String, ConnectionPoolSettings) = {
implicit val config = playDbConfig
Class.forName(require(name, "driver"))
val default = ConnectionPoolSettings()
val settings = ConnectionPoolSettings(
initialSize = opt(name, "poolInitialSize").map(v => v.toInt).getOrElse(default.initialSize),
maxSize = opt(name, "poolMaxSize").map(v => v.toInt).getOrElse(default.maxSize),
validationQuery = opt(name, "poolValidationQuery").getOrElse(default.validationQuery)
)
(require(name, "url"), opt(name, "user").getOrElse(""), opt(name, "password").getOrElse(""), settings)
}
registeredPoolNames.synchronized {
name match {
case "global" =>
// because "db.global" was used as "scalikejdbc.global" previously
Logger(classOf[PlayPlugin]).warn("Configuration with \"db.global\" is ignored. Use \"scalikejdbc.global\" instead.")
case "default" =>
if (!registeredPoolNames.contains("default")) {
val (url, user, password, settings) = load(name)
ConnectionPool.singleton(url, user, password, settings)
registeredPoolNames.append("default")
}
case _ =>
if (!registeredPoolNames.contains(name)) {
val (url, user, password, settings) = load(name)
ConnectionPool.add(Symbol(name), url, user, password, settings)
registeredPoolNames.append(name)
}
}
}
}
opt(loggingSQLAndTime, "enabled")(globalConfig).map(_.toBoolean).foreach {
enabled =>
implicit val config = globalConfig
val default = LoggingSQLAndTimeSettings()
GlobalSettings.loggingSQLAndTime = LoggingSQLAndTimeSettings(
enabled = enabled,
logLevel = opt(loggingSQLAndTime, "logLevel").map(v => Symbol(v)).getOrElse(default.logLevel),
warningEnabled = opt(loggingSQLAndTime, "warningEnabled").map(_.toBoolean).getOrElse(default.warningEnabled),
warningThresholdMillis = opt(loggingSQLAndTime, "warningThresholdMillis").map(_.toLong).getOrElse(default.warningThresholdMillis),
warningLogLevel = opt(loggingSQLAndTime, "warningLogLevel").map(v => Symbol(v)).getOrElse(default.warningLogLevel)
)
}
opt("closeAllOnStop", "enabled")(playConfig).foreach { enabled => closeAllOnStop = enabled.toBoolean }
if (Play.isTest || Play.isDev) {
loadFixtures()
}
}
override def onStop(): Unit = {
if (Play.isTest || Play.isDev) {
cleanFixtures()
}
if (closeAllOnStop) {
ConnectionPool.closeAll()
registeredPoolNames.clear()
}
}
}
object PlayPlugin {
private val registeredPoolNames = new scala.collection.mutable.ListBuffer[String]
def opt(name: String, key: String)(implicit config: Configuration): Option[String] = {
config.getString(name + "." + key)
}
def require(name: String, key: String)(implicit config: Configuration): String = {
config.getString(name + "." + key) getOrElse {
throw config.reportError(name, "Missing configuration [db." + name + "." + key + "]")
}
}
}