From 9e2c66c341b60662f284c5e67c509e9f83d57667 Mon Sep 17 00:00:00 2001 From: takezoe Date: Sat, 8 Mar 2014 14:48:58 +0900 Subject: [PATCH] (refs #115)ServletContext is passed to Command --- src/main/scala/servlet/GitRepositoryServlet.scala | 2 +- src/main/scala/ssh/GitCommand.scala | 9 +++++---- src/main/scala/ssh/SshServerListener.scala | 9 +-------- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index 7eedd2137..b909ba12b 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -64,7 +64,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest] defining(request.paths){ paths => val owner = paths(1) val repository = paths(2).replaceFirst("\\.git$", "") - val baseURL = request.getRequestURL.toString.replaceFirst("/git/.*", "") + val baseURL = request.getRequestURL.toString.replaceFirst("/git/.*", "") // TODO Use base URL in SystemSettings logger.debug("repository:" + owner + "/" + repository) logger.debug("baseURL:" + baseURL) diff --git a/src/main/scala/ssh/GitCommand.scala b/src/main/scala/ssh/GitCommand.scala index 46a5566cd..909e92b03 100644 --- a/src/main/scala/ssh/GitCommand.scala +++ b/src/main/scala/ssh/GitCommand.scala @@ -11,6 +11,7 @@ import org.apache.sshd.server.command.UnknownCommand import servlet.{Database, CommitLogHook} import service.SystemSettingsService import org.eclipse.jgit.errors.RepositoryNotFoundException +import javax.servlet.ServletContext object GitCommand { @@ -94,7 +95,7 @@ class GitUploadPack(override val command: String) extends GitCommand(command: St } -class GitReceivePack(override val command: String) extends GitCommand(command: String) with SystemSettingsService { +class GitReceivePack(context: ServletContext, override val command: String) extends GitCommand(command: String) with SystemSettingsService { // TODO Correct this info. where i get base url? val BaseURL: String = loadSystemSettings().baseUrl.getOrElse("http://localhost:8080") @@ -104,7 +105,7 @@ class GitReceivePack(override val command: String) extends GitCommand(command: S val repository = git.getRepository val receive = new ReceivePack(repository) receive.setPostReceiveHook(new CommitLogHook(owner, repositoryName, user, BaseURL)) - Database(SshServer.getServletContext) withTransaction { + Database(context) withTransaction { receive.receive(in, out, err) } } @@ -112,14 +113,14 @@ class GitReceivePack(override val command: String) extends GitCommand(command: S } -class GitCommandFactory extends CommandFactory { +class GitCommandFactory(context: ServletContext) extends CommandFactory { private val logger = LoggerFactory.getLogger(classOf[GitCommandFactory]) override def createCommand(command: String): Command = { logger.debug(s"command: $command") command match { case GitCommand.CommandRegex("upload", owner, repoName) => new GitUploadPack(command) - case GitCommand.CommandRegex("receive", owner, repoName) => new GitReceivePack(command) + case GitCommand.CommandRegex("receive", owner, repoName) => new GitReceivePack(context, command) case _ => new UnknownCommand(command) } } diff --git a/src/main/scala/ssh/SshServerListener.scala b/src/main/scala/ssh/SshServerListener.scala index 477f3a1a3..1c13b00ea 100644 --- a/src/main/scala/ssh/SshServerListener.scala +++ b/src/main/scala/ssh/SshServerListener.scala @@ -15,20 +15,15 @@ object SshServer { private val server = org.apache.sshd.SshServer.setUpDefaultServer() - // TODO think other way. this is for create database session - private var context: ServletContext = null - - private def configure() = { server.setPort(DEFAULT_PORT) // TODO read from config server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(s"${Directory.GitBucketHome}/gitbucket.ser")) server.setPublickeyAuthenticator(new PublicKeyAuthenticator) - server.setCommandFactory(new GitCommandFactory) + server.setCommandFactory(new GitCommandFactory(context)) } def start(context: ServletContext) = this.synchronized { if (SSH_SERVICE_ENABLE) { - this.context = context configure() server.start() logger.info(s"Start SSH Server Listen on ${server.getPort}") @@ -38,8 +33,6 @@ object SshServer { def stop() = { server.stop(true) } - - def getServletContext = this.context } /*