From ba7408456da8a6d1baea29831329d7dd27ddd3d7 Mon Sep 17 00:00:00 2001 From: Guilherme I F L Weizenmann Date: Thu, 28 Sep 2023 15:00:52 -0300 Subject: [PATCH] feat(core,cmds): fixes and outputs/loggers --- .../com/itquasar/multiverse/jmacro/cli/Launcher.java | 7 +++++-- .../java/com/itquasar/multiverse/jmacro/cli/Run.java | 2 +- .../base/commands/ConfigurationCommand.groovy | 7 +++++-- .../base/providers/LoggerCommandProvider.java | 7 +++++-- .../jmacro/core/command/AbstractCommand.groovy | 12 ++++++------ .../jmacro/core/engine/ScriptEngineAware.java | 10 ++++------ 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Launcher.java b/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Launcher.java index e66d6e0..f4a64fb 100644 --- a/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Launcher.java +++ b/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Launcher.java @@ -4,23 +4,26 @@ import jnr.posix.SignalHandler; import jnr.posix.util.SunMiscSignal; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import picocli.CommandLine; import java.util.Arrays; public class Launcher { + private static final Logger LOGGER = LogManager.getLogger(Launcher.class); + static { SignalHandler handler = signalCode -> { Signal signal = Signal.valueOf(signalCode); - System.out.println("Received signal " + signal.name() + " (" + signal.intValue() + "|" + signalCode + ")"); + LOGGER.warn("Received signal " + signal.name() + " (" + signal.intValue() + "|" + signalCode + ")"); System.exit(128 + signalCode); }; for (Signal signal : new Signal[]{Signal.SIGINT, Signal.SIGTERM, Signal.SIGQUIT, Signal.SIGPIPE, Signal.SIGABRT, Signal.SIGIOT}) { try { SunMiscSignal.signal(signal, handler); } catch (IllegalArgumentException ex) { - System.out.println("Error registering signal handler for " + signal.name()); + LOGGER.warn("Error registering signal handler for " + signal.name()); } } } diff --git a/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Run.java b/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Run.java index 6df5cbe..607d8db 100644 --- a/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Run.java +++ b/jmacro-cli/src/main/java/com/itquasar/multiverse/jmacro/cli/Run.java @@ -194,7 +194,7 @@ public Optional checkInfixAndExtension(String path) { LOGGER.info("Path with infix and default extension " + path); } } - Cli.out.print("Returning path as " + path); + LOGGER.info("Returning path as " + path); return Optional.of(path); } } diff --git a/jmacro-commands/jmacro-commands-base/src/main/groovy/com/itquasar/multiverse/jmacro/commands/base/commands/ConfigurationCommand.groovy b/jmacro-commands/jmacro-commands-base/src/main/groovy/com/itquasar/multiverse/jmacro/commands/base/commands/ConfigurationCommand.groovy index f586f07..93896bf 100644 --- a/jmacro-commands/jmacro-commands-base/src/main/groovy/com/itquasar/multiverse/jmacro/commands/base/commands/ConfigurationCommand.groovy +++ b/jmacro-commands/jmacro-commands-base/src/main/groovy/com/itquasar/multiverse/jmacro/commands/base/commands/ConfigurationCommand.groovy @@ -8,6 +8,7 @@ import com.itquasar.multiverse.jmacro.core.engine.ConfigurationHolder import com.itquasar.multiverse.jmacro.core.engine.Core import com.itquasar.multiverse.jmacro.core.engine.ScriptConfiguration import com.itquasar.multiverse.jmacro.core.engine.ScriptEngineAware +import com.itquasar.multiverse.jmacro.core.exception.JMacroException import java.util.function.Consumer @@ -50,10 +51,8 @@ class ConfigurationCommand extends AbstractCommand implements CallableCommand { @@ -17,7 +18,9 @@ public Class getCommandType() { @Override public WrappingCommand getCommand(Core core, ScriptEngineAware scriptEngineAware) { - return new WrappingCommand<>(Command::getScriptLogger, core, scriptEngineAware, false); + Logger logger = scriptEngineAware.logger(); + Objects.requireNonNull(logger, "Logger must be not null"); + return new WrappingCommand<>(logger, core, scriptEngineAware); } } diff --git a/jmacro-core/src/main/groovy/com/itquasar/multiverse/jmacro/core/command/AbstractCommand.groovy b/jmacro-core/src/main/groovy/com/itquasar/multiverse/jmacro/core/command/AbstractCommand.groovy index b407002..3fdbdec 100644 --- a/jmacro-core/src/main/groovy/com/itquasar/multiverse/jmacro/core/command/AbstractCommand.groovy +++ b/jmacro-core/src/main/groovy/com/itquasar/multiverse/jmacro/core/command/AbstractCommand.groovy @@ -63,31 +63,31 @@ abstract class AbstractCommand implements Command, Constants { @Override Core getCore() { - return core + return this.core } @Override ScriptEngineAware getScriptEngineAware() { - return scriptEngineAware + return this.scriptEngineAware } @Override ScriptEngine getScriptEngine() { - return scriptEngineAware.scriptEngine() + return this.scriptEngineAware.scriptEngine() } @Override ScriptContext getContext() { - return context + return this.context } @Override Bindings getBindings() { - return bindings + return this.bindings } @Override Logger getScriptLogger() { - return scriptLogger + return this.scriptLogger } } diff --git a/jmacro-core/src/main/java/com/itquasar/multiverse/jmacro/core/engine/ScriptEngineAware.java b/jmacro-core/src/main/java/com/itquasar/multiverse/jmacro/core/engine/ScriptEngineAware.java index d40bf33..6dcb802 100644 --- a/jmacro-core/src/main/java/com/itquasar/multiverse/jmacro/core/engine/ScriptEngineAware.java +++ b/jmacro-core/src/main/java/com/itquasar/multiverse/jmacro/core/engine/ScriptEngineAware.java @@ -5,7 +5,7 @@ import javax.script.Bindings; import javax.script.ScriptEngine; -import com.itquasar.multiverse.jmacro.core.engine.ScriptEngineAware; +import java.util.Objects; import static com.itquasar.multiverse.jmacro.core.interfaces.Constants.SCRIPT_ENGINE_AWARE_KEY; @@ -18,11 +18,9 @@ public record ScriptEngineAware( ) { public static ScriptEngineAware of(Bindings globalScope, ScriptEngine engine, Logger scriptLogger) { - ScriptEngineAware scriptEngineAware = (ScriptEngineAware) globalScope.get(SCRIPT_ENGINE_AWARE_KEY); - scriptEngineAware = scriptEngineAware != null ? scriptEngineAware : new ScriptEngineAware( + Objects.requireNonNull(scriptLogger, "Script logger must be not null"); + return (ScriptEngineAware) globalScope.computeIfAbsent(SCRIPT_ENGINE_AWARE_KEY, (k) -> new ScriptEngineAware( engine, scriptLogger, new ScriptConfiguration(scriptLogger), new Credentials(), ScriptUI.get() - ); - globalScope.put(SCRIPT_ENGINE_AWARE_KEY, scriptEngineAware); - return scriptEngineAware; + )); } }