<commit_before>package com.github.kotlinee.framework

import com.github.kotlinee.framework.vaadin.UrlParamShortener
import com.vaadin.server.VaadinService
import com.vaadin.server.VaadinSession
import com.vaadin.ui.UI
import org.slf4j.LoggerFactory
import java.io.Serializable
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger
import javax.persistence.EntityManagerFactory
import javax.persistence.Persistence
import javax.servlet.http.Cookie
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

object Kotlinee {
    /**
     * Initializes the KotlinEE framework. Just call this from your context listener.
     */
    fun kotlineeInit() = synchronized(this) {
        // TomEE also has by default 5 threads, so I guess this is okay :-D
        executor = Executors.newScheduledThreadPool(5, threadFactory)
    }

    /**
     * Destroys the KotlinEE framework. Just call this from your context listener.
     */
    fun kotlineeDestroy() = synchronized(this) {
        if (isStarted) {
            executor!!.shutdown()
            executor!!.awaitTermination(1, TimeUnit.DAYS)
            executor = null
        }
    }

    /**
     * True if [kotlineeInit] has been called.
     */
    val isStarted: Boolean
    get() = synchronized(this) { executor != null }

    private var executor: ScheduledExecutorService? = null

    private fun checkStarted() {
        if (!isStarted) throw IllegalStateException("kotlineeInit() has not been called, or Kotlinee is already destroyed")
    }

    /**
     * The executor used by [async] and [scheduleAtFixedRate]. You can submit your own tasks as you wish.
     */
    val asyncExecutor: ScheduledExecutorService
        get() = synchronized(this) { checkStarted(); executor!! }

    /**
     * The thread factory used by the [async] method. By default the factory
     * creates non-daemon threads named "async-ID".
     *
     * Needs to be set before [kotlineeInit] is called.
     */
    @Volatile
    var threadFactory: ThreadFactory = object : ThreadFactory {
        private val id = AtomicInteger()
        override fun newThread(r: Runnable): Thread? {
            val thread = Thread(r)
            thread.name = "async-${id.incrementAndGet()}"
            return thread
        }
    }

    internal val log = LoggerFactory.getLogger(javaClass)

    /**
     * Used for data persistence - the JDBC/EntityManager/JPA thingy. By default uses the "sample" persistence unit
     * present in `META-INF/persistence.xml` but you can of course use any factory you wish.
     */
    @Volatile
    var entityManagerFactory: EntityManagerFactory = Persistence.createEntityManagerFactory("sample")<commit_msg>EMF is now closed properly<commit_after>package com.github.kotlinee.framework

import com.github.kotlinee.framework.vaadin.UrlParamShortener
import com.vaadin.server.VaadinService
import com.vaadin.server.VaadinSession
import com.vaadin.ui.UI
import org.slf4j.LoggerFactory
import java.io.Serializable
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger
import javax.persistence.EntityManagerFactory
import javax.persistence.Persistence
import javax.servlet.http.Cookie
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

object Kotlinee {
    /**
     * Initializes the KotlinEE framework. Just call this from your context listener.
     */
    fun kotlineeInit() = synchronized(this) {
        // TomEE also has by default 5 threads, so I guess this is okay :-D
        executor = Executors.newScheduledThreadPool(5, threadFactory)
    }

    /**
     * Destroys the KotlinEE framework. Just call this from your context listener.
     */
    fun kotlineeDestroy() = synchronized(this) {
        if (isStarted) {
            executor!!.shutdown()
            executor!!.awaitTermination(1, TimeUnit.DAYS)
            executor = null
            entityManagerFactory.close()
        }
    }

    /**
     * True if [kotlineeInit] has been called.
     */
    val isStarted: Boolean
    get() = synchronized(this) { executor != null }

    private var executor: ScheduledExecutorService? = null

    private fun checkStarted() {
        if (!isStarted) throw IllegalStateException("kotlineeInit() has not been called, or Kotlinee is already destroyed")
    }

    /**
     * The executor used by [async] and [scheduleAtFixedRate]. You can submit your own tasks as you wish.
     */
    val asyncExecutor: ScheduledExecutorService
        get() = synchronized(this) { checkStarted(); executor!! }

    /**
     * The thread factory used by the [async] method. By default the factory
     * creates non-daemon threads named "async-ID".
     *
     * Needs to be set before [kotlineeInit] is called.
     */
    @Volatile
    var threadFactory: ThreadFactory = object : ThreadFactory {
        private val id = AtomicInteger()
        override fun newThread(r: Runnable): Thread? {
            val thread = Thread(r)
            thread.name = "async-${id.incrementAndGet()}"
            return thread
        }
    }

    internal val log = LoggerFactory.getLogger(javaClass)

    /**
     * Used for data persistence - the JDBC/EntityManager/JPA thingy. By default uses the "sample" persistence unit
     * present in `META-INF/persistence.xml` but you can of course use any factory you wish.
     */
    @Volatile
    var entityManagerFactory: EntityManagerFactory = Persistence.createEntityManagerFactory("sample")
    set(value) { field.close(); field = value }
{'fraction_non_alphanumeric': 0.08999641448547867, 'fraction_numerical': 0.0014342058085335247, 'mean_word_length': 2.9764789736279402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4156124', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1540, 'n_words': 456}