<commit_before>package io.ktor.server.servlet

import com.typesafe.config.*
import io.ktor.application.*
import io.ktor.config.*
import io.ktor.server.engine.*
import org.slf4j.*
import javax.servlet.annotation.*

/**
 * This servlet need to be installed into a servlet container
 */
@MultipartConfig
open class ServletApplicationEngine : KtorServlet() {
    private val environment: ApplicationEngineEnvironment by lazy {
        val servletContext = servletContext

        servletContext.getAttribute(ApplicationEngineEnvironmentAttributeKey)?.let { return@lazy it as ApplicationEngineEnvironment }

        val parameterNames = servletContext.initParameterNames.toList().filter { it.startsWith("io.ktor") }
        val parameters = parameterNames.associateBy({ it.removePrefix("io.ktor.") }, { servletContext.getInitParameter(it) })

        val hocon = ConfigFactory.parseMap(parameters)
        val configPath = "ktor.config"
        val applicationIdPath = "ktor.application.id"

        val combinedConfig = if (hocon.hasPath(configPath)) {
            val configStream = servletContext.classLoader.getResourceAsStream(hocon.getString(configPath))
            val loadedKtorConfig = ConfigFactory.parseReader(configStream.bufferedReader())
            hocon.withFallback(loadedKtorConfig)
        } else
            hocon.withFallback(ConfigFactory.load())

        val applicationId = combinedConfig.tryGetString(applicationIdPath) ?: "Application"

        applicationEngineEnvironment {
            config = HoconApplicationConfig(combinedConfig)
            log = LoggerFactory.getLogger(applicationId)
            classLoader = servletContext.classLoader
        }.apply {
            monitor.subscribe(ApplicationStarting)  {<commit_msg>Make servlet engine use servlet config instead of context (#1063)<commit_after>package io.ktor.server.servlet

import com.typesafe.config.*
import io.ktor.application.*
import io.ktor.config.*
import io.ktor.server.engine.*
import org.slf4j.*
import javax.servlet.*
import javax.servlet.annotation.*

/**
 * This servlet need to be installed into a servlet container
 */
@MultipartConfig
open class ServletApplicationEngine : KtorServlet() {
    private val environment: ApplicationEngineEnvironment by lazy {
        val servletContext = servletContext
        val servletConfig = servletConfig

        servletContext.getAttribute(ApplicationEngineEnvironmentAttributeKey)?.let { return@lazy it as ApplicationEngineEnvironment }

        val parameterNames = (servletContext.initParameterNames?.toList().orEmpty() +
            servletConfig.initParameterNames?.toList().orEmpty()).filter { it.startsWith("io.ktor") }.distinct()
        val parameters = parameterNames.associateBy({ it.removePrefix("io.ktor.") }, {
            servletConfig.getInitParameter(it) ?: servletContext.getInitParameter(it)
        })

        val hocon = ConfigFactory.parseMap(parameters)
        val configPath = "ktor.config"
        val applicationIdPath = "ktor.application.id"

        val combinedConfig = if (hocon.hasPath(configPath)) {
            val configStream = servletContext.classLoader.getResourceAsStream(hocon.getString(configPath))
                ?: throw ServletException("No config ${hocon.getString(configPath)} found for the servlet named $servletName")
            val loadedKtorConfig = configStream.bufferedReader().use { ConfigFactory.parseReader(it) }
            hocon.withFallback(loadedKtorConfig)
        } else
            hocon.withFallback(ConfigFactory.load())

        val applicationId = combinedConfig.tryGetString(applicationIdPath) ?: "Application"

        applicationEngineEnvironment {
            config = HoconApplicationConfig(combinedConfig)
            log = LoggerFactory.getLogger(applicationId)
            classLoader = servletContext.classLoader
        }.apply {
            monitor.subscribe(ApplicationStarting)  {
{'fraction_non_alphanumeric': 0.09114183307731695, 'fraction_numerical': 0.0015360983102918587, 'mean_word_length': 3.564252336448598, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6725067', 'n_tokens_mistral': 996, 'n_tokens_neox': 987, 'n_words': 202}