Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/shafirov/kara
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuskunze committed Dec 22, 2015
2 parents 9ae4bcf + 471e617 commit b975865
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 37 deletions.
1 change: 1 addition & 0 deletions .idea/libraries/KotlinJavaRuntime.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions samples/KaraDemo/src/kara/demo/routes/Home.kt
Expand Up @@ -15,7 +15,7 @@ object Home {

@Get("/async")
class Async() : Request({
async {
async() {
TextResult("This've been rendered async.")
}
})
Expand Down Expand Up @@ -43,7 +43,7 @@ object Home {

@Get("/json")
class JsonPage : Request({
json {
jsonResult {
jsonObject {
jsonValue("version", 5)
jsonObject("people")
Expand Down
60 changes: 48 additions & 12 deletions src/KaraLib/src/kara/ActionContext.kt
Expand Up @@ -11,6 +11,8 @@ import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpSession
import kotlin.html.Link
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


Expand All @@ -30,6 +32,8 @@ class ActionContext(val appContext: ApplicationContext,
public val session = if (allowHttpSession) HttpActionSession({request.getSession(true)!!}) else NullSession

public val data: HashMap<Any, Any?> = HashMap()
private val sessionCache = HashMap<String, Any?>()

public val startedAt : Long = System.currentTimeMillis()

fun redirect(link: Link): ActionResult {
Expand All @@ -52,15 +56,24 @@ class ActionContext(val appContext: ApplicationContext,

fun toSession(key: String, value: Any?) {
if (value !is Serializable?) error("Non serializable value to session: key=$key, value=$value")
session.setAttribute(key, (value as? Serializable)?.toBytes())
sessionCache[key] = value
}

fun fromSession(key: String): Any? {
val raw = session.getAttribute(key)
return when (raw) {
is ByteArray -> raw.readObject()
else -> raw
return sessionCache.getOrPut(key) {
val raw = session.getAttribute(key)
when (raw) {
is ByteArray -> raw.readObject()
else -> raw
}
}
}

fun flushSessionCache() {
sessionCache.forEach { key, value ->
session.setAttribute(key, (value as? Serializable)?.toBytes())
}
sessionCache.clear()
}

fun sessionToken(): String {
Expand Down Expand Up @@ -101,22 +114,44 @@ class ActionContext(val appContext: ApplicationContext,
}
}

public class RequestScope<T>() {
public class RequestScope<T:Any>(): ReadWriteProperty<Nothing?, T?> {
@Suppress("UNCHECKED_CAST")
operator fun getValue(o : Any?, desc: KProperty<*>): T {
override fun getValue(thisRef: Nothing?, property: KProperty<*>): T? {
val data = ActionContext.current().data
return data.get(desc) as T
return data.get(property) as T?
}

operator fun setValue(o : Any?, desc: KProperty<*>, value: T) {
ActionContext.current().data.put(desc, value)
override fun setValue(thisRef: Nothing?, property: KProperty<*>, value: T?) {
ActionContext.current().data.put(property, value)
}
}

public class LazyRequestScope<T:Any>(val initial: () -> T): ReadOnlyProperty<Nothing?, T> {
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Nothing?, property: KProperty<*>): T = ActionContext.current().data.getOrPut(property, { initial() }) as T
}

public class LazyRequestScope<T:Any>(val initial: () -> T) {
public class SessionScope<T:Any>(): ReadWriteProperty<Nothing?, T?> {
@Suppress("UNCHECKED_CAST")
operator fun getValue(o: Any?, desc: KProperty<*>): T = ActionContext.current().data.getOrPut(desc, { initial() }) as T
override fun getValue(thisRef: Nothing?, property: KProperty<*>): T? {
return ActionContext.current().fromSession(property.name) as T?
}

override fun setValue(thisRef: Nothing?, property: KProperty<*>, value: T?) {
ActionContext.current().toSession(property.name, value)
}
}

public class LazySessionScope<T:Any>(val initial: () -> T): ReadOnlyProperty<Nothing?, T> {
private val store = SessionScope<T>()

override fun getValue(thisRef: Nothing?, property: KProperty<*>): T {
return store.getValue(thisRef, property) ?: run {
val i = initial()
store.setValue(thisRef, property, i)
i
}
}
}

public class ContextException(msg : String) : Exception(msg) {}
Expand All @@ -128,6 +163,7 @@ public fun <T> ActionContext.withContext(body: () -> T): T {
}
finally {
ActionContext.contexts.set(null)
flushSessionCache()
}
}

3 changes: 1 addition & 2 deletions src/KaraLib/src/kara/ApplicationConfig.kt
@@ -1,7 +1,6 @@
package kara

import kara.config.Config
import kara.internal.logger
import java.io.File
import java.net.URL
import java.util.*
Expand Down Expand Up @@ -79,7 +78,7 @@ public open class ApplicationConfig(val appClassloader: ClassLoader) : Config()
}

it.endsWith("/*") -> {
File(it.removeSuffix("/*")).listFiles { it.isFile && it.name.endsWith(".jar") }?.toList() ?: listOf()
File(it.removeSuffix("/*")).listFiles{ file -> file.isFile && file.name.endsWith(".jar") }?.toList() ?: listOf()
}

else -> {
Expand Down
23 changes: 11 additions & 12 deletions src/KaraLib/src/kara/Json.kt
Expand Up @@ -166,20 +166,19 @@ inline fun JsonRoot.jsonObject(body: JsonObject.() -> Unit){
set(obj)
}

inline fun jsonArray(body: JsonArray.() -> Unit) : JsonElement {
val array = JsonArray()
array.body()
return array
inline fun jsonResult(body: JsonRoot.() -> Unit): JsonResult {
return JsonResult(jsonNode(body))
}

inline fun jsonObject(body: JsonObject.() -> Unit) : JsonElement {
val obj = JsonObject()
obj.body()
return obj
inline fun jsonString(body: JsonRoot.() -> Unit): String {
return StringBuilder().apply { jsonNode(body).build(this) }.toString()
}

inline fun json(body: JsonRoot.() -> Unit) : JsonResult {
val json = JsonRoot()
json.body()
return JsonResult(json)
inline fun jsonNode(body: JsonRoot.() -> Unit): JsonElement {
return JsonRoot().apply {
body()
}
}

@Deprecated(replaceWith = ReplaceWith("jsonResult(body)"), message = "use jsonResult instead", level = DeprecationLevel.WARNING)
inline fun json(body: JsonRoot.() -> Unit) = jsonResult(body)
Expand Up @@ -23,11 +23,13 @@ object Foo2 {

class ObjectInstances() {

@Suppress("DEPRECATION")
@Test fun testDeprecatedObjectInstanceViaReflection() {
assertNotNull(Foo2::class.java.objectInstance0())
assertNotNull(Foo2.Test::class.java.objectInstance0())
}

@Suppress("DEPRECATION")
@Test fun testObjectInstance() {
assertNotNull(Foo2::class.objectInstance)
assertNotNull(Foo2.Test::class.objectInstance)
Expand Down
18 changes: 10 additions & 8 deletions src/Kootstrap/src/kotlin/html/bootstrap/Modal.kt
@@ -1,17 +1,19 @@
package kotlin.html.bootstrap

import kara.*
import javax.servlet.http.*
import kara.ActionContext
import kara.ActionResult
import kara.link
import javax.servlet.http.HttpServletResponse
import kotlin.html.*

class ModalBuilder() {
var button: (A.() -> Unit)? = null
var h: highlight = highlight.default
var c: caliber = caliber.default

var form : (FORM.() -> Unit)? = null
fun form(content: FORM.() -> Unit) {
form = content
var formContent: (FORM.() -> Unit)? = null
fun formContent(content: FORM.() -> Unit) {
formContent = content
}

fun button(h: highlight = highlight.default, size: caliber = caliber.default, b: A.() -> Unit) {
Expand Down Expand Up @@ -90,7 +92,7 @@ fun HtmlBodyTag.modalDialogForm(id: String, action: Link, formMethod: FormMethod
modalDialog(id, content) {
form{
addClass(form_horizontal)
it.form ?. let {it()}
it.formContent?.let {it()}
this.action = action
this.method = formMethod
modalBody(it)
Expand Down Expand Up @@ -129,7 +131,7 @@ fun HtmlBodyTag.modalForm(action: Link, formMethod: FormMethod = FormMethod.post
modalFrame(content) {
form {
addClass(form_horizontal)
it.form ?. let {it()}
it.formContent?. let {it()}
this.action = action
this.method = formMethod
modalBody(it)
Expand Down Expand Up @@ -225,7 +227,7 @@ fun dialogForm(action: Link, formMethod: FormMethod = FormMethod.post, enctype:
addClass("modal-content")
form {
addClass(form_horizontal)
builder.form ?. let {it()}
builder.formContent?. let {it()}

this.action = action
this.method = formMethod
Expand Down
2 changes: 1 addition & 1 deletion src/Kootstrap/src/kotlin/html/bootstrap/Pages.kt
Expand Up @@ -45,7 +45,7 @@ public fun HtmlBodyTag.tabs(activeName: String, body: PagesBuilder.() -> Unit) {
content {
for (item in builder.items) {
pane(item.id, activeName == item.id) {
item.content()
item.content(this)
}
}
}
Expand Down

0 comments on commit b975865

Please sign in to comment.