Skip to content

Commit

Permalink
Merge pull request #623 from hexagonkt/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jaguililla authored Apr 27, 2023
2 parents 601a00f + 5c7f1a2 commit 3ab8b5f
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 97 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.github.jk1.license.render.ReportRenderer
*/

plugins {
kotlin("jvm") version("1.8.20") apply(false)
kotlin("jvm") version("1.8.21") apply(false)

id("idea")
id("eclipse")
Expand Down
49 changes: 2 additions & 47 deletions core/src/main/kotlin/com/hexagonkt/core/Jvm.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.hexagonkt.core

import java.lang.management.ManagementFactory.getMemoryMXBean
import java.lang.management.ManagementFactory.getRuntimeMXBean
import java.lang.management.MemoryUsage
import java.lang.management.RuntimeMXBean
import java.net.InetAddress
import java.nio.charset.Charset
import java.time.ZoneId
Expand All @@ -15,8 +11,8 @@ import kotlin.reflect.KClass
* Object with utilities to gather information about the running JVM.
*/
object Jvm {
/** Default timezone. TODO Defining this lazily fails in macOS */
val timeZone: TimeZone = TimeZone.getDefault()
/** Default timezone. */
val timeZone: TimeZone by lazy { TimeZone.getDefault() }

/** Default zone ID. */
val zoneId: ZoneId by lazy { timeZone.toZoneId() }
Expand All @@ -33,12 +29,6 @@ object Jvm {
/** The IP address of the machine running this program. */
val ip: String by lazy { InetAddress.getLocalHost().hostAddress }

/**
* ID representing the running Java virtual machine. If the 'java.management' module is not
* available returns `N/A`.
*/
val id: String by lazy { runtime?.name ?: "N/A" }

/** Name of the JVM running this program. For example: OpenJDK 64-Bit Server VM. */
val name: String by lazy { System.getProperty("java.vm.name") }

Expand All @@ -56,41 +46,6 @@ object Jvm {
"%s_%s.%s".format(locale.language, locale.country, charset.name())
}

private val heap: MemoryUsage? by lazy {
try { getMemoryMXBean().heapMemoryUsage } catch (_: NoClassDefFoundError) { null }
}

private val runtime: RuntimeMXBean? by lazy {
try { getRuntimeMXBean() } catch (_: NoClassDefFoundError) { null }
}

/**
* Amount of memory in kilobytes that the JVM initially requests from the operating system. If
* the 'java.management' module is not available returns `N/A`.
*
* @return Initial amount of memory in kilobytes.
*/
fun initialMemory(): String =
heap?.let { "%,d".format(it.init / 1024) } ?: "N/A"

/**
* Amount of used memory in kilobytes. If the 'java.management' module is not available returns
* `N/A`.
*
* @return Used memory in kilobytes.
*/
fun usedMemory(): String =
heap?.let { "%,d".format(it.used / 1024) } ?: "N/A"

/**
* Uptime of the Java virtual machine in seconds. If the 'java.management' module is not
* available returns `N/A`.
*
* @return JVM uptime in seconds.
*/
fun uptime(): String =
runtime?.let { "%01.3f".format(it.uptime / 1e3) } ?: "N/A"

/**
* Retrieve a setting by name by looking in the JVM system properties first and in OS
* environment variables if not found.
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/kotlin/com/hexagonkt/core/media/MediaTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hexagonkt.core.media

import com.hexagonkt.core.media.MediaTypeGroup.*
import java.io.File
import java.net.URI
import java.net.URL

val MEDIA_TYPE_FORMAT: Regex = """\*|([\w+.-]+)""".toRegex()
Expand Down Expand Up @@ -196,6 +197,9 @@ fun parseMediaType(fullType: String): MediaType {
return MediaType(group, type)
}

fun mediaTypeOfOrNull(uri: URI): MediaType? =
mediaTypeOfOrNull(pathExtension(uri.path))

fun mediaTypeOfOrNull(url: URL): MediaType? =
mediaTypeOfOrNull(pathExtension(url.file))

Expand All @@ -205,6 +209,9 @@ fun mediaTypeOfOrNull(file: File): MediaType? =
fun mediaTypeOfOrNull(extension: String): MediaType? =
MEDIA_TYPES_EXTENSIONS[extension]

fun mediaTypeOf(uri: URI): MediaType =
mediaTypeOfOrNull(uri) ?: error("Media type not found for: '$uri' URI")

fun mediaTypeOf(url: URL): MediaType =
mediaTypeOfOrNull(url) ?: error("Media type not found for: '$url' URL")

Expand Down
12 changes: 12 additions & 0 deletions core/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

module com.hexagonkt.core {

requires transitive kotlin.stdlib;

exports com.hexagonkt.core;
exports com.hexagonkt.core.logging;
exports com.hexagonkt.core.media;
exports com.hexagonkt.core.security;

provides java.net.spi.URLStreamHandlerProvider with com.hexagonkt.core.ClasspathHandlerProvider;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ internal class HexagonCoreSamplesTest {
"""
}

classLogger.trace { "Message only evaluated if trace enabled at ${Jvm.id}" }
classLogger.debug { "Message only evaluated if debug enabled at ${Jvm.id}" }
classLogger.warn { "Message only evaluated if warn enabled at ${Jvm.id}" }
classLogger.info { "Message only evaluated if info enabled at ${Jvm.id}" }
classLogger.trace { "Message only evaluated if trace enabled" }
classLogger.debug { "Message only evaluated if debug enabled" }
classLogger.warn { "Message only evaluated if warn enabled" }
classLogger.info { "Message only evaluated if info enabled" }

val exception = IllegalStateException("Exception")
classLogger.warn(exception) { "Warning with exception" }
Expand Down
8 changes: 0 additions & 8 deletions core/src/test/kotlin/com/hexagonkt/core/JvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ internal class JvmTest {
assert(Inet4Address.getAllByName(Jvm.ip).isNotEmpty())
}

@Test fun `JVM metrics have valid values` () {
val numberRegex = Regex("[\\d.,]+")
assert(Jvm.initialMemory().matches(numberRegex))
assert(Jvm.usedMemory().matches(numberRegex))
assert(Jvm.uptime().matches(numberRegex))
}

@Test fun `Locale code matches with default locale` () {
assert(Jvm.localeCode.contains(Jvm.locale.country))
assert(Jvm.localeCode.contains(Jvm.locale.language))
Expand Down Expand Up @@ -109,7 +102,6 @@ internal class JvmTest {
}

@Test fun `Default JVM info is fetched correctly`() {
assert(Jvm.id.isNotBlank())
assert(Jvm.cpuCount > 0)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.hexagonkt.core.media
import com.hexagonkt.core.media.MediaTypeGroup.*
import kotlin.test.Test
import java.io.File
import java.net.URI
import kotlin.IllegalArgumentException
import kotlin.IllegalStateException
import java.net.URL
Expand Down Expand Up @@ -41,8 +42,10 @@ internal class MediaTypesTest {
}

@Test fun `Media types of files and URLs can be retrieved`() {
assertEquals(APPLICATION_TOML, mediaTypeOfOrNull(URI("http://localhost/file.toml")))
assertEquals(TEXT_PLAIN, mediaTypeOfOrNull(URL("http://localhost/file.txt")))
assertEquals(APPLICATION_JSON, mediaTypeOfOrNull(URL("http://localhost/file.json")))
assertEquals(APPLICATION_TOML, mediaTypeOf(URI("http://localhost/file.toml")))
assertEquals(TEXT_PLAIN, mediaTypeOf(URL("http://localhost/file.txt")))
assertEquals(APPLICATION_JSON, mediaTypeOf(URL("http://localhost/file.json")))
assertEquals(APPLICATION_AVRO, mediaTypeOf("avro"))
Expand All @@ -59,6 +62,8 @@ internal class MediaTypesTest {
assertEquals(APPLICATION_YAML, mediaTypeOf(File("file.yml")))
assertNull(mediaTypeOfOrNull(File("file")))
assertNull(mediaTypeOfOrNull(File("file.baz")))
assertNull(mediaTypeOfOrNull(URI("http://localhost/file.baz")))
assertNull(mediaTypeOfOrNull(URL("http://localhost/file.baz")))
}

@Test fun `Exception is thrown if the media type is not found`() {
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.warning.mode=all
org.gradle.console=plain

# Gradle
version=2.8.1
version=2.8.2
group=com.hexagonkt
description=The atoms of your platform

Expand All @@ -33,13 +33,13 @@ logoLarge=assets/img/logo.svg
iconsDirectory=content

# VERSIONS
kotlinVersion=1.8.20
kotlinVersion=1.8.21
dokkaVersion=1.8.10
mockkVersion=1.13.5
junitVersion=5.9.2
junitVersion=5.9.3
gatlingVersion=3.9.3
jmhVersion=1.36
mkdocsMaterialVersion=9.1.7
mkdocsMaterialVersion=9.1.8
mermaidDokkaVersion=0.4.4
nativeToolsVersion=0.9.21

Expand Down
4 changes: 2 additions & 2 deletions gradle/kotlin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repositories {

dependencies {
final String scriptMockkVersion = findProperty("mockkVersion") ?: "1.13.4"
final String scriptJunitVersion = findProperty("junitVersion") ?: "5.9.2"
final String scriptJunitVersion = findProperty("junitVersion") ?: "5.9.3"

implementation("org.jetbrains.kotlin:kotlin-stdlib")

Expand Down Expand Up @@ -138,7 +138,7 @@ tasks.withType(Test) { testTask ->
}

jacoco {
toolVersion = "0.8.9"
toolVersion = "0.8.10"
}

tasks.jacocoTestReport {
Expand Down
7 changes: 7 additions & 0 deletions handlers/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

module com.hexagonkt.handlers {

requires transitive kotlin.stdlib;

exports com.hexagonkt.handlers;
}
10 changes: 10 additions & 0 deletions http/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

module com.hexagonkt.http {

requires transitive com.hexagonkt.core;

exports com.hexagonkt.http;
exports com.hexagonkt.http.model;
exports com.hexagonkt.http.model.ws;
exports com.hexagonkt.http.patterns;
}
8 changes: 8 additions & 0 deletions http_client/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

module com.hexagonkt.http_client {

requires transitive kotlin.stdlib;
requires transitive com.hexagonkt.http_handlers;

exports com.hexagonkt.http.client;
}
13 changes: 13 additions & 0 deletions http_client_jetty/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

module com.hexagonkt.http_client_jetty {

requires transitive com.hexagonkt.http;
requires transitive com.hexagonkt.http_client;
requires transitive org.eclipse.jetty.io;
requires transitive org.eclipse.jetty.util;
requires transitive org.eclipse.jetty.client;
requires transitive org.eclipse.jetty.http2.client;
requires transitive org.eclipse.jetty.http2.http.client.transport;

exports com.hexagonkt.http.client.jetty;
}
15 changes: 15 additions & 0 deletions http_client_jetty_ws/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

module com.hexagonkt.http_client_jetty_ws {

requires transitive kotlin.stdlib;
requires transitive com.hexagonkt.http_client_jetty;
requires transitive org.eclipse.jetty.io;
requires transitive org.eclipse.jetty.util;
requires transitive org.eclipse.jetty.client;
requires transitive org.eclipse.jetty.http2.client;
requires transitive org.eclipse.jetty.http2.http.client.transport;
requires transitive org.eclipse.jetty.websocket.jetty.api;
requires transitive org.eclipse.jetty.websocket.jetty.client;

exports com.hexagonkt.http.client.jetty;
}
8 changes: 8 additions & 0 deletions http_handlers/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

module com.hexagonkt.http_handlers {

requires transitive com.hexagonkt.http;
requires transitive com.hexagonkt.handlers;

exports com.hexagonkt.http.handlers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import com.hexagonkt.core.Ansi.DEFAULT
import com.hexagonkt.core.Ansi.MAGENTA
import com.hexagonkt.core.Ansi.RESET
import com.hexagonkt.core.Ansi.UNDERLINE
import com.hexagonkt.core.Jvm.initialMemory
import com.hexagonkt.core.Jvm.uptime
import com.hexagonkt.core.Jvm.usedMemory
import com.hexagonkt.core.prependIndent
import com.hexagonkt.http.server.HttpServerFeature.ZIP
import com.hexagonkt.http.handlers.HttpHandler
Expand Down Expand Up @@ -176,27 +173,7 @@ data class HttpServer(
val startUpTimeValue = "$BOLD$MAGENTA$startUpTime ms$RESET"
val bindingValue = "$BLUE$UNDERLINE$binding$RESET"

val information = if (settings.vmInformation) {
val jvmMemoryValue = "$BLUE${initialMemory()}$RESET"
val bootTimeValue = "$BOLD$MAGENTA${uptime()} s$RESET"
val usedMemoryValue = "$BOLD$MAGENTA${usedMemory()} KB$RESET"

"""
Server Adapter: $serverAdapterValue ($protocols)
Supported Features: $features
Configuration Options: $options
🖥️️ Running in '$hostnameValue' with $cpuCountValue CPUs $jvmMemoryValue KB
🛠 Using $javaVersionValue
🌍 Locale: $localeValue Timezone: $timezoneValue Charset: $charsetValue
⏱️ Started in $bootTimeValue (server: $startUpTimeValue) using $usedMemoryValue
🚀 Served at $bindingValue${if (protocol == HTTP2) " (HTTP/2)" else "" }
"""
}
else {
val information =
"""
Server Adapter: $serverAdapterValue ($protocols)
Expand All @@ -211,7 +188,6 @@ data class HttpServer(
🚀 Served at $bindingValue${if (protocol == HTTP2) " (HTTP/2)" else "" }
"""
}

val banner = (settings.banner?.let { "$it\n" } ?: banner) + information.trimIndent()
return banner.prependIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import java.net.InetAddress
* @property sslSettings SSL settings info for configuring the server.
* @property banner Server banner message.
* @property zip Option to compress server responses.
* @property vmInformation If true, show JVM information on start banner. If enabled, it forces
* the `java.management` module to be included.
*/
data class HttpServerSettings(
val bindAddress: InetAddress = InetAddress.getLoopbackAddress(),
Expand All @@ -26,5 +24,4 @@ data class HttpServerSettings(
val sslSettings: SslSettings? = null,
val banner: String? = null,
val zip: Boolean = false,
val vmInformation: Boolean = false,
)
9 changes: 9 additions & 0 deletions http_server/src/main/kotlin/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

module com.hexagonkt.http_server {

requires transitive com.hexagonkt.core;
requires transitive com.hexagonkt.http_handlers;

exports com.hexagonkt.http.server;
exports com.hexagonkt.http.server.callbacks;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ internal class HttpServerTest {

val banners = listOf(
serve(VoidAdapter, serverSettings) {},
serve(VoidAdapter, serverSettings.copy(vmInformation = true)) {}
)
.map {
it.createBanner(System.currentTimeMillis())
Expand All @@ -63,7 +62,6 @@ internal class HttpServerTest {
it
}
assertContains(banners.first(), "(excluding VM)")
assertFalse(banners.last().contains("(excluding VM)"))
}

@Test fun `Banner creation with enabled features and custom options`() {
Expand Down
Loading

0 comments on commit 3ab8b5f

Please sign in to comment.