mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 14:05:52 +01:00
Experimental JDBC API for JavaScript plug-ins
This commit is contained in:
@@ -4,6 +4,11 @@ import org.mozilla.javascript.{Context => JsContext}
|
||||
import org.mozilla.javascript.{Function => JsFunction}
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import plugin.PluginSystem._
|
||||
import util.ControlUtil._
|
||||
import plugin.PluginSystem.GlobalMenu
|
||||
import plugin.PluginSystem.RepositoryAction
|
||||
import plugin.PluginSystem.Action
|
||||
import plugin.PluginSystem.RepositoryMenu
|
||||
|
||||
class JavaScriptPlugin(val id: String, val version: String,
|
||||
val author: String, val url: String, val description: String) extends Plugin {
|
||||
@@ -62,6 +67,30 @@ class JavaScriptPlugin(val id: String, val version: String,
|
||||
})
|
||||
}
|
||||
|
||||
object db {
|
||||
// TODO Use JavaScript Map instead of java.util.Map
|
||||
def select(sql: String): Array[java.util.Map[String, String]] = {
|
||||
defining(PluginConnectionHolder.threadLocal.get){ conn =>
|
||||
using(conn.prepareStatement(sql)){ stmt =>
|
||||
using(stmt.executeQuery()){ rs =>
|
||||
val list = new java.util.ArrayList[java.util.Map[String, String]]()
|
||||
while(rs.next){
|
||||
defining(rs.getMetaData){ meta =>
|
||||
val map = new java.util.HashMap[String, String]()
|
||||
Range(1, meta.getColumnCount).map { i =>
|
||||
val name = meta.getColumnName(i)
|
||||
map.put(name, rs.getString(name))
|
||||
}
|
||||
list.add(map)
|
||||
}
|
||||
}
|
||||
list.toArray(new Array[java.util.Map[String, String]](list.size))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object JavaScriptPlugin {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package plugin
|
||||
|
||||
import plugin.PluginSystem._
|
||||
import java.sql.Connection
|
||||
|
||||
trait Plugin {
|
||||
val id: String
|
||||
@@ -14,3 +15,7 @@ trait Plugin {
|
||||
def repositoryActions : List[RepositoryAction]
|
||||
def globalActions : List[Action]
|
||||
}
|
||||
|
||||
object PluginConnectionHolder {
|
||||
val threadLocal = new ThreadLocal[Connection]
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import twirl.api.Html
|
||||
import service.{AccountService, RepositoryService, SystemSettingsService}
|
||||
import model.Account
|
||||
import util.{JGitUtil, Keys}
|
||||
import plugin.PluginConnectionHolder
|
||||
|
||||
class PluginActionInvokeFilter extends Filter with SystemSettingsService with RepositoryService with AccountService {
|
||||
|
||||
@@ -58,7 +59,12 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
|
||||
val systemSettings = loadSystemSettings()
|
||||
getRepository(owner, name, systemSettings.baseUrl(request)).flatMap { repository =>
|
||||
plugin.PluginSystem.repositoryActions.find(_.path == remain).map { action =>
|
||||
val result = action.function(request, response, repository)
|
||||
val result = try {
|
||||
PluginConnectionHolder.threadLocal.set(session.conn)
|
||||
action.function(request, response, repository)
|
||||
} finally {
|
||||
PluginConnectionHolder.threadLocal.remove()
|
||||
}
|
||||
result match {
|
||||
case x: String => {
|
||||
response.setContentType("text/html; charset=UTF-8")
|
||||
|
||||
Reference in New Issue
Block a user