77import nuix .engine .Engine ;
88import nuix .engine .GlobalContainer ;
99import nuix .engine .GlobalContainerFactory ;
10+ import org .apache .logging .log4j .core .LifeCycle ;
11+ import org .jetbrains .annotations .Nullable ;
1012import org .joda .time .DateTime ;
11- import org .slf4j .Logger ;
12- import org .slf4j .LoggerFactory ;
13+ import org .apache .logging .log4j .Level ;
14+ import org .apache .logging .log4j .LogManager ;
15+ import org .apache .logging .log4j .Logger ;
16+ import org .apache .logging .log4j .core .LoggerContext ;
17+ import org .apache .logging .log4j .core .config .Configuration ;
18+ import org .apache .logging .log4j .core .config .LoggerConfig ;
1319
1420import java .io .File ;
1521import java .io .IOException ;
1622import java .lang .management .ManagementFactory ;
1723import java .lang .management .RuntimeMXBean ;
1824import java .util .Arrays ;
25+ import java .util .HashMap ;
1926import java .util .List ;
2027import java .util .Map ;
28+ import java .util .function .Consumer ;
2129import java .util .function .Supplier ;
2230
2331/***
@@ -65,7 +73,8 @@ public class NuixEngine implements AutoCloseable {
6573 protected Utilities utilities = null ;
6674 protected Thread shutdownHook = null ;
6775
68- protected NuixEngine () { }
76+ protected NuixEngine () {
77+ }
6978
7079 public static void closeGlobalContainer () {
7180 globalContainer .close ();
@@ -282,7 +291,7 @@ public NuixEngine setUserDataDirectory(File directory) {
282291 * @throws Exception Allows exceptions to bubble up so caller can handle them.
283292 */
284293 public Utilities getUtilities () throws Exception {
285- if (utilities == null ) {
294+ if (utilities == null ) {
286295 // Check to make sure some requirements are in place before proceeding
287296 checkPreConditions ();
288297
@@ -448,12 +457,15 @@ protected void initializeLogging() {
448457 // Use Log4j2 config YAML from engine base directory
449458 File log4jConfigFile = new File (engineDistributionDirectorySupplier .get (), "config/log4j2.yml" );
450459 System .setProperty ("log4j.configurationFile" , log4jConfigFile .getAbsolutePath ());
451- log = LoggerFactory .getLogger (this .getClass ());
460+ log = LogManager .getLogger (this .getClass ());
461+ log .info ("log4j.configurationFile => " + log4jConfigFile .getAbsolutePath ());
452462
453463 // Set default level to INFO
454- // Set default level to INFO
455- org .apache .log4j .Logger logger4j = org .apache .log4j .Logger .getRootLogger ();
456- logger4j .setLevel (org .apache .log4j .Level .toLevel ("INFO" ));
464+ LoggerContext ctx = (LoggerContext ) LogManager .getContext (false );
465+ Configuration config = ctx .getConfiguration ();
466+ LoggerConfig loggerConfig = config .getLoggerConfig (LogManager .ROOT_LOGGER_NAME );
467+ loggerConfig .setLevel (Level .INFO );
468+ ctx .updateLoggers ();
457469 }
458470 }
459471
@@ -519,6 +531,150 @@ public NuixVersion getNuixVersion() {
519531 return NuixVersion .parse (getNuixVersionString ());
520532 }
521533
534+ /***
535+ * Runs the Ruby script contained in the provided String. The following variables/constants are injected into the
536+ * Ruby environment before executing the script:<br>
537+ * <ul>
538+ * <li><code>$utilities</code> - The Nuix utilities object</li>
539+ * <li><code>$nuix_version</code> - A {@link NuixVersion} object representing the current engine version</li>
540+ * <li><code>NUIX_VERSION</code> - A String containing the current engine version</li>
541+ * </ul>
542+ * @param script The Ruby script to execute. Cannot be null.
543+ * @param additionalVariables A map of any additional global/local variables you would like to set. Note that
544+ * constants cannot be set using this approach. Can be null.
545+ * @param standardOutputReceiver Consumer which will receive standard output messages. If null is provided, will
546+ * default to logging info messages.
547+ * @param errorOutputReceiver Consumer which will receive error output messages. If null is provided, will
548+ * default to logging error messages.
549+ * @return A {@link RubyScriptRunner} instance. Call {@link RubyScriptRunner#join()} to wait for script to complete.
550+ * @throws Exception Exceptions are allowed to bubble up.
551+ */
552+ public RubyScriptRunner runRubyScriptAsync (String script , @ Nullable Map <String , Object > additionalVariables ,
553+ @ Nullable Consumer <String > standardOutputReceiver ,
554+ @ Nullable Consumer <String > errorOutputReceiver ) throws Exception {
555+ Map <String , Object > vars = new HashMap <>();
556+ if (additionalVariables != null ) {
557+ vars .putAll (additionalVariables );
558+ }
559+
560+ vars .put ("$utilities" , getUtilities ());
561+ vars .put ("$nuix_version" , getNuixVersion ());
562+ RubyScriptRunner rubyScriptRunner = new RubyScriptRunner ();
563+ rubyScriptRunner .setStandardOutputConsumer (standardOutputReceiver );
564+ rubyScriptRunner .setErrorOutputConsumer (errorOutputReceiver );
565+ rubyScriptRunner .runScriptAsync (script , getNuixVersionString (), vars );
566+ return rubyScriptRunner ;
567+ }
568+
569+ /***
570+ * Runs the Ruby script contained in the provided String, defaulting to logging standard/error script output.
571+ * The following variables/constants are injected into the Ruby environment before executing the script:<br>
572+ * <ul>
573+ * <li><code>$utilities</code> - The Nuix utilities object</li>
574+ * <li><code>$nuix_version</code> - A {@link NuixVersion} object representing the current engine version</li>
575+ * <li><code>NUIX_VERSION</code> - A String containing the current engine version</li>
576+ * </ul>
577+ * @param script The Ruby script to execute. Cannot be null.
578+ * @param additionalVariables A map of any additional global/local variables you would like to set. Note that
579+ * constants cannot be set using this approach. Can be null.
580+ * @return A {@link RubyScriptRunner} instance. Call {@link RubyScriptRunner#join()} to wait for script to complete.
581+ * @throws Exception Exceptions are allowed to bubble up.
582+ */
583+ public RubyScriptRunner runRubyScriptAsync (String script , @ Nullable Map <String , Object > additionalVariables ) throws Exception {
584+ return runRubyScriptAsync (script , additionalVariables , null , null );
585+ }
586+
587+ /***
588+ * Runs the Ruby script contained in the provided String, defaulting to logging standard/error script output.
589+ * The following variables/constants are injected into the Ruby environment before executing the script:<br>
590+ * <ul>
591+ * <li><code>$utilities</code> - The Nuix utilities object</li>
592+ * <li><code>$nuix_version</code> - A {@link NuixVersion} object representing the current engine version</li>
593+ * <li><code>NUIX_VERSION</code> - A String containing the current engine version</li>
594+ * </ul>
595+ * @param script The Ruby script to execute. Cannot be null.
596+ * @return A {@link RubyScriptRunner} instance. Call {@link RubyScriptRunner#join()} to wait for script to complete.
597+ * @throws Exception Exceptions are allowed to bubble up.
598+ */
599+ public RubyScriptRunner runRubyScriptAsync (String script ) throws Exception {
600+ return runRubyScriptAsync (script , null , null , null );
601+ }
602+
603+ /***
604+ * Runs the Ruby script contained in the specified file. Note that since you are pointing to an actual file, script
605+ * will have a defined value for <code>__FILE__</code>, allowing for you to reference other files relative to the
606+ * specified script file. The following variables/constants are injected into the Ruby environment before
607+ * executing the script:<br>
608+ * <ul>
609+ * <li><code>$utilities</code> - The Nuix utilities object</li>
610+ * <li><code>$nuix_version</code> - A {@link NuixVersion} object representing the current engine version</li>
611+ * <li><code>NUIX_VERSION</code> - A String containing the current engine version</li>
612+ * </ul>
613+ * @param scriptFile The file containing the Ruby script to execute. Cannot be null.
614+ * @param additionalVariables A map of any additional global/local variables you would like to set. Note that
615+ * constants cannot be set using this approach. Can be null.
616+ * @param standardOutputReceiver Consumer which will receive standard output messages. If null is provided, will
617+ * default to logging info messages.
618+ * @param errorOutputReceiver Consumer which will receive error output messages. If null is provided, will
619+ * default to logging error messages.
620+ * @return A {@link RubyScriptRunner} instance. Call {@link RubyScriptRunner#join()} to wait for script to complete.
621+ * @throws Exception Exceptions are allowed to bubble up.
622+ */
623+ public RubyScriptRunner runRubyScriptFileAsync (File scriptFile , @ Nullable Map <String , Object > additionalVariables ,
624+ @ Nullable Consumer <String > standardOutputReceiver ,
625+ @ Nullable Consumer <String > errorOutputReceiver ) throws Exception {
626+ Map <String , Object > vars = new HashMap <>();
627+ if (additionalVariables != null ) {
628+ vars .putAll (additionalVariables );
629+ }
630+
631+ vars .put ("$utilities" , getUtilities ());
632+ vars .put ("$nuix_version" , getNuixVersion ());
633+ RubyScriptRunner rubyScriptRunner = new RubyScriptRunner ();
634+ rubyScriptRunner .setStandardOutputConsumer (standardOutputReceiver );
635+ rubyScriptRunner .setErrorOutputConsumer (errorOutputReceiver );
636+ rubyScriptRunner .runFileAsync (scriptFile , getNuixVersionString (), vars );
637+ return rubyScriptRunner ;
638+ }
639+
640+ /***
641+ * Runs the Ruby script contained in the specified file, defaulting to logging standard/error script output.
642+ * Note that since you are pointing to an actual file, script will have a defined value for <code>__FILE__</code>,
643+ * allowing for you to reference other files relative to the specified script file. The following
644+ * variables/constants are injected into the Ruby environment before executing the script:<br>
645+ * <ul>
646+ * <li><code>$utilities</code> - The Nuix utilities object</li>
647+ * <li><code>$nuix_version</code> - A {@link NuixVersion} object representing the current engine version</li>
648+ * <li><code>NUIX_VERSION</code> - A String containing the current engine version</li>
649+ * </ul>
650+ * @param scriptFile The file containing the Ruby script to execute. Cannot be null.
651+ * @param additionalVariables A map of any additional global/local variables you would like to set. Note that
652+ * constants cannot be set using this approach. Can be null.
653+ * @return A {@link RubyScriptRunner} instance. Call {@link RubyScriptRunner#join()} to wait for script to complete.
654+ * @throws Exception Exceptions are allowed to bubble up.
655+ */
656+ public RubyScriptRunner runRubyScriptFileAsync (File scriptFile , @ Nullable Map <String , Object > additionalVariables ) throws Exception {
657+ return runRubyScriptFileAsync (scriptFile , additionalVariables , null , null );
658+ }
659+
660+ /***
661+ * Runs the Ruby script contained in the specified file, defaulting to logging standard/error script output.
662+ * Note that since you are pointing to an actual file, script will have a defined value for <code>__FILE__</code>,
663+ * allowing for you to reference other files relative to the specified script file. The following
664+ * variables/constants are injected into the Ruby environment before executing the script:<br>
665+ * <ul>
666+ * <li><code>$utilities</code> - The Nuix utilities object</li>
667+ * <li><code>$nuix_version</code> - A {@link NuixVersion} object representing the current engine version</li>
668+ * <li><code>NUIX_VERSION</code> - A String containing the current engine version</li>
669+ * </ul>
670+ * @param scriptFile The file containing the Ruby script to execute. Cannot be null.
671+ * @return A {@link RubyScriptRunner} instance. Call {@link RubyScriptRunner#join()} to wait for script to complete.
672+ * @throws Exception Exceptions are allowed to bubble up.
673+ */
674+ public RubyScriptRunner runRubyScriptFileAsync (File scriptFile ) throws Exception {
675+ return runRubyScriptFileAsync (scriptFile , null , null , null );
676+ }
677+
522678 /***
523679 * Logs information about all Nuix third party dependencies
524680 * @param utilities Needs an instance of Utilities to get access to third party dependency information
@@ -586,5 +742,11 @@ public void close() throws Exception {
586742 Runtime .getRuntime ().removeShutdownHook (shutdownHook );
587743 shutdownHook = null ;
588744 }
745+
746+ // Shutdown logging
747+ if (log != null ) {
748+ ((LifeCycle ) LogManager .getContext ()).stop ();
749+ log = null ;
750+ }
589751 }
590752}
0 commit comments