EN · DE · RU · FR · ES

#333: ConsoleTimeoutReader.kt

projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt Clase Kotlin, projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt 123 líneas · 77 código · 36 comentarios · 10 en blanco
Propósito: Archivo fuente: org/projectforge/start/ConsoleTimeoutReader.kt. ConsoleTimeoutReader.kt es parte de la aplicación de gestión de proyectos de código abierto ProjectForge.

Fuente (primeras 100 líneas)

/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
//         www.projectforge.org
//
// Copyright (C) 2001-2026 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge tiene doble licencia.
//
// Esta edición comunitaria es software libre; puedes redistribuirla y/o
// modificarla bajo los términos de la Licencia Pública General GNU publicada
// por la Free Software Foundation; versión 3 de la Licencia.
//
// Esta edición comunitaria se distribuye con la esperanza de que sea útil,
// pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
// COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la Licencia
// Pública General GNU para más detalles.
//
// Deberías haber recibido una copia de la Licencia Pública General GNU junto
// con este programa; si no, visita http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////

package org.projectforge.start

import mu.KotlinLogging
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.time.Duration
import java.util.concurrent.ExecutionException
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlin.system.exitProcess

private val log = KotlinLogging.logger {}

/**
 * Para leer una entrada de consola del sistema (con tiempo de espera).
 */
open class ConsoleTimeoutReader
/**
 * @param pregunta
 * @param respuestaPorDefecto La respuesta si el usuario simplemente presiona Enter.
 */
@JvmOverloads
constructor(
  private val question: String,
  private val defaultAnswer: String? = null,
  var timeOutSeconds: Int = CONSOLE_INPUT_TIMEOUT
) {
  private val streamReader = InputStreamReader(System.`in`)
  private val bufferedReader = BufferedReader(streamReader)

  fun ask(): String? {
    log.info("ProjectForge espera $timeOutSeconds segundos para tu entrada en la consola (si se ejecuta sin consola, ProjectForge continuará de todos modos): $question")
    println()
    println(StringUtils.center(" PREGUNTA ", 120, "?"))
    println()
    var answer: String?
    do {
      answer = readConsoleAnswerWithTimeout()
      if (answer == null) return null
      val result = if (defaultAnswer != null && answer.isEmpty()) {
        defaultAnswer
      } else {
        answer.trim { it <= ' ' }.lowercase()
      }
      if (answerValid(result)) return result
    } while (true)
  }

  /**
   * Si el usuario simplemente presiona Enter, el parámetro answer será la respuesta por defecto configurada o una cadena vacía,
   * si no se define ningún valor por defecto.
   *
   * @param answer Respuesta dada desde la entrada de consola ("" o entrada en minúsculas).
   * @return true si la respuesta comienza con 's' o 'n', de lo contrario false.
   */
  protected open fun answerValid(answer: String): Boolean {
    return answer.startsWith("s") || answer.startsWith("n")
  }

  private fun readConsoleAnswerWithTimeout(): String? {
    val timeout = Duration.ofSeconds(timeOutSeconds.toLong())
    val executor = Executors.newSingleThreadExecutor()
    val handler = executor.submit<String> { readConsoleAnswer(question) }
    var answer: String? = null
    try {
      answer = handler[timeout.toMillis(), TimeUnit.MILLISECONDS]
    } catch (ex: TimeoutException) {
      log.info("Tiempo de espera de entrada de consola excedido (>" + timeOutSeconds + "s). Abortando.")
      handler.cancel(true)
    } catch (ex: InterruptedException) {
      log.info("Tiempo de espera de entrada de consola excedido (>" + timeOutSeconds + "s). Abortando.")
      handler.cancel(true)
    } catch (ex: ExecutionException) {

Historial Git

868d6abb7 2025 -> 2026
63081666f Encabezados de archivos fuente: 2024 -> 2025.
b6092df09 Copyright 2023 -> 2024
ab45d51fa Copyright 2001-2022 -> 2001-2023.
4def09990 Funciones Kotlin obsoletas: toLowerCase -> lowerCase, toUpperCase -> uppercase.

868d6abb7

2025 -> 2026
868d6abb75cd191a892911ac8e45058932cf9074
diff --git a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
index 6464bf280..95e1afbea 100644
--- a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
+++ b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
@@ -3,7 +3,7 @@
 // Project ProjectForge Community Edition
 //         www.projectforge.org
 //
-// Copyright (C) 2001-2025 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2026 Micromata GmbH, Germany (www.micromata.com)
 //
 // ProjectForge is dual-licensed.
 //

63081666f

Encabezados de archivos fuente: 2024 -> 2025.
63081666f620fb87315f01b817e560e0b2f6a33a
diff --git a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
index e2d880025..6464bf280 100644
--- a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
+++ b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
@@ -3,7 +3,7 @@
 // Project ProjectForge Community Edition
 //         www.projectforge.org
 //
-// Copyright (C) 2001-2024 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2025 Micromata GmbH, Germany (www.micromata.com)
 //
 // ProjectForge is dual-licensed.
 //

b6092df09

Copyright 2023 -> 2024
b6092df0927c4a3b161e888445f31dcab57493f2
diff --git a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
index ea873a836..e2d880025 100644
--- a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
+++ b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
@@ -3,7 +3,7 @@
 // Project ProjectForge Community Edition
 //         www.projectforge.org
 //
-// Copyright (C) 2001-2023 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2024 Micromata GmbH, Germany (www.micromata.com)
 //
 // ProjectForge is dual-licensed.
 //

ab45d51fa

Copyright 2001-2022 -> 2001-2023.
ab45d51fa419ede6174b31d69987f96d4b841ff9
diff --git a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
index 133bc81d1..ea873a836 100644
--- a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
+++ b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
@@ -3,7 +3,7 @@
 // Project ProjectForge Community Edition
 //         www.projectforge.org
 //
-// Copyright (C) 2001-2022 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2023 Micromata GmbH, Germany (www.micromata.com)
 //
 // ProjectForge is dual-licensed.
 //

4def09990

Funciones Kotlin obsoletas: toLowerCase -> lowerCase, toUpperCase -> uppercase.
4def099906252d291059bdc513a2273ebd704228
diff --git a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
index 55fb4a075..133bc81d1 100644
--- a/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
+++ b/projectforge-application/src/main/kotlin/org/projectforge/start/ConsoleTimeoutReader.kt
@@ -67,7 +67,7 @@ constructor(
       val result = if (defaultAnswer != null && answer.isEmpty()) {
         defaultAnswer
       } else {
-        answer.trim { it <= ' ' }.toLowerCase()
+        answer.trim { it <= ' ' }.lowercase()
       }
       if (answerValid(result)) return result
     } while (true)