From 4300b89db53c2050ef32504598a377d6546387e7 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Wed, 16 Mar 2022 23:26:13 +0800 Subject: [PATCH] switch to envelope + jul --- build.gradle | 35 +++++++++++-------- gradle.properties | 8 +++-- gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle | 9 ++++- src/main/java/module-info.java | 12 +++++++ .../woggioni/{gcs => gbcs}/Authenticator.kt | 2 +- .../net/woggioni/{gcs => gbcs}/Authorizer.kt | 2 +- .../{gcs => gbcs}/GradleBuildCacheServer.kt | 16 ++++----- .../net/woggioni/{gcs => gbcs}/Logging.kt | 29 ++++++++++++++- src/main/resources/logging.properties | 8 +++++ 10 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 src/main/java/module-info.java rename src/main/kotlin/net/woggioni/{gcs => gbcs}/Authenticator.kt (98%) rename src/main/kotlin/net/woggioni/{gcs => gbcs}/Authorizer.kt (84%) rename src/main/kotlin/net/woggioni/{gcs => gbcs}/GradleBuildCacheServer.kt (97%) rename src/main/kotlin/net/woggioni/{gcs => gbcs}/Logging.kt (80%) create mode 100644 src/main/resources/logging.properties diff --git a/build.gradle b/build.gradle index bfbecf2..0dad52e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,14 @@ plugins { id 'org.jetbrains.kotlin.jvm' - id 'application' + id 'net.woggioni.gradle.envelope' id 'maven-publish' } +import net.woggioni.gradle.envelope.EnvelopeJarTask + group = 'net.woggioni' -version = '0.1' +version = getProperty('gbcs.version') repositories { mavenCentral() @@ -16,7 +18,7 @@ dependencies { implementation group: 'org.slf4j', name: 'slf4j-api', version: getProperty('slf4j.version') implementation group: 'io.netty', name: 'netty-codec-http', version: getProperty('netty.version') - runtimeOnly group: 'org.slf4j', name: 'slf4j-simple', version: getProperty('slf4j.version') + runtimeOnly group: 'org.slf4j', name: 'slf4j-jdk14', version: getProperty('slf4j.version') testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: getProperty('junit.jupiter.version') testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: getProperty('junit.jupiter.version') @@ -24,22 +26,20 @@ dependencies { } java { - withJavadocJar() withSourcesJar() } -if(JavaVersion.current() > JavaVersion.VERSION_1_8) { - tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME) { - options.release = 8 - } +tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) { + modularity.inferModulePath = true + options.compilerArgs << '--patch-module' << 'net.woggioni.gbcs=' + project.sourceSets.main.output.asPath + options.release = 11 } -run { - systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', 'trace' -} - -application { - mainClassName = 'net.woggioni.gcs.GradleBuildCacheServer' +Provider envelopeJarTaskProvider = tasks.named('envelopeJar', EnvelopeJarTask.class) { + mainModule = 'net.woggioni.gbcs' + mainClass = 'net.woggioni.gbcs.GradleBuildCacheServer' + systemProperty 'java.util.logging.config.class', 'net.woggioni.gbcs.LoggingConfig' + systemProperty 'log.config.source', 'logging.properties' } wrapper { @@ -47,6 +47,11 @@ wrapper { gradleVersion = getProperty('gradle.version') } +def envelopeJarArtifact = artifacts.add('archives', envelopeJarTaskProvider.get().archiveFile.get().asFile) { + type = 'jar' + builtBy envelopeJarTaskProvider +} + publishing { repositories { maven { @@ -55,7 +60,7 @@ publishing { } publications { maven(MavenPublication) { - from(components["java"]) + artifact envelopeJarArtifact } } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 7ad565a..bbcbaa3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,8 +1,10 @@ -gradle.lombok.plugin.version = 0.1 - -gradle.version = 7.4 +gbcs.version = 0.1-SNAPSHOT +gradle.version = 7.4.1 kotlin.version = 1.6.10 +envelope.gradle.plugin.version = 1.0-SNAPSHOT +lombok.gradle.plugin.version = 0.1 + junit.jupiter.version = 5.8.2 netty.version = 4.1.74.Final h2.version = 2.1.210 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 41dfb87..00e33ed 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle index 3ce653f..c2511d2 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,13 +1,20 @@ pluginManagement { repositories { maven { + content { + includeModule 'net.woggioni.gradle', 'envelope' + includeModule 'net.woggioni.gradle.envelope', 'net.woggioni.gradle.envelope.gradle.plugin' + includeModule 'net.woggioni.gradle', 'lombok' + includeModule 'net.woggioni.gradle.lombok', 'net.woggioni.gradle.lombok.gradle.plugin' + } url = 'https://woggioni.net/mvn/' } gradlePluginPortal() } plugins { - id "net.woggioni.gradle.lombok" version "0.1" + id "net.woggioni.gradle.lombok" version getProperty('lombok.gradle.plugin.version') + id "net.woggioni.gradle.envelope" version getProperty('envelope.gradle.plugin.version') id 'org.jetbrains.kotlin.jvm' version getProperty('kotlin.version') } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..b0ffb10 --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,12 @@ +module net.woggioni.gbcs { + requires kotlin.stdlib; + requires io.netty.buffer; + requires io.netty.transport; + requires io.netty.codec.http; + requires io.netty.common; + requires io.netty.handler; + requires io.netty.codec; + requires java.logging; + requires org.slf4j; + exports net.woggioni.gbcs; +} \ No newline at end of file diff --git a/src/main/kotlin/net/woggioni/gcs/Authenticator.kt b/src/main/kotlin/net/woggioni/gbcs/Authenticator.kt similarity index 98% rename from src/main/kotlin/net/woggioni/gcs/Authenticator.kt rename to src/main/kotlin/net/woggioni/gbcs/Authenticator.kt index 4ac39b7..bab3495 100644 --- a/src/main/kotlin/net/woggioni/gcs/Authenticator.kt +++ b/src/main/kotlin/net/woggioni/gbcs/Authenticator.kt @@ -1,4 +1,4 @@ -package net.woggioni.gcs +package net.woggioni.gbcs import io.netty.buffer.Unpooled import io.netty.channel.ChannelFutureListener diff --git a/src/main/kotlin/net/woggioni/gcs/Authorizer.kt b/src/main/kotlin/net/woggioni/gbcs/Authorizer.kt similarity index 84% rename from src/main/kotlin/net/woggioni/gcs/Authorizer.kt rename to src/main/kotlin/net/woggioni/gbcs/Authorizer.kt index 08d6d89..8b873fb 100644 --- a/src/main/kotlin/net/woggioni/gcs/Authorizer.kt +++ b/src/main/kotlin/net/woggioni/gbcs/Authorizer.kt @@ -1,4 +1,4 @@ -package net.woggioni.gcs +package net.woggioni.gbcs import io.netty.handler.codec.http.HttpRequest diff --git a/src/main/kotlin/net/woggioni/gcs/GradleBuildCacheServer.kt b/src/main/kotlin/net/woggioni/gbcs/GradleBuildCacheServer.kt similarity index 97% rename from src/main/kotlin/net/woggioni/gcs/GradleBuildCacheServer.kt rename to src/main/kotlin/net/woggioni/gbcs/GradleBuildCacheServer.kt index b26b08b..a4999a9 100644 --- a/src/main/kotlin/net/woggioni/gcs/GradleBuildCacheServer.kt +++ b/src/main/kotlin/net/woggioni/gbcs/GradleBuildCacheServer.kt @@ -1,4 +1,4 @@ -package net.woggioni.gcs +package net.woggioni.gbcs import io.netty.bootstrap.ServerBootstrap import io.netty.buffer.ByteBuf @@ -9,11 +9,9 @@ import io.netty.channel.ChannelFutureListener import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelInitializer import io.netty.channel.ChannelOption -import io.netty.channel.ChannelOutboundHandlerAdapter import io.netty.channel.ChannelPromise import io.netty.channel.DefaultFileRegion import io.netty.channel.EventLoopGroup -import io.netty.channel.FileRegion import io.netty.channel.SimpleChannelInboundHandler import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel @@ -22,7 +20,6 @@ import io.netty.handler.codec.http.DefaultFullHttpResponse import io.netty.handler.codec.http.DefaultHttpContent import io.netty.handler.codec.http.DefaultHttpResponse import io.netty.handler.codec.http.FullHttpRequest -import io.netty.handler.codec.http.FullHttpResponse import io.netty.handler.codec.http.HttpContentCompressor import io.netty.handler.codec.http.HttpHeaderNames import io.netty.handler.codec.http.HttpHeaderValues @@ -54,19 +51,19 @@ class GradleBuildCacheServer { internal class HttpChunkContentCompressor(threshold : Int, vararg compressionOptions: CompressionOptions = emptyArray()) : HttpContentCompressor(threshold, *compressionOptions) { override fun write(ctx: ChannelHandlerContext, msg: Any, promise: ChannelPromise) { - var msg: Any? = msg - if (msg is ByteBuf) { + var message: Any? = msg + if (message is ByteBuf) { // convert ByteBuf to HttpContent to make it work with compression. This is needed as we use the // ChunkedWriteHandler to send files when compression is enabled. - val buff = msg + val buff = message if (buff.isReadable) { // We only encode non empty buffers, as empty buffers can be used for determining when // the content has been flushed and it confuses the HttpContentCompressor // if we let it go - msg = DefaultHttpContent(buff) + message = DefaultHttpContent(buff) } } - super.write(ctx, msg, promise) + super.write(ctx, message, promise) } } @@ -267,6 +264,7 @@ class GradleBuildCacheServer { private const val HTTP_PORT = 8080 @JvmStatic fun main(args: Array) { + Thread.currentThread().contextClassLoader = GradleBuildCacheServer::class.java.classLoader GradleBuildCacheServer().run() } diff --git a/src/main/kotlin/net/woggioni/gcs/Logging.kt b/src/main/kotlin/net/woggioni/gbcs/Logging.kt similarity index 80% rename from src/main/kotlin/net/woggioni/gcs/Logging.kt rename to src/main/kotlin/net/woggioni/gbcs/Logging.kt index 59ef682..081a0f5 100644 --- a/src/main/kotlin/net/woggioni/gcs/Logging.kt +++ b/src/main/kotlin/net/woggioni/gbcs/Logging.kt @@ -1,9 +1,15 @@ -package net.woggioni.gcs +package net.woggioni.gbcs import io.netty.channel.ChannelHandlerContext import org.slf4j.Logger import org.slf4j.LoggerFactory import java.net.InetSocketAddress +import java.nio.file.Files +import java.nio.file.Path +import java.util.logging.LogManager + + + inline fun T.contextLogger() = LoggerFactory.getLogger(T::class.java) @@ -105,3 +111,24 @@ inline fun log(log : Logger, ctx : ChannelHandlerContext, log.loggerMethod(clientAddress + " - " + messageBuilder()) } } + + +class LoggingConfig { + + init { + val logManager = LogManager.getLogManager() + System.getProperty("log.config.source")?.let withSource@ { source -> + val urls = LoggingConfig::class.java.classLoader.getResources(source) + while(urls.hasMoreElements()) { + val url = urls.nextElement() + url.openStream().use { inputStream -> + logManager.readConfiguration(inputStream) + return@withSource + } + } + Path.of(source).takeIf(Files::exists) + ?.let(Files::newInputStream) + ?.use(logManager::readConfiguration) + } + } +} \ No newline at end of file diff --git a/src/main/resources/logging.properties b/src/main/resources/logging.properties new file mode 100644 index 0000000..8a2887d --- /dev/null +++ b/src/main/resources/logging.properties @@ -0,0 +1,8 @@ +.level=FINEST + +handlers = java.util.logging.ConsoleHandler + +java.util.logging.ConsoleHandler.level = FINEST +java.util.logging.ConsoleHandler.filter = +java.util.logging.ConsoleHandler.formatter = +java.util.logging.ConsoleHandler.encoding = \ No newline at end of file