Skip to content

Commit

Permalink
feat(core,cmds): fixes and outputs/loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
giflw committed Sep 28, 2023
1 parent f85d120 commit ba74084
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public Optional<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -50,10 +51,8 @@ class ConfigurationCommand extends AbstractCommand implements CallableCommand<Co

ConfigurationCommand set(String name, def value) {
if (this.configHolders.containsKey(name)) {
scriptLogger.error("CONFIG HOLDER $name: ${this.configHolders.containsKey(name)}")
this.configHolders[name].set(value)
} else {
scriptLogger.error("CONFIG CUSTOM $name: ${this.custom}")
this.custom.putAt(name, value)
}
return this
Expand Down Expand Up @@ -85,6 +84,10 @@ class ConfigurationCommand extends AbstractCommand implements CallableCommand<Co
return value
}

def propertyMissing(String name, def value) {
throw new JMacroException("Property missing redirect is disabled in configuration")
}


@Override
Iterator iterator() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.itquasar.multiverse.jmacro.commands.base.providers;

import com.itquasar.multiverse.jmacro.core.command.Command;
import com.itquasar.multiverse.jmacro.core.command.CommandProvider;
import com.itquasar.multiverse.jmacro.core.command.WrappingCommand;
import com.itquasar.multiverse.jmacro.core.engine.Core;
import com.itquasar.multiverse.jmacro.core.engine.ScriptEngineAware;
import org.apache.logging.log4j.Logger;

import java.util.Objects;

@SuppressWarnings("rawtypes")
public class LoggerCommandProvider implements CommandProvider<WrappingCommand> {

Expand All @@ -17,7 +18,9 @@ public Class<WrappingCommand> getCommandType() {

@Override
public WrappingCommand<Logger> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
));
}
}

0 comments on commit ba74084

Please sign in to comment.