Skip to content

Commit

Permalink
plugin system use proxy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuchitama committed Jan 10, 2015
1 parent 9d56d72 commit 9c88693
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/scala/ScalatraBootstrap.scala
@@ -1,12 +1,16 @@
import _root_.servlet.{PluginActionInvokeFilter, BasicAuthenticationFilter, TransactionFilter}
import app._
import service.ProxyService

//import jp.sf.amateras.scalatra.forms.ValidationJavaScriptProvider
import org.scalatra._
import javax.servlet._
import java.util.EnumSet

class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
ProxyService.setProxy()

// Register TransactionFilter and BasicAuthenticationFilter at first
context.addFilter("transactionFilter", new TransactionFilter)
context.getFilterRegistration("transactionFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*")
Expand Down
19 changes: 12 additions & 7 deletions src/main/scala/plugin/PluginUpdateJob.scala
@@ -1,12 +1,13 @@
package plugin

import util.Directory._
import org.eclipse.jgit.api.Git
import org.slf4j.LoggerFactory
import org.quartz.{Scheduler, JobExecutionContext, Job}
import org.quartz.JobBuilder._
import org.quartz.TriggerBuilder._
import org.quartz.SimpleScheduleBuilder._
import org.quartz.TriggerBuilder._
import org.quartz.{Job, JobExecutionContext, Scheduler}
import org.slf4j.LoggerFactory
import service.ProxyService
import util.Directory._

class PluginUpdateJob extends Job {

Expand All @@ -16,7 +17,6 @@ class PluginUpdateJob extends Job {
/**
* Clone or pull all plugin repositories
*
* TODO Support plugin repository access through the proxy server
*/
override def execute(context: JobExecutionContext): Unit = {
try {
Expand All @@ -33,7 +33,13 @@ class PluginUpdateJob extends Job {
Git.open(repo).pull().call()
} else {
// clone if the repository is not exist
Git.cloneRepository().setURI(repository.url).setDirectory(repo).call()
val git = Git.cloneRepository().setURI(repository.url).setDirectory(repo).call()
val repositoryConfig = git.getRepository.getConfig

if (ProxyService.isUseProxy) {
repositoryConfig.setString("remote", "origin", "proxy", ProxyService.proxyAddress)
repositoryConfig.save()
}
}
}
logger.info("End plugin information updating")
Expand All @@ -48,7 +54,6 @@ class PluginUpdateJob extends Job {
}

object PluginUpdateJob {

def schedule(scheduler: Scheduler): Unit = {
val job = newJob(classOf[PluginUpdateJob])
.withIdentity("pluginUpdateJob")
Expand Down
46 changes: 46 additions & 0 deletions src/main/scala/service/ProxyService.scala
@@ -0,0 +1,46 @@
package service

import java.io.IOException
import java.net.{Proxy, SocketAddress, URI, ProxySelector, InetSocketAddress}

import org.slf4j.LoggerFactory

trait ProxyService {

protected val logger = LoggerFactory.getLogger(this.getClass)

private val proxyHost = Option(System.getProperty("http.proxyHost"))
private val proxyPort = Option(System.getProperty("http.proxyPort").toInt)

protected val proxy:Proxy = {
(for(host <- proxyHost; port <- proxyPort) yield {
val socket = InetSocketAddress.createUnresolved(host, port)
new Proxy(Proxy.Type.HTTP, socket)
}).getOrElse(Proxy.NO_PROXY)
}

lazy val isUseProxy = proxy != Proxy.NO_PROXY

lazy val proxyAddress:String = Option(proxy.address).map(_.toString).getOrElse("")

}

object ProxyService extends ProxyService {
def setProxy() {
ProxySelector.setDefault(new ProxySelector {

import scala.collection.JavaConverters._

override def select(uri: URI): java.util.List[Proxy] = {
logger.info(s"proxy set: ${proxyAddress}")
List(proxy).asJava
}

override def connectFailed(uri: URI, socketAddress: SocketAddress, e: IOException): Unit = {
if (uri == null || socketAddress == null || e == null) {
throw new IllegalArgumentException("Arguments can not be null.")
}
}
})
}
}

0 comments on commit 9c88693

Please sign in to comment.