Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Oct 21, 2017
1 parent 0a55ce0 commit a566b05
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.*
@Controller
abstract class AbstractController {

protected var log = LoggerFactory.getLogger(this.javaClass)
protected var log = LoggerFactory.getLogger(this.javaClass)!!

@Autowired
protected var msg: MessageSource? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DefaultLoginController : AbstractController() {
session.setAttribute("authNMsg", "authenticated")
session.setAttribute("userid", userid)

var target = session.getAttribute("target") as String?
val target = session.getAttribute("target") as String?
if (target == null) {
res.sendRedirect("/admins/main")
} else {
Expand Down Expand Up @@ -115,7 +115,7 @@ class DefaultLoginController : AbstractController() {
}
val admin = userLoginHistory[userid]
return admin != null && admin.loginFailedCount == accountLockCount
&& Date().time - admin.lastLoginFailedTime!!.getTime() < accountLockTime
&& Date().time - admin.lastLoginFailedTime!!.time < accountLockTime
}

protected fun authUser(userId: String?, password: String?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class IndexController : AbstractController() {
ses.removeAttribute("dlpinit")
setViewAndCommonObjects(mav, locale, "index")
mav.addObject("title", "EasyBuggy Bootlin")
var permName: String?
var lblPerm: String?
val permName: String?
val lblPerm: String?
if (System.getProperty("java.version").startsWith("1.7")) {
permName = "PermGen space"
lblPerm = msg?.getMessage("label.permgen.space", null, locale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AuthenticationFilter : Filter {
queryString = queryString.replace("logintype=$loginType&", "")
queryString = queryString.replace("&logintype=" + loginType!!, "")
queryString = queryString.replace("logintype=" + loginType, "")
if (queryString.length > 0) {
if (queryString.isNotEmpty()) {
queryString = "?" + queryString
}
}
Expand All @@ -51,12 +51,10 @@ class AuthenticationFilter : Filter {
/* Not authenticated yet */
session = request.getSession(true)
session!!.setAttribute("target", target)
if (loginType == null) {
response.sendRedirect(response.encodeRedirectURL("/login" + queryString))
} else if ("sessionfixation" == loginType) {
response.sendRedirect(response.encodeRedirectURL("/$loginType/login$queryString"))
} else {
response.sendRedirect("/$loginType/login$queryString")
when (loginType) {
null -> response.sendRedirect(response.encodeRedirectURL("/login" + queryString))
"sessionfixation" -> response.sendRedirect(response.encodeRedirectURL("/$loginType/login$queryString"))
else -> response.sendRedirect("/$loginType/login$queryString")
}
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class User : Serializable {
var lastLoginFailedTime: Date? = null

override fun toString(): String {
return "User dn=$dn [userId=$userId, name=$name, password=$password, secret=" + secret + ", phone=" + phone + ", mail=" + mail + ", loginFailedCount=" + loginFailedCount + ", lastLoginFailedTime=" + lastLoginFailedTime + "]"
return "User dn=$dn [userId=$userId, name=$name, password=$password, secret=$secret, phone=$phone, mail=$mail, loginFailedCount=$loginFailedCount, lastLoginFailedTime=$lastLoginFailedTime]"
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class SlowRegularExpressionController : AbstractController() {
@RequestMapping(value = "/slowre")
fun process(@RequestParam(value = "word", required = false) word: String?, mav: ModelAndView,
locale: Locale): ModelAndView {
var message: String?
val message: String?
setViewAndCommonObjects(mav, locale, "slowregex")
if (!StringUtils.isBlank(word)) {
message = if (!StringUtils.isBlank(word)) {
if (isMatched(word!!)) {
message = msg?.getMessage("msg.match.regular.expression", null, locale)
msg?.getMessage("msg.match.regular.expression", null, locale)
} else {
message = msg?.getMessage("msg.not.match.regular.expression", null, locale)
msg?.getMessage("msg.not.match.regular.expression", null, locale)
}
} else {
message = msg?.getMessage("msg.enter.string", null, locale)
msg?.getMessage("msg.enter.string", null, locale)
}
mav.addObject("msg", message)
return mav
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class StringPlusOperationController : AbstractController() {
log.info("Start Date: {}", Date())
var i = 0
while (i < length && i < MAX_LENGTH) {
s = s + characters[rand.nextInt(characters.size)]
s += characters[rand.nextInt(characters.size)]
i++
// builder.append(characters[rand.nextInt(characters.length)]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class DBConnectionLeakController : AbstractController() {
@Throws(SQLException::class)
private fun selectUsers(): List<User> {
val users = ArrayList<User>()
var conn: Connection?
var stmt: Statement?
var rs: ResultSet?
conn = jdbcTemplate!!.dataSource.connection
val conn: Connection? = jdbcTemplate!!.dataSource.connection
val stmt: Statement?
val rs: ResultSet?
stmt = conn!!.createStatement()
rs = stmt!!.executeQuery("select id, name, phone, mail from users where ispublic = 'true'")
while (rs!!.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.dao.DataAccessException
import org.springframework.dao.DeadlockLoserDataAccessException
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.RowMapper
import org.springframework.stereotype.Controller
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.support.DefaultTransactionDefinition
Expand All @@ -30,10 +29,10 @@ class DeadlockController2 : AbstractController() {
setViewAndCommonObjects(mav, locale, "deadlock2")
// Overwrite title (because title is the same as xee page)
mav.addObject("title", msg?.getMessage("title.xee.page", null, locale))
var users: MutableList<User>?
val users: MutableList<User>?
val order = getOrder(req)
if ("POST" == req.method) {
users = ArrayList<User>()
users = ArrayList()
var j = 0
while (true) {
val uid = req.getParameter("uid_" + (j + 1)) ?: break
Expand All @@ -56,18 +55,18 @@ class DeadlockController2 : AbstractController() {

private fun getOrder(req: HttpServletRequest): String {
var order = req.getParameter("order")
if ("asc" == order) {
order = "desc"
order = if ("asc" == order) {
"desc"
} else {
order = "asc"
"asc"
}
return order
}

private fun selectUsers(order: String, locale: Locale, mav: ModelAndView): MutableList<User>? {
var users: MutableList<User>? = null
try {
users = jdbcTemplate!!.query("select * from users where ispublic = 'true' order by id " + if ("desc" == order) "desc" else "asc", RowMapper<User> { rs, _ ->
users = jdbcTemplate!!.query("select * from users where ispublic = 'true' order by id " + if ("desc" == order) "desc" else "asc", { rs, _ ->
val user = User()
user.userId = rs.getString("id")
user.name = rs.getString("name")
Expand All @@ -77,11 +76,11 @@ class DeadlockController2 : AbstractController() {
})
} catch (e: DataAccessException) {
mav.addObject("errmsg",
msg?.getMessage("msg.db.access.error.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.db.access.error.occur", arrayOf(e.message), null, locale))
log.error("DataAccessException occurs: ", e)
} catch (e: Exception) {
mav.addObject("errmsg",
msg?.getMessage("msg.unknown.exception.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.unknown.exception.occur", arrayOf(e.message), null, locale))
log.error("Exception occurs: ", e)
}

Expand All @@ -95,7 +94,7 @@ class DeadlockController2 : AbstractController() {
var executeUpdate = 0
try {
for (user in users) {
executeUpdate = executeUpdate + jdbcTemplate!!.update("Update users set name = ?, phone = ?, mail = ? where id = ?",
executeUpdate += jdbcTemplate!!.update("Update users set name = ?, phone = ?, mail = ? where id = ?",
user.name, user.phone, user.mail, user.userId)
log.info(user.userId + " is updated.")
Thread.sleep(500)
Expand All @@ -109,12 +108,12 @@ class DeadlockController2 : AbstractController() {
} catch (e: DataAccessException) {
txMgr!!.rollback(trnStatus)
mav.addObject("errmsg",
msg?.getMessage("msg.db.access.error.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.db.access.error.occur", arrayOf(e.message), null, locale))
log.error("DataAccessException occurs: ", e)
} catch (e: Exception) {
txMgr!!.rollback(trnStatus)
mav.addObject("errmsg",
msg?.getMessage("msg.unknown.exception.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.unknown.exception.occur", arrayOf(e.message), null, locale))
log.error("Exception occurs: ", e)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EndlessWaitingController : AbstractController() {
} catch (e: InterruptedException) {
log.error("InterruptedException occurs: ", e)
mav.addObject("errmsg",
msg?.getMessage("msg.unknown.exception.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.unknown.exception.occur", arrayOf(e.message), null, locale))
}

}
Expand All @@ -52,8 +52,8 @@ class EndlessWaitingController : AbstractController() {
private fun createBatchFile(count: Int, tmpdir: String): File? {

val osName = System.getProperty("os.name").toLowerCase()
var batFileName: String?
var firstLine: String?
val batFileName: String?
val firstLine: String?
if (osName.toLowerCase().startsWith("windows")) {
batFileName = "test.bat"
firstLine = "@echo off"
Expand All @@ -62,7 +62,7 @@ class EndlessWaitingController : AbstractController() {
firstLine = "#!/bin/sh"
}

var batFile: File?
val batFile: File?
try {
batFile = File(tmpdir, batFileName)
} catch (e: Exception) {
Expand All @@ -73,10 +73,10 @@ class EndlessWaitingController : AbstractController() {
try {
FileWriter(batFile).use { fileWriter ->
BufferedWriter(fileWriter).use { buffwriter ->
if (!batFile!!.setExecutable(true)) {
if (!batFile.setExecutable(true)) {
log.debug("batFile.setExecutable(true) returns false.")
}
buffwriter.write(firstLine!!)
buffwriter.write(firstLine)
buffwriter.newLine()

var i = 0
Expand All @@ -92,7 +92,7 @@ class EndlessWaitingController : AbstractController() {
fileWriter.close()
if (!osName.toLowerCase().startsWith("windows")) {
val runtime = Runtime.getRuntime()
runtime.exec("chmod 777 " + batFile!!.absolutePath)
runtime.exec("chmod 777 " + batFile.absolutePath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FileDescriptorLeakController : AbstractController() {
} catch (e: Exception) {
log.error("Exception occurs: ", e)
mav.addObject("errmsg",
msg?.getMessage("msg.unknown.exception.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.unknown.exception.occur", arrayOf(e.message), null, locale))
}

return mav
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IntegerOverflowController : AbstractController() {
var multipleNumber: Long = 1
if (times >= 0) {
for (i in 0 until times) {
multipleNumber = multipleNumber * 2
multipleNumber *= 2
}
thickness = BigDecimal(multipleNumber).divide(BigDecimal(10)) // mm
thicknessM = thickness!!.divide(BigDecimal(1000)) // m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView
import org.t246osslab.easybuggy4kt.controller.AbstractController
import java.lang.management.ManagementFactory
import java.lang.management.MemoryPoolMXBean
import java.lang.management.MemoryType
import java.util.*

Expand All @@ -19,13 +18,8 @@ class MemoryLeakController : AbstractController() {
setViewAndCommonObjects(mav, locale, "memoryleak")
toDoRemove()

val heapPoolMXBeans = ArrayList<MemoryPoolMXBean>()
val memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans()
for (memoryPoolMXBean in memoryPoolMXBeans) {
if (MemoryType.HEAP == memoryPoolMXBean.type) {
heapPoolMXBeans.add(memoryPoolMXBean)
}
}
val heapPoolMXBeans = memoryPoolMXBeans.filter { MemoryType.HEAP == it.type }
mav.addObject("memoryPoolMXBeans", heapPoolMXBeans)
return mav
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView
import org.t246osslab.easybuggy4kt.controller.AbstractController
import java.lang.management.ManagementFactory
import java.lang.management.MemoryPoolMXBean
import java.lang.management.MemoryType
import java.util.*

Expand All @@ -24,23 +23,18 @@ class MemoryLeakController2 : AbstractController() {
msg?.getMessage("label.permgen.space", null, locale)
else
msg?.getMessage("label.metaspace", null, locale)
mav.addObject("note", msg?.getMessage("msg.permgen.space.leak.occur", arrayOf<String?>(permName), locale))
mav.addObject("note", msg?.getMessage("msg.permgen.space.leak.occur", arrayOf(permName), locale))
try {
toDoRemove()

val nonHeapPoolMXBeans = ArrayList<MemoryPoolMXBean>()
val memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans()
for (memoryPoolMXBean in memoryPoolMXBeans) {
if (MemoryType.NON_HEAP == memoryPoolMXBean.type) {
nonHeapPoolMXBeans.add(memoryPoolMXBean)
}
}
val nonHeapPoolMXBeans = memoryPoolMXBeans.filter { MemoryType.NON_HEAP == it.type }
mav.addObject("memoryPoolMXBeans", nonHeapPoolMXBeans)

} catch (e: Exception) {
log.error("Exception occurs: ", e)
mav.addObject("errmsg",
msg?.getMessage("msg.unknown.exception.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.unknown.exception.occur", arrayOf(e.message), null, locale))

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class NetworkSocketLeakController : AbstractController() {
@RequestMapping(value = "/netsocketleak")
fun process(mav: ModelAndView, req: HttpServletRequest, locale: Locale): ModelAndView {
setViewAndCommonObjects(mav, locale, "netsocketleak")
var connection: HttpURLConnection?
var url: URL?
val connection: HttpURLConnection?
val url: URL?
var pingURL: String? = req.getParameter("pingurl")
try {
if (pingURL == null) {
Expand All @@ -36,7 +36,7 @@ class NetworkSocketLeakController : AbstractController() {
} catch (e: Exception) {
log.error("Exception occurs: ", e)
mav.addObject("errmsg",
msg?.getMessage("msg.unknown.exception.occur", arrayOf<String?>(e.message), null, locale))
msg?.getMessage("msg.unknown.exception.occur", arrayOf(e.message), null, locale))
}

return mav
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RoundOffErrorController : AbstractController() {
locale: Locale): ModelAndView {
setViewAndCommonObjects(mav, locale, "roundofferror")
val number = NumberUtils.toDouble(strNumber, -1.0)
if (1 <= number && number <= 9) {
if (number in 1.0..9.0) {
mav.addObject("number", strNumber)
mav.addObject("result", number - 0.9)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class CodeInjectionController : AbstractController() {
mav.addObject("msg", msg?.getMessage("msg.valid.json", null, locale))
} catch (e: ScriptException) {
mav.addObject("errmsg", msg?.getMessage("msg.invalid.json",
arrayOf<String?>(e.message), null, locale))
arrayOf(e.message), null, locale))
} catch (e: Exception) {
log.error("Exception occurs: ", e)
mav.addObject("errmsg", msg?.getMessage("msg.invalid.json",
arrayOf<String?>(e.message), null, locale))
arrayOf(e.message), null, locale))
}

}
Expand Down
Loading

0 comments on commit a566b05

Please sign in to comment.