<commit_before>/**
 * @author <NAME>
 * @since v1
 */
package io.routr.core;

import java.util.TimerTask;
import java.util.Timer;
import javax.script.*;

public class Launcher {
  // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
    public static final String NASHORN_POLYFILL_STRING_PROTOTYPE_INCLUDES = "if (!String.prototype.includes) { Object.defineProperty(String.prototype, 'includes', { value: function(search, start) { if (typeof start !== 'number') { start = 0 } if (start + search.length > this.length) { return false } else { return this.indexOf(search, start) !== -1 } } }) }";
    // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
    public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES  = "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }";

    private static String baseScript = String.join(
        System.getProperty("line.separator"),
        NASHORN_POLYFILL_STRING_PROTOTYPE_INCLUDES,
        NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES,
        "var System = Java.type('java.lang.System')",
        "load(System.getProperty('user.dir') + '/libs/jvm-npm.js')",
        "load(System.getProperty('user.dir') + ",
        "'/libs/app.bundle.js')",
        "var Server = require('@routr/core/server')",
        "var Rest = require('@routr/rest/rest')",
        "var Registry = require('@routr/registry/registry')"
    );

    private final static String baseScriptDev = String.join(
        System.getProperty("line.separator"),
        "load(System.getProperty('user.dir') + ",
        "'/node_modules/@routr/core/server.js')"
    );

    static public void main(String... args) {
        try {
            new Launcher().launch();
        } catch(ScriptException ex) {
            System.out.println("Unable to run server: " + ex.getMessage());
        }
    }

    // TODO: Check Java version and show warning if version >= 11
    public void launch() throws ScriptException {
        String mode = System.getenv("ROUTR_LAUNCH_MODE");

        if (mode != null && mode.equalsIgnoreCase("dev")) {
            this.baseScript = this.baseScriptDev;
        }

        JSEngine engine = JSEngine.get(System.getenv("ROUTR_JS_ENGINE"));
        ScriptEngine mainCtx = createJSContext(engine);
        ScriptEngine registryCtx = createJSContext(engine);
        ScriptEngine restCtx = createJSContext(engine);

        System.out.println("DBG001");
        // Runs the main thread
        mainCtx.eval(this.baseScript);
        mainCtx.eval("new Server().start()");

        System.out.println("DBG002");
        // Runs the restful api threadPool
        restCtx.eval(this.baseScript);
        restCtx.eval("new Rest().start()");

        System.out.println("DBG003");
        // Runs the main registry thread
        registryCtx.eval(this.baseScript);
        registryCtx.eval("var reg = new Registry()");

        System.out.println("DBG004");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run () {
                try {
                    registryCtx.eval("reg.registerAll()");
                } catch(ScriptException e) {
                    // ?
                    e.printStackTrace();
                }
            }
        }, 10000, 60 * 1000);
    }

    public ScriptEngine createJSContext(JSEngine eng) throws ScriptException {
        ScriptEngine engine = null;
        try {
            engine = new ScriptEngineManager().getEngineByName(eng.getName());<commit_msg>Cleanup<commit_after>/**
 * @author <NAME>
 * @since v1
 */
package io.routr.core;

import java.util.TimerTask;
import java.util.Timer;
import javax.script.*;

public class Launcher {
  // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
    public static final String NASHORN_POLYFILL_STRING_PROTOTYPE_INCLUDES = "if (!String.prototype.includes) { Object.defineProperty(String.prototype, 'includes', { value: function(search, start) { if (typeof start !== 'number') { start = 0 } if (start + search.length > this.length) { return false } else { return this.indexOf(search, start) !== -1 } } }) }";
    // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
    public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES  = "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }";

    private static String baseScript = String.join(
        System.getProperty("line.separator"),
        NASHORN_POLYFILL_STRING_PROTOTYPE_INCLUDES,
        NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES,
        "var System = Java.type('java.lang.System')",
        "load(System.getProperty('user.dir') + '/libs/jvm-npm.js')",
        "load(System.getProperty('user.dir') + ",
        "'/libs/app.bundle.js')",
        "var Server = require('@routr/core/server')",
        "var Rest = require('@routr/rest/rest')",
        "var Registry = require('@routr/registry/registry')"
    );

    private final static String baseScriptDev = String.join(
        System.getProperty("line.separator"),
        "load(System.getProperty('user.dir') + ",
        "'/node_modules/@routr/core/server.js')"
    );

    static public void main(String... args) {
        try {
            new Launcher().launch();
        } catch(ScriptException ex) {
            System.out.println("Unable to run server: " + ex.getMessage());
        }
    }

    // TODO: Check Java version and show warning if version >= 11
    public void launch() throws ScriptException {
        String mode = System.getenv("ROUTR_LAUNCH_MODE");

        if (mode != null && mode.equalsIgnoreCase("dev")) {
            this.baseScript = this.baseScriptDev;
        }

        JSEngine engine = JSEngine.get(System.getenv("ROUTR_JS_ENGINE"));
        ScriptEngine mainCtx = createJSContext(engine);
        ScriptEngine registryCtx = createJSContext(engine);
        ScriptEngine restCtx = createJSContext(engine);

        // Runs the main thread
        mainCtx.eval(this.baseScript);
        mainCtx.eval("new Server().start()");

        // Runs the restful api threadPool
        restCtx.eval(this.baseScript);
        restCtx.eval("new Rest().start()");

        // Runs the main registry thread
        registryCtx.eval(this.baseScript);
        registryCtx.eval("var reg = new Registry()");

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run () {
                try {
                    registryCtx.eval("reg.registerAll()");
                } catch(ScriptException e) {
                    // ?
                }
            }
        }, 10000, 60 * 1000);
    }

    public ScriptEngine createJSContext(JSEngine eng) throws ScriptException {
        ScriptEngine engine = null;
        try {
            engine = new ScriptEngineManager().getEngineByName(eng.getName());
{'fraction_non_alphanumeric': 0.14375230372281608, 'fraction_numerical': 0.006634721710283818, 'mean_word_length': 2.763291724456773, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '964764', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2373, 'n_words': 528}