Use ControlUtil.

This commit is contained in:
takezoe
2013-09-21 22:13:15 +09:00
parent 18ca3cbd80
commit e89b2020a3
14 changed files with 272 additions and 278 deletions

View File

@@ -33,5 +33,18 @@ object ControlUtil {
git.getRepository.close
}
def executeIf(condition: => Boolean)(action: => Unit): Boolean =
if(condition){
action
true
} else {
false
}
def optionIf[T](condition: => Boolean)(action: => Option[T]): Option[T] =
if(condition){
action
} else {
None
}
}

View File

@@ -69,29 +69,17 @@ object JGitUtil {
rev.getFullMessage,
rev.getParents().map(_.name).toList)
val summary = {
val i = fullMessage.trim.indexOf("\n")
val firstLine = if(i >= 0){
fullMessage.trim.substring(0, i).trim
} else {
fullMessage
}
if(firstLine.length > shortMessage.length){
shortMessage
} else {
firstLine
val summary = defining(fullMessage.trim.indexOf("\n")){ i =>
defining(if(i >= 0) fullMessage.trim.substring(0, i).trim else fullMessage){ firstLine =>
if(firstLine.length > shortMessage.length) shortMessage else firstLine
}
}
val description = {
val i = fullMessage.trim.indexOf("\n")
if(i >= 0){
val description = defining(fullMessage.trim.indexOf("\n")){ i =>
optionIf(i >= 0){
Some(fullMessage.trim.substring(i).trim)
} else {
None
}
}
}
case class DiffInfo(changeType: ChangeType, oldPath: String, newPath: String, oldContent: Option[String], newContent: Option[String])
@@ -124,7 +112,7 @@ object JGitUtil {
val revWalk = new RevWalk(git.getRepository)
val revCommit = revWalk.parseAny(objectId) match {
case r: RevTag => revWalk.parseCommit(r.getObject)
case _ => revWalk.parseCommit(objectId)
case _ => revWalk.parseCommit(objectId)
}
revWalk.dispose
revCommit
@@ -462,29 +450,24 @@ object JGitUtil {
}
}
def initRepository(dir: java.io.File): Unit = {
def initRepository(dir: java.io.File): Unit =
using(new RepositoryBuilder().setGitDir(dir).setBare.build){ repository =>
repository.create
setReceivePack(repository)
}
}
def cloneRepository(from: java.io.File, to: java.io.File): Unit = {
val git = Git.cloneRepository.setURI(from.toURI.toString).setDirectory(to).setBare(true).call
try {
def cloneRepository(from: java.io.File, to: java.io.File): Unit =
using(Git.cloneRepository.setURI(from.toURI.toString).setDirectory(to).setBare(true).call){ git =>
setReceivePack(git.getRepository)
} finally {
git.getRepository.close
}
}
def isEmpty(git: Git): Boolean = git.getRepository.resolve(Constants.HEAD) == null
private def setReceivePack(repository: org.eclipse.jgit.lib.Repository): Unit = {
val config = repository.getConfig
config.setBoolean("http", null, "receivepack", true)
config.save
}
private def setReceivePack(repository: org.eclipse.jgit.lib.Repository): Unit =
defining(repository.getConfig){ config =>
config.setBoolean("http", null, "receivepack", true)
config.save
}
def getDefaultBranch(git: Git, repository: RepositoryService.RepositoryInfo,
revstr: String = ""): Option[(ObjectId, String)] = {

View File

@@ -1,10 +1,9 @@
package util
import service.SystemSettingsService.Ldap
import util.ControlUtil._
import service.SystemSettingsService
import com.novell.ldap._
import service.SystemSettingsService.Ldap
import scala.Some
import scala.annotation.tailrec
/**
@@ -96,12 +95,10 @@ object LDAPUtil {
}
}
private def findMailAddress(conn: LDAPConnection, userDN: String, mailAttribute: String): Option[String] = {
val results = conn.search(userDN, LDAPConnection.SCOPE_BASE, null, Array[String](mailAttribute), false)
if (results.hasMore) {
Option(results.next.getAttribute(mailAttribute)).map(_.getStringValue)
} else {
None
private def findMailAddress(conn: LDAPConnection, userDN: String, mailAttribute: String): Option[String] =
defining(conn.search(userDN, LDAPConnection.SCOPE_BASE, null, Array[String](mailAttribute), false)){ results =>
optionIf (results.hasMore) {
Option(results.next.getAttribute(mailAttribute)).map(_.getStringValue)
}
}
}
}

View File

@@ -2,6 +2,7 @@ package util
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.{ReentrantLock, Lock}
import util.ControlUtil._
object LockUtil {
@@ -23,8 +24,7 @@ object LockUtil {
/**
* Synchronizes a given function which modifies the working copy of the wiki repository.
*/
def lock[T](key: String)(f: => T): T = {
val lock = getLockObject(key)
def lock[T](key: String)(f: => T): T = defining(getLockObject(key)){ lock =>
try {
lock.lock()
f

View File

@@ -2,14 +2,15 @@ package util
import java.net.{URLDecoder, URLEncoder}
import org.mozilla.universalchardet.UniversalDetector
import util.ControlUtil._
object StringUtil {
def sha1(value: String): String = {
val md = java.security.MessageDigest.getInstance("SHA-1")
md.update(value.getBytes)
md.digest.map(b => "%02x".format(b)).mkString
}
def sha1(value: String): String =
defining(java.security.MessageDigest.getInstance("SHA-1")){ md =>
md.update(value.getBytes)
md.digest.map(b => "%02x".format(b)).mkString
}
def md5(value: String): String = {
val md = java.security.MessageDigest.getInstance("MD5")
@@ -28,13 +29,13 @@ object StringUtil {
def convertFromByteArray(content: Array[Byte]): String = new String(content, detectEncoding(content))
def detectEncoding(content: Array[Byte]): String = {
val detector = new UniversalDetector(null)
detector.handleData(content, 0, content.length)
detector.dataEnd()
detector.getDetectedCharset match {
case null => "UTF-8"
case e => e
def detectEncoding(content: Array[Byte]): String =
defining(new UniversalDetector(null)){ detector =>
detector.handleData(content, 0, content.length)
detector.dataEnd()
detector.getDetectedCharset match {
case null => "UTF-8"
case e => e
}
}
}
}

View File

@@ -25,10 +25,7 @@ trait Validations {
*/
def date(constraints: Constraint*): SingleValueType[java.util.Date] =
new SingleValueType[java.util.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){
def convert(value: String): java.util.Date = {
val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd")
formatter.parse(value)
}
def convert(value: String): java.util.Date = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(value)
}
}